node/test/parallel/test-stream-finished-async-local-storage.js
avcribl 4fe325d93d
stream: preserve AsyncLocalStorage on finished only when needed
PR-URL: https://github.com/nodejs/node/pull/59873
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-10-27 19:23:34 +00:00

25 lines
890 B
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const { Readable, finished } = require('stream');
const { AsyncLocalStorage } = require('async_hooks');
const { strictEqual } = require('assert');
const AsyncContextFrame = require('internal/async_context_frame');
const internalAsyncHooks = require('internal/async_hooks');
// This test verifies that ALS context is preserved when using stream.finished()
const als = new AsyncLocalStorage();
const readable = new Readable();
als.run('test-context-1', () => {
finished(readable, common.mustCall(() => {
strictEqual(AsyncContextFrame.enabled || internalAsyncHooks.getHookArrays()[0].length > 0,
true, 'One of AsyncContextFrame or async hooks criteria should be met');
strictEqual(als.getStore(), 'test-context-1', 'ALS context should be preserved');
}));
});
readable.destroy();