|
3 | 3 |
|
4 | 4 | const common = require('../common'); |
5 | 5 | const { Readable, finished } = require('stream'); |
6 | | -const { createHook } = require('async_hooks'); |
| 6 | +const { createHook, executionAsyncId } = require('async_hooks'); |
7 | 7 | const { strictEqual } = require('assert'); |
8 | 8 | const internalAsyncHooks = require('internal/async_hooks'); |
9 | 9 |
|
10 | 10 | // This test verifies that when there are active async hooks, stream.finished() uses |
11 | 11 | // the bindAsyncResource path |
12 | 12 |
|
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'); |
14 | 25 | const readable = new Readable(); |
15 | 26 |
|
16 | 27 | finished(readable, common.mustCall(() => { |
| 28 | + const currentAsyncId = executionAsyncId(); |
| 29 | + const ctx = contextMap.get(currentAsyncId); |
17 | 30 | strictEqual(internalAsyncHooks.getHookArrays()[0].length > 0, |
18 | 31 | true, 'Should have active user async hook'); |
| 32 | + strictEqual(ctx, 'abc-123', 'Context should be preserved'); |
19 | 33 | })); |
20 | 34 |
|
21 | 35 | readable.destroy(); |
0 commit comments