Skip to content

Commit d648ecc

Browse files
Trotttargos
authored andcommitted
test: improve test-async-hooks-http-parser-destroy
Improve reporting in test-async-hooks-http-parser-destroy when failing. Before, failures looked like this (edited slightly to conform to our commit message 72-char line length restriction): The expression evaluated to a falsy value: assert.ok(destroyedIds.indexOf(createdAsyncId) >= 0) Now, you get a slightly better idea of what's up. (Is it missing one ID? More than one? All of them?): AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected ... Lines skipped [ 156, ... 757, - 761, 765 ] PR-URL: #27319 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent b68ecf3 commit d648ecc

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
2-
const common = require('../common');
3-
const Countdown = require('../common/countdown');
2+
require('../common');
43
const assert = require('assert');
54
const async_hooks = require('async_hooks');
65
const http = require('http');
@@ -15,13 +14,18 @@ const KEEP_ALIVE = 100;
1514
const createdIds = [];
1615
const destroyedIds = [];
1716
async_hooks.createHook({
18-
init: common.mustCallAtLeast((asyncId, type) => {
17+
init: (asyncId, type) => {
1918
if (type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST') {
2019
createdIds.push(asyncId);
2120
}
22-
}, N),
21+
},
2322
destroy: (asyncId) => {
24-
destroyedIds.push(asyncId);
23+
if (createdIds.includes(asyncId)) {
24+
destroyedIds.push(asyncId);
25+
}
26+
if (destroyedIds.length === 2 * N) {
27+
server.close();
28+
}
2529
}
2630
}).enable();
2731

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

37-
const countdown = new Countdown(N, () => {
38-
server.close(() => {
39-
// Give the server sockets time to close (which will also free their
40-
// associated parser objects) after the server has been closed.
41-
setTimeout(() => {
42-
assert.strictEqual(createdIds.length, 2 * N);
43-
createdIds.forEach((createdAsyncId) => {
44-
assert.ok(destroyedIds.indexOf(createdAsyncId) >= 0);
45-
});
46-
}, KEEP_ALIVE * 2);
47-
});
48-
});
49-
5041
server.listen(0, function() {
5142
for (let i = 0; i < N; ++i) {
5243
(function makeRequest() {
5344
http.get({
5445
port: server.address().port,
5546
agent: keepAliveAgent
5647
}, function(res) {
57-
countdown.dec();
5848
res.resume();
5949
});
6050
})();
6151
}
6252
});
53+
54+
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);
58+
}
59+
60+
process.on('SIGTERM', () => {
61+
// Catching SIGTERM and calling `process.exit(1)` so that the `exit` event
62+
// is triggered and the assertions are checked. This can be useful for
63+
// troubleshooting this test if it times out.
64+
process.exit(1);
65+
});
66+
67+
// Ordinary exit.
68+
process.on('exit', checkOnExit);

0 commit comments

Comments
 (0)