node/test/async-hooks/test-httpparser-reuse.js
Antoine du Hamel cf46746b8a
test: add trailing commas in async-hooks tests (#45549)
PR-URL: https://github.com/nodejs/node/pull/45549
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2022-11-21 18:43:47 +01:00

76 lines
1.9 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { createHook } = require('async_hooks');
const http = require('http');
// Verify that resource emitted for an HTTPParser is not reused.
// Verify that correct create/destroy events are emitted.
const reused = Symbol('reused');
const reusedParser = [];
const incomingMessageParser = [];
const clientRequestParser = [];
const dupDestroys = [];
const destroyed = [];
createHook({
init(asyncId, type, triggerAsyncId, resource) {
switch (type) {
case 'HTTPINCOMINGMESSAGE':
incomingMessageParser.push(asyncId);
break;
case 'HTTPCLIENTREQUEST':
clientRequestParser.push(asyncId);
break;
}
if (resource[reused]) {
reusedParser.push(
`resource reused: ${asyncId}, ${triggerAsyncId}, ${type}`,
);
}
resource[reused] = true;
},
destroy(asyncId) {
if (destroyed.includes(asyncId)) {
dupDestroys.push(asyncId);
} else {
destroyed.push(asyncId);
}
},
}).enable();
const server = http.createServer((req, res) => {
res.end();
});
server.listen(0, common.mustCall(() => {
const PORT = server.address().port;
const url = `http://127.0.0.1:${PORT}`;
http.get(url, common.mustCall(() => {
server.close(common.mustCall(() => {
server.listen(PORT, common.mustCall(() => {
http.get(url, common.mustCall(() => {
server.close(common.mustCall(() => {
setTimeout(common.mustCall(verify), 200);
}));
}));
}));
}));
}));
}));
function verify() {
assert.strictEqual(reusedParser.length, 0);
assert.strictEqual(incomingMessageParser.length, 2);
assert.strictEqual(clientRequestParser.length, 2);
assert.strictEqual(dupDestroys.length, 0);
incomingMessageParser.forEach((id) => assert.ok(destroyed.includes(id)));
clientRequestParser.forEach((id) => assert.ok(destroyed.includes(id)));
}