Skip to content

Commit 2142b6d

Browse files
Flarnatargos
authored andcommitted
test: improve test-async-hooks-http-parser-destroy
Improve asserts to distinguish between reequest and response parsers. Change the assert sequence to first assert on the number of ids to easier identify if some operation is missing/incomplete. Destroy HTTP agent once expected number of events have been seen to avoid waiting on socket timeouts. Refs: #28112 PR-URL: #28253 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 406c50c commit 2142b6d

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

test/parallel/test-async-hooks-http-parser-destroy.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,34 @@ const http = require('http');
1111
const N = 50;
1212
const KEEP_ALIVE = 100;
1313

14-
const createdIds = [];
15-
const destroyedIds = [];
14+
const createdIdsIncomingMessage = [];
15+
const createdIdsClientRequest = [];
16+
const destroyedIdsIncomingMessage = [];
17+
const destroyedIdsClientRequest = [];
18+
1619
async_hooks.createHook({
1720
init: (asyncId, type) => {
18-
if (type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST') {
19-
createdIds.push(asyncId);
21+
if (type === 'HTTPINCOMINGMESSAGE') {
22+
createdIdsIncomingMessage.push(asyncId);
23+
}
24+
if (type === 'HTTPCLIENTREQUEST') {
25+
createdIdsClientRequest.push(asyncId);
2026
}
2127
},
2228
destroy: (asyncId) => {
23-
if (createdIds.includes(asyncId)) {
24-
destroyedIds.push(asyncId);
29+
if (createdIdsIncomingMessage.includes(asyncId)) {
30+
destroyedIdsIncomingMessage.push(asyncId);
2531
}
26-
if (destroyedIds.length === 2 * N) {
32+
if (createdIdsClientRequest.includes(asyncId)) {
33+
destroyedIdsClientRequest.push(asyncId);
34+
}
35+
36+
if (destroyedIdsClientRequest.length === N && keepAliveAgent) {
37+
keepAliveAgent.destroy();
38+
keepAliveAgent = undefined;
39+
}
40+
41+
if (destroyedIdsIncomingMessage.length === N && server.listening) {
2742
server.close();
2843
}
2944
}
@@ -33,28 +48,34 @@ const server = http.createServer((req, res) => {
3348
res.end('Hello');
3449
});
3550

36-
const keepAliveAgent = new http.Agent({
51+
let keepAliveAgent = new http.Agent({
3752
keepAlive: true,
3853
keepAliveMsecs: KEEP_ALIVE,
3954
});
4055

41-
server.listen(0, function() {
56+
server.listen(0, () => {
4257
for (let i = 0; i < N; ++i) {
4358
(function makeRequest() {
4459
http.get({
4560
port: server.address().port,
4661
agent: keepAliveAgent
47-
}, function(res) {
62+
}, (res) => {
4863
res.resume();
4964
});
5065
})();
5166
}
5267
});
5368

5469
function checkOnExit() {
55-
assert.deepStrictEqual(destroyedIds.sort(), createdIds.sort());
56-
// There should be two IDs for each request.
57-
assert.strictEqual(createdIds.length, N * 2);
70+
assert.strictEqual(createdIdsIncomingMessage.length, N);
71+
assert.strictEqual(createdIdsClientRequest.length, N);
72+
assert.strictEqual(destroyedIdsIncomingMessage.length, N);
73+
assert.strictEqual(destroyedIdsClientRequest.length, N);
74+
75+
assert.deepStrictEqual(destroyedIdsIncomingMessage.sort(),
76+
createdIdsIncomingMessage.sort());
77+
assert.deepStrictEqual(destroyedIdsClientRequest.sort(),
78+
createdIdsClientRequest.sort());
5879
}
5980

6081
process.on('SIGTERM', () => {

0 commit comments

Comments
 (0)