Skip to content

Commit c208f9d

Browse files
addaleaxjasnell
authored andcommittedJun 5, 2017
test: check destroy hooks are called before exit
Verify that the destroy callback for a TCP server is called before exit if it is closed in another destroy callback. Fixes: #13262 PR-URL: #13369 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Andreas Madsen <[email protected]>
1 parent 6917df2 commit c208f9d

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
// Test that async ids that are added to the destroy queue while running a
3+
// `destroy` callback are handled correctly.
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const async_hooks = require('async_hooks');
8+
9+
const initCalls = new Set();
10+
let destroyResCallCount = 0;
11+
let res2;
12+
13+
async_hooks.createHook({
14+
init: common.mustCallAtLeast((id, provider, triggerId) => {
15+
if (provider === 'foobar')
16+
initCalls.add(id);
17+
}, 2),
18+
destroy: common.mustCallAtLeast((id) => {
19+
if (!initCalls.has(id)) return;
20+
21+
switch (destroyResCallCount++) {
22+
case 0:
23+
// Trigger the second `destroy` call.
24+
res2.emitDestroy();
25+
break;
26+
case 2:
27+
assert.fail('More than 2 destroy() invocations');
28+
break;
29+
}
30+
}, 2)
31+
}).enable();
32+
33+
const res1 = new async_hooks.AsyncResource('foobar');
34+
res2 = new async_hooks.AsyncResource('foobar');
35+
res1.emitDestroy();
36+
37+
process.on('exit', () => assert.strictEqual(destroyResCallCount, 2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// Regression test for https://github.com/nodejs/node/issues/13262
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const async_hooks = require('async_hooks');
8+
9+
let seenId, seenResource;
10+
11+
async_hooks.createHook({
12+
init: common.mustCall((id, provider, triggerId, resource) => {
13+
seenId = id;
14+
seenResource = resource;
15+
assert.strictEqual(provider, 'Immediate');
16+
assert.strictEqual(triggerId, 1);
17+
}),
18+
before: common.mustNotCall(),
19+
after: common.mustNotCall(),
20+
destroy: common.mustCall((id) => {
21+
assert.strictEqual(seenId, id);
22+
})
23+
}).enable();
24+
25+
const immediate = setImmediate(common.mustNotCall());
26+
assert.strictEqual(immediate, seenResource);
27+
clearImmediate(immediate);

0 commit comments

Comments
 (0)
Please sign in to comment.