Skip to content

Commit 2d55f08

Browse files
committed
added unit tests
1 parent 8e5b4a2 commit 2d55f08

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
const { Readable, finished } = require('stream');
6+
const { AsyncLocalStorage } = require('async_hooks');
7+
const { strictEqual } = require('assert');
8+
const AsyncContextFrame = require('internal/async_context_frame');
9+
const internalAsyncHooks = require('internal/async_hooks');
10+
11+
// This test verifies that ALS context is preserved when using stream.finished()
12+
13+
const als = new AsyncLocalStorage();
14+
const readable = new Readable();
15+
16+
als.run('test-context-1', () => {
17+
finished(readable, common.mustCall(() => {
18+
strictEqual(AsyncContextFrame.enabled || internalAsyncHooks.getHookArrays()[0].length > 0, true, 'test-context-1', 'One of AsyncContextFrame or async hooks criteria should be met');
19+
strictEqual(als.getStore(), 'test-context-1', 'ALS context should be preserved');
20+
}));
21+
});
22+
23+
readable.destroy();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
const { Readable, finished } = require('stream');
6+
const { createHook } = require('async_hooks');
7+
const { strictEqual } = require('assert');
8+
const internalAsyncHooks = require('internal/async_hooks');
9+
10+
// This test verifies that when there are active async hooks, stream.finished() uses
11+
// the bindAsyncResource path
12+
13+
createHook({}).enable();
14+
const readable = new Readable();
15+
16+
finished(readable, common.mustCall(() => {
17+
strictEqual(internalAsyncHooks.getHookArrays()[0].length > 0, true, 'Should have active user async hook');
18+
}));
19+
20+
readable.destroy();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --expose-internals --no-async-context-frame
2+
'use strict';
3+
4+
const common = require('../common');
5+
const { Readable, finished } = require('stream');
6+
const { strictEqual } = require('assert');
7+
const AsyncContextFrame = require('internal/async_context_frame');
8+
const internalAsyncHooks = require('internal/async_hooks');
9+
10+
// This test verifies that when there are no active async hooks, stream.finished() uses the default callback path
11+
12+
const readable = new Readable();
13+
14+
finished(readable, common.mustCall(() => {
15+
strictEqual(internalAsyncHooks.getHookArrays()[0].length === 0, true, 'Should not have active user async hook');
16+
strictEqual(AsyncContextFrame.current() || internalAsyncHooks.getHookArrays()[0].length > 0, false, 'Default callback path should be used');
17+
}));
18+
19+
readable.destroy();

0 commit comments

Comments
 (0)