Skip to content

Commit 428301a

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 <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 54168b7 commit 428301a

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

1301+
### `--test-concurrency`
1302+
1303+
<!-- YAML
1304+
added: REPLACEME
1305+
-->
1306+
1307+
The maximum number of test files that the test runner CLI will execute
1308+
concurrently. The default value is `os.availableParallelism() - 1`.
1309+
13011310
### `--test-name-pattern`
13021311

13031312
<!-- YAML

doc/api/test.md

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

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

376377
Each test file is executed as if it was a regular script. That is, if the test
377378
file itself uses `node:test` to define tests, all of those tests will be
@@ -2449,6 +2450,7 @@ added: v18.7.0
24492450
[TAP]: https://testanything.org/
24502451
[TTY]: tty.md
24512452
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
2453+
[`--test-concurrency`]: cli.md#--test-concurrency
24522454
[`--test-name-pattern`]: cli.md#--test-name-pattern
24532455
[`--test-only`]: cli.md#--test-only
24542456
[`--test-reporter-destination`]: cli.md#--test-reporter-destination

doc/node.1

+4
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ Specify the minimum allocation from the OpenSSL secure heap. The default is 2. T
400400
.It Fl -test
401401
Starts the Node.js command line test runner.
402402
.
403+
.It Fl -test-concurrency
404+
The maximum number of test files that the test runner CLI will execute
405+
concurrently.
406+
.
403407
.It Fl -test-name-pattern
404408
A regular expression that configures the test runner to only execute tests
405409
whose name matches the provided pattern.

lib/internal/main/test_runner.js

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

24-
let concurrency = true;
24+
let concurrency = getOptionValue('--test-concurrency') || true;
2525
let inspectPort;
2626

2727
if (isUsingInspector()) {

src/node_options.cc

+3
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
570570
AddOption("--test",
571571
"launch test runner on startup",
572572
&EnvironmentOptions::test_runner);
573+
AddOption("--test-concurrency",
574+
"specify test runner concurrency",
575+
&EnvironmentOptions::test_runner_concurrency);
573576
AddOption("--experimental-test-coverage",
574577
"enable code coverage in the test runner",
575578
&EnvironmentOptions::test_runner_coverage);

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class EnvironmentOptions : public Options {
156156
std::string redirect_warnings;
157157
std::string diagnostic_dir;
158158
bool test_runner = false;
159+
uint64_t test_runner_concurrency = 0;
159160
bool test_runner_coverage = false;
160161
std::vector<std::string> test_name_pattern;
161162
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)