nodejs/test/parallel/test-async-context-frame.mjs
Bryan English ef6b9ffc8d
async_hooks: add an InactiveAsyncContextFrame class
This gives a class prototype for AsyncContextFrame that contains the
required methods, so that when we swap the prototype,
ActiveAsyncContextFrame methods are used instead. Previously, the
methods were defined in AsyncContextFrame, so swapping the prototype
didn't swap those static methods.

Also, make the ActiveAsyncContextFrame extend from Map.

Fixes: https://github.com/nodejs/node/issues/54503
PR-URL: https://github.com/nodejs/node/pull/54510
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
2024-08-30 20:27:28 +00:00

62 lines
1.6 KiB
JavaScript

import { isWindows } from '../common/index.mjs';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { opendir } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { describe, it } from 'node:test';
import { sep } from 'node:path';
import { strictEqual } from 'node:assert';
const python = process.env.PYTHON || (isWindows ? 'python' : 'python3');
const testRunner = fileURLToPath(
new URL('../../tools/test.py', import.meta.url)
);
const setNames = ['async-hooks', 'parallel'];
// Get all test names for each set
const testSets = await Promise.all(setNames.map(async (name) => {
const path = fileURLToPath(new URL(`../${name}`, import.meta.url));
const dir = await opendir(path);
const tests = [];
for await (const entry of dir) {
if (entry.name.startsWith('test-async-local-storage-')) {
tests.push(entry.name);
}
}
return {
name,
tests
};
}));
// Merge test sets with set name prefix
const tests = testSets.reduce((m, v) => {
for (const test of v.tests) {
m.push(`${v.name}${sep}${test}`);
}
return m;
}, []);
describe('AsyncContextFrame', {
concurrency: tests.length
}, () => {
for (const test of tests) {
it(test, async () => {
const proc = spawn(python, [
testRunner,
'--node-args=--experimental-async-context-frame',
test,
], {
stdio: ['ignore', 'ignore', 'inherit'],
});
const [code] = await once(proc, 'exit');
strictEqual(code, 0, `Test ${test} failed with exit code ${code}`);
});
}
});