Skip to content

Commit

Permalink
test_runner: introduce NODE_TEST_WORKER_ID for improved concurrent te…
Browse files Browse the repository at this point in the history
…st execution

Added a new environment variable, `NODE_TEST_WORKER_ID`, which ranges from 1 to N when `--experimental-test-isolation=process` is enabled and defaults to 1 when `--experimental-test-isolation=none` is used.
  • Loading branch information
cu8code committed Nov 30, 2024
1 parent 3fb2ea8 commit 9e1d701
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,14 @@ class FileTest extends Test {
}
}

function runTestFile(path, filesWatcher, opts) {
function runTestFile(path, filesWatcher, opts, workerId = 1) {
const watchMode = filesWatcher != null;
const testPath = path === kIsolatedProcessName ? '' : path;
const testOpts = { __proto__: null, signal: opts.signal };
const subtest = opts.root.createSubtest(FileTest, testPath, testOpts, async (t) => {
const args = getRunArgs(path, opts);
const stdio = ['pipe', 'pipe', 'pipe'];
const env = { __proto__: null, ...process.env, NODE_TEST_CONTEXT: 'child-v8' };
const env = { __proto__: null, ...process.env, NODE_TEST_CONTEXT: 'child-v8', NODE_TEST_WORKER_ID: workerId };
if (watchMode) {
stdio.push('ipc');
env.WATCH_REPORT_DEPENDENCIES = '1';
Expand Down Expand Up @@ -724,8 +724,10 @@ function run(options = kEmptyObject) {
runFiles = () => {
root.harness.bootstrapPromise = null;
root.harness.buildPromise = null;
let workerId = 1;
return SafePromiseAllSettledReturnVoid(testFiles, (path) => {
const subtest = runTestFile(path, filesWatcher, opts);
const subtest = runTestFile(path, filesWatcher, opts, workerId);
workerId++;
filesWatcher?.runningSubtests.set(path, subtest);
return subtest;
});
Expand Down Expand Up @@ -766,6 +768,7 @@ function run(options = kEmptyObject) {

root.entryFile = resolve(testFile);
debug('loading test file:', fileURL.href);
process.env.NODE_TEST_WORKER_ID = 1;
try {
await cascadedLoader.import(fileURL, parent, { __proto__: null });
} catch (err) {
Expand Down

0 comments on commit 9e1d701

Please sign in to comment.