Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: introduce NODE_TEST_WORKER_ID for improved concurrent te… #56091

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -3306,6 +3306,12 @@ If `value` equals `'child'`, test reporter options will be overridden and test
output will be sent to stdout in the TAP format. If any other value is provided,
Node.js makes no guarantees about the reporter format used or its stability.

### `NODE_TEST_WORKER_ID`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the correct place for this. this is a list of env vars node accepts, not env vars it exposes


This environment variable is set by the test runner. When using process-level test
isolation, each worker process is assigned a unique ID, starting at `1`. This is
set to 1 for all tests when test isolation is disabled.

### `NODE_TLS_REJECT_UNAUTHORIZED=value`

If `value` equals `'0'`, certificate validation is disabled for TLS connections.
Expand Down
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I have 8 cpus and 16 tests, I would expect each worker to be reused twice, but this implementation will not reuse worker ids, I think that is misleading

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
5 changes: 5 additions & 0 deletions test/fixtures/test-runner/worker-id/1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const test = require('node:test');

test('test1', t => {
console.log('NODE_TEST_WORKER_ID', process.env.NODE_TEST_WORKER_ID)
});
5 changes: 5 additions & 0 deletions test/fixtures/test-runner/worker-id/2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const test = require('node:test');

test('test2', t => {
console.log('NODE_TEST_WORKER_ID', process.env.NODE_TEST_WORKER_ID)
});
5 changes: 5 additions & 0 deletions test/fixtures/test-runner/worker-id/3.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const test = require('node:test');

test('test3', t => {
console.log('NODE_TEST_WORKER_ID', process.env.NODE_TEST_WORKER_ID)
});
100 changes: 100 additions & 0 deletions test/parallel/test-runner-worker-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

require('../common');
const assert = require('assert');
const test = require('node:test');
const { spawnSync, spawn } = require('child_process');
const { join } = require('path');
const fixtures = require('../common/fixtures');
const testFixtures = fixtures.path('test-runner');

test('testing with isolation enabled', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I suggest being more expressive about what we are testing.

For example, describe('test runner worker-id'), followed by it('should set NODE_TEST_WORKER_ID in isolation x'), etc.

const args = ['--test', '--experimental-test-isolation=process'];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'worker-id') });

assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();

assert.match(stdout, /NODE_TEST_WORKER_ID 1/);
assert.match(stdout, /NODE_TEST_WORKER_ID 2/);
assert.match(stdout, /NODE_TEST_WORKER_ID 3/);

assert.strictEqual(child.status, 0);
});

test('testing with isolation disabled', () => {
const args = ['--test', '--experimental-test-isolation=none'];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'worker-id') });

assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
const regex = /NODE_TEST_WORKER_ID 1/g;
const result = stdout.match(regex);

assert.strictEqual(result.length, 3);
assert.strictEqual(child.status, 0);
});


test('testing with isolation enabled in watch mode', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watch mode tests added here have at least two problems:

  1. They don't actually test watch mode beyond passing the --watch flag. To properly test watch mode, you have to trigger the restart functionality.
  2. These tests will almost certainly be flaky in the CI. Waiting for a second, killing the child process, and expecting it to have run successfully probably works locally, but the CI machines can be very slow. I don't think we want to have a timeout at all.

I think these need to be refactored so that:

  • The test runs once.
  • Once the expected output is seen, a source file is modified (we don't want to modify the fixtures files directly either) to trigger a restart.
  • The test runs again. We need the output to be the same both times.

const args = ['--watch', '--test', '--experimental-test-isolation=process'];

const child = spawn(process.execPath, args, { cwd: join(testFixtures, 'worker-id') });

let outputData = '';
let errorData = '';

child.stdout.on('data', (data) => {
outputData += data.toString();
});

child.stderr.on('data', (data) => {
errorData += data.toString();
});

setTimeout(() => {
child.kill();

assert.strictEqual(errorData, '');
assert.match(outputData, /NODE_TEST_WORKER_ID 1/);
assert.match(outputData, /NODE_TEST_WORKER_ID 2/);
assert.match(outputData, /NODE_TEST_WORKER_ID 3/);

if (child.exitCode !== null) {
assert.strictEqual(child.exitCode, null); // Since we killed it manually
}
}, 1000);

});

test('testing with isolation disabled in watch mode', async () => {
const args = ['--watch', '--test', '--experimental-test-isolation=none'];

const child = spawn(process.execPath, args, { cwd: join(testFixtures, 'worker-id') });

let outputData = '';
let errorData = '';

child.stdout.on('data', (data) => {
outputData += data.toString();
});

child.stderr.on('data', (data) => {
errorData += data.toString();
});

setTimeout(() => {
child.kill();

assert.strictEqual(errorData, '');
const regex = /NODE_TEST_WORKER_ID 1/g;
const result = outputData.match(regex);

assert.strictEqual(result.length, 3);

if (child.exitCode !== null) {
assert.strictEqual(child.exitCode, null);
}
}, 1000);

});
Loading