Skip to content

Commit d51ca0d

Browse files
committed
expanded on the unit test
1 parent c49b350 commit d51ca0d

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

test/parallel/test-stream-finished-bindAsyncResource-path.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
33

44
const common = require('../common');
55
const { Readable, finished } = require('stream');
6-
const { createHook } = require('async_hooks');
6+
const { createHook, executionAsyncId } = require('async_hooks');
77
const { strictEqual } = require('assert');
88
const internalAsyncHooks = require('internal/async_hooks');
99

1010
// This test verifies that when there are active async hooks, stream.finished() uses
1111
// the bindAsyncResource path
1212

13-
createHook({}).enable();
13+
createHook({
14+
init(asyncId, type, triggerAsyncId) {
15+
if (type === 'STREAM_END_OF_STREAM') {
16+
const parentContext = contextMap.get(triggerAsyncId);
17+
contextMap.set(asyncId, parentContext);
18+
}
19+
}
20+
}).enable();
21+
22+
const contextMap = new Map();
23+
const asyncId = executionAsyncId();
24+
contextMap.set(asyncId, 'abc-123');
1425
const readable = new Readable();
1526

1627
finished(readable, common.mustCall(() => {
28+
const currentAsyncId = executionAsyncId();
29+
const ctx = contextMap.get(currentAsyncId);
1730
strictEqual(internalAsyncHooks.getHookArrays()[0].length > 0,
1831
true, 'Should have active user async hook');
32+
strictEqual(ctx, 'abc-123', 'Context should be preserved');
1933
}));
2034

2135
readable.destroy();

0 commit comments

Comments
 (0)