Skip to content

Commit 9f9c582

Browse files
committed
test_runner, cli: add --test-concurrency flag
This commit adds a new --test-concurrency CLI flag that controls the parallelism of the test runner CLI. PR-URL: #49996 Fixes: #49487 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 69fb55e commit 9f9c582

File tree

7 files changed

+70
-6
lines changed

7 files changed

+70
-6
lines changed

doc/api/cli.md

+9
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,15 @@ Starts the Node.js command line test runner. This flag cannot be combined with
16541654
See the documentation on [running tests from the command line][]
16551655
for more details.
16561656

1657+
### `--test-concurrency`
1658+
1659+
<!-- YAML
1660+
added: REPLACEME
1661+
-->
1662+
1663+
The maximum number of test files that the test runner CLI will execute
1664+
concurrently. The default value is `os.availableParallelism() - 1`.
1665+
16571666
### `--test-name-pattern`
16581667

16591668
<!-- YAML

doc/api/test.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,12 @@ in the [test runner execution model][] section.
350350

351351
### Test runner execution model
352352

353-
Each matching test file is executed in a separate child process. If the child
354-
process finishes with an exit code of 0, the test is considered passing.
355-
Otherwise, the test is considered to be a failure. Test files must be
356-
executable by Node.js, but are not required to use the `node:test` module
357-
internally.
353+
Each matching test file is executed in a separate child process. The maximum
354+
number of child processes running at any time is controlled by the
355+
[`--test-concurrency`][] flag. If the child process finishes with an exit code
356+
of 0, the test is considered passing. Otherwise, the test is considered to be a
357+
failure. Test files must be executable by Node.js, but are not required to use
358+
the `node:test` module internally.
358359

359360
Each test file is executed as if it was a regular script. That is, if the test
360361
file itself uses `node:test` to define tests, all of those tests will be
@@ -2551,6 +2552,7 @@ added:
25512552
[TTY]: tty.md
25522553
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
25532554
[`--import`]: cli.md#--importmodule
2555+
[`--test-concurrency`]: cli.md#--test-concurrency
25542556
[`--test-name-pattern`]: cli.md#--test-name-pattern
25552557
[`--test-only`]: cli.md#--test-only
25562558
[`--test-reporter-destination`]: cli.md#--test-reporter-destination

doc/node.1

+4
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ Specify the minimum allocation from the OpenSSL secure heap. The default is 2. T
418418
.It Fl -test
419419
Starts the Node.js command line test runner.
420420
.
421+
.It Fl -test-concurrency
422+
The maximum number of test files that the test runner CLI will execute
423+
concurrently.
424+
.
421425
.It Fl -test-name-pattern
422426
A regular expression that configures the test runner to only execute tests
423427
whose name matches the provided pattern.

lib/internal/main/test_runner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const {
2222
prepareMainThreadExecution(false);
2323
markBootstrapComplete();
2424

25-
let concurrency = true;
25+
let concurrency = getOptionValue('--test-concurrency') || true;
2626
let inspectPort;
2727

2828
if (isUsingInspector()) {

src/node_options.cc

+3
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
595595
AddOption("--test",
596596
"launch test runner on startup",
597597
&EnvironmentOptions::test_runner);
598+
AddOption("--test-concurrency",
599+
"specify test runner concurrency",
600+
&EnvironmentOptions::test_runner_concurrency);
598601
AddOption("--experimental-test-coverage",
599602
"enable code coverage in the test runner",
600603
&EnvironmentOptions::test_runner_coverage);

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class EnvironmentOptions : public Options {
159159
std::string env_file;
160160
bool has_env_file_string = false;
161161
bool test_runner = false;
162+
uint64_t test_runner_concurrency = 0;
162163
bool test_runner_coverage = false;
163164
std::vector<std::string> test_name_pattern;
164165
std::vector<std::string> test_reporter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
const common = require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
const { deepStrictEqual, strictEqual } = require('node:assert');
5+
const { spawnSync } = require('node:child_process');
6+
const { readdirSync, writeFileSync } = require('node:fs');
7+
const { join } = require('node:path');
8+
const { beforeEach, test } = require('node:test');
9+
10+
function createTestFile(name) {
11+
writeFileSync(join(tmpdir.path, name), `
12+
const fs = require('node:fs');
13+
14+
fs.unlinkSync(__filename);
15+
setTimeout(() => {}, 1_000_000_000);
16+
`);
17+
}
18+
19+
beforeEach(() => {
20+
tmpdir.refresh();
21+
createTestFile('test-1.js');
22+
createTestFile('test-2.js');
23+
});
24+
25+
test('concurrency of one', () => {
26+
const cp = spawnSync(process.execPath, ['--test', '--test-concurrency=1'], {
27+
cwd: tmpdir.path,
28+
timeout: common.platformTimeout(1000),
29+
});
30+
31+
strictEqual(cp.stderr.toString(), '');
32+
strictEqual(cp.error.code, 'ETIMEDOUT');
33+
deepStrictEqual(readdirSync(tmpdir.path), ['test-2.js']);
34+
});
35+
36+
test('concurrency of two', () => {
37+
const cp = spawnSync(process.execPath, ['--test', '--test-concurrency=2'], {
38+
cwd: tmpdir.path,
39+
timeout: common.platformTimeout(1000),
40+
});
41+
42+
strictEqual(cp.stderr.toString(), '');
43+
strictEqual(cp.error.code, 'ETIMEDOUT');
44+
deepStrictEqual(readdirSync(tmpdir.path), []);
45+
});

0 commit comments

Comments
 (0)