Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test-async-hooks-http-parser-destroy #27319

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions test/parallel/test-async-hooks-http-parser-destroy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const common = require('../common');
const Countdown = require('../common/countdown');
require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const http = require('http');
Expand All @@ -15,13 +14,18 @@ const KEEP_ALIVE = 100;
const createdIds = [];
const destroyedIds = [];
async_hooks.createHook({
init: common.mustCallAtLeast((asyncId, type) => {
init: (asyncId, type) => {
if (type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST') {
createdIds.push(asyncId);
}
}, N),
},
destroy: (asyncId) => {
destroyedIds.push(asyncId);
if (createdIds.includes(asyncId)) {
destroyedIds.push(asyncId);
}
if (destroyedIds.length === 2 * N) {
server.close();
}
}
}).enable();

Expand All @@ -34,29 +38,31 @@ const keepAliveAgent = new http.Agent({
keepAliveMsecs: KEEP_ALIVE,
});

const countdown = new Countdown(N, () => {
server.close(() => {
// Give the server sockets time to close (which will also free their
// associated parser objects) after the server has been closed.
setTimeout(() => {
assert.strictEqual(createdIds.length, 2 * N);
createdIds.forEach((createdAsyncId) => {
assert.ok(destroyedIds.indexOf(createdAsyncId) >= 0);
});
}, KEEP_ALIVE * 2);
});
});

server.listen(0, function() {
for (let i = 0; i < N; ++i) {
(function makeRequest() {
http.get({
port: server.address().port,
agent: keepAliveAgent
}, function(res) {
countdown.dec();
res.resume();
});
})();
}
});

function checkOnExit() {
assert.deepStrictEqual(destroyedIds.sort(), createdIds.sort());
// There should be two IDs for each request.
assert.strictEqual(createdIds.length, N * 2);
}

process.on('SIGTERM', () => {
// Catching SIGTERM and calling `process.exit(1)` so that the `exit` event
// is triggered and the assertions are checked. This can be useful for
// troubleshooting this test if it times out.
process.exit(1);
Trott marked this conversation as resolved.
Show resolved Hide resolved
});

// Ordinary exit.
process.on('exit', checkOnExit);