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 <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent b68ecf3 commit d648ecc

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
2-
constcommon=require('../common');
3-
constCountdown=require('../common/countdown');
2+
require('../common');
43
constassert=require('assert');
54
constasync_hooks=require('async_hooks');
65
consthttp=require('http');
@@ -15,13 +14,18 @@ const KEEP_ALIVE = 100;
1514
constcreatedIds=[];
1615
constdestroyedIds=[];
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-
constcountdown=newCountdown(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(leti=0;i<N;++i){
5243
(functionmakeRequest(){
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+
functioncheckOnExit(){
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)