Skip to content

Commit 709c6c0

Browse files
cjihrigtargos
authored andcommitted
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 <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
1 parent 4978bdc commit 709c6c0

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
@@ -1626,6 +1626,15 @@ Starts the Node.js command line test runner. This flag cannot be combined with
16261626
See the documentation on [running tests from the command line][]
16271627
for more details.
16281628

1629+
### `--test-concurrency`
1630+
1631+
<!-- YAML
1632+
added: REPLACEME
1633+
-->
1634+
1635+
The maximum number of test files that the test runner CLI will execute
1636+
concurrently. The default value is `os.availableParallelism() - 1`.
1637+
16291638
### `--test-name-pattern`
16301639

16311640
<!-- YAML

doc/api/test.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,12 @@ When searching for test files to execute, the test runner behaves as follows:
373373
automatically executed by the test runner, but are supported if explicitly
374374
provided on the command line.
375375

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

382383
Each test file is executed as if it was a regular script. That is, if the test
383384
file itself uses `node:test` to define tests, all of those tests will be
@@ -2562,6 +2563,7 @@ added:
25622563
[TTY]: tty.md
25632564
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
25642565
[`--import`]: cli.md#--importmodule
2566+
[`--test-concurrency`]: cli.md#--test-concurrency
25652567
[`--test-name-pattern`]: cli.md#--test-name-pattern
25662568
[`--test-only`]: cli.md#--test-only
25672569
[`--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)