node/test/async-hooks/test-httpparser-reuse.js
Rich Trott 3b063dc21c test: use consistent test naming
To conform with other test names, move
test/async-hooks/test-httparser-reuse.js to
test/async-hooks/test-httpparser-reuse.js.

PR-URL: https://github.com/nodejs/node/pull/28744
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-07-18 11:48:58 -07: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)));
}