Skip to content

Commit 99e738a

Browse files
rluvatonCeres6
authored andcommitted
test_runner: add shards support
PR-URL: nodejs#48639 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 8cb5efe commit 99e738a

File tree

19 files changed

+448
-4
lines changed

19 files changed

+448
-4
lines changed

doc/api/cli.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,27 @@ changes:
15001500
Configures the test runner to only execute top level tests that have the `only`
15011501
option set.
15021502

1503+
### `--test-shard`
1504+
1505+
<!-- YAML
1506+
added: REPLACEME
1507+
-->
1508+
1509+
Test suite shard to execute in a format of `<index>/<total>`, where
1510+
1511+
`index` is a positive integer, index of divided parts
1512+
`total` is a positive integer, total of divided part
1513+
This command will divide all tests files into `total` equal parts,
1514+
and will run only those that happen to be in an `index` part.
1515+
1516+
For example, to split your tests suite into three parts, use this:
1517+
1518+
```bash
1519+
node --test --test-shard=1/3
1520+
node --test --test-shard=2/3
1521+
node --test --test-shard=3/3
1522+
```
1523+
15031524
### `--throw-deprecation`
15041525

15051526
<!-- YAML
@@ -2177,6 +2198,7 @@ Node.js options that are allowed are:
21772198
* `--test-only`
21782199
* `--test-reporter-destination`
21792200
* `--test-reporter`
2201+
* `--test-shard`
21802202
* `--throw-deprecation`
21812203
* `--title`
21822204
* `--tls-cipher-list`

doc/api/test.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,11 @@ changes:
851851
If unspecified, subtests inherit this value from their parent.
852852
**Default:** `Infinity`.
853853
* `watch` {boolean} Whether to run in watch mode or not. **Default:** `false`.
854+
* `shard` {Object} Running tests in a specific shard. **Default:** `undefined`.
855+
* `index` {number} is a positive integer between 1 and `<total>`
856+
that specifies the index of the shard to run. This option is _required_.
857+
* `total` {number} is a positive integer that specifies the total number
858+
of shards to split the test files to. This option is _required_.
854859
* Returns: {TestsStream}
855860

856861
```mjs

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ The destination for the corresponding test reporter.
424424
Configures the test runner to only execute top level tests that have the `only`
425425
option set.
426426
.
427+
.It Fl -test-shard
428+
Test suite shard to execute in a format of <index>/<total>.
429+
.
427430
.It Fl -throw-deprecation
428431
Throw errors for deprecations.
429432
.

lib/internal/main/test_runner.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ const { isUsingInspector } = require('internal/util/inspector');
88
const { run } = require('internal/test_runner/runner');
99
const { setupTestReporters } = require('internal/test_runner/utils');
1010
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
11+
const {
12+
codes: {
13+
ERR_INVALID_ARG_VALUE,
14+
},
15+
} = require('internal/errors');
16+
const {
17+
NumberParseInt,
18+
RegExpPrototypeExec,
19+
StringPrototypeSplit,
20+
} = primordials;
1121

1222
prepareMainThreadExecution(false);
1323
markBootstrapComplete();
@@ -22,7 +32,32 @@ if (isUsingInspector()) {
2232
inspectPort = process.debugPort;
2333
}
2434

25-
run({ concurrency, inspectPort, watch: getOptionValue('--watch'), setup: setupTestReporters })
35+
let shard;
36+
const shardOption = getOptionValue('--test-shard');
37+
if (shardOption) {
38+
if (!RegExpPrototypeExec(/^\d+\/\d+$/, shardOption)) {
39+
process.exitCode = kGenericUserError;
40+
41+
throw new ERR_INVALID_ARG_VALUE(
42+
'--test-shard',
43+
shardOption,
44+
'must be in the form of <index>/<total>',
45+
);
46+
}
47+
48+
const { 0: indexStr, 1: totalStr } = StringPrototypeSplit(shardOption, '/');
49+
50+
const index = NumberParseInt(indexStr, 10);
51+
const total = NumberParseInt(totalStr, 10);
52+
53+
shard = {
54+
__proto__: null,
55+
index,
56+
total,
57+
};
58+
}
59+
60+
run({ concurrency, inspectPort, watch: getOptionValue('--watch'), setup: setupTestReporters, shard })
2661
.once('test:fail', () => {
2762
process.exitCode = kGenericUserError;
2863
});

lib/internal/test_runner/runner.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ const console = require('internal/console/global');
3939
const {
4040
codes: {
4141
ERR_INVALID_ARG_TYPE,
42+
ERR_INVALID_ARG_VALUE,
4243
ERR_TEST_FAILURE,
44+
ERR_OUT_OF_RANGE,
4345
},
4446
} = require('internal/errors');
45-
const { validateArray, validateBoolean, validateFunction } = require('internal/validators');
47+
const {
48+
validateArray,
49+
validateBoolean,
50+
validateFunction,
51+
validateObject,
52+
validateInteger,
53+
} = require('internal/validators');
4654
const { getInspectPort, isUsingInspector, isInspectorMessage } = require('internal/util/inspector');
4755
const { isRegExp } = require('internal/util/types');
4856
const { kEmptyObject } = require('internal/util');
@@ -416,7 +424,7 @@ function run(options) {
416424
if (options === null || typeof options !== 'object') {
417425
options = kEmptyObject;
418426
}
419-
let { testNamePatterns } = options;
427+
let { testNamePatterns, shard } = options;
420428
const { concurrency, timeout, signal, files, inspectPort, watch, setup } = options;
421429

422430
if (files != null) {
@@ -425,6 +433,22 @@ function run(options) {
425433
if (watch != null) {
426434
validateBoolean(watch, 'options.watch');
427435
}
436+
if (shard != null) {
437+
validateObject(shard, 'options.shard');
438+
// Avoid re-evaluating the shard object in case it's a getter
439+
shard = { __proto__: null, index: shard.index, total: shard.total };
440+
441+
validateInteger(shard.total, 'options.shard.total', 1);
442+
validateInteger(shard.index, 'options.shard.index');
443+
444+
if (shard.index <= 0 || shard.total < shard.index) {
445+
throw new ERR_OUT_OF_RANGE('options.shard.index', `>= 1 && <= ${shard.total} ("options.shard.total")`, shard.index);
446+
}
447+
448+
if (watch) {
449+
throw new ERR_INVALID_ARG_VALUE('options.shard', watch, 'shards not supported with watch mode');
450+
}
451+
}
428452
if (setup != null) {
429453
validateFunction(setup, 'options.setup');
430454
}
@@ -446,7 +470,11 @@ function run(options) {
446470
}
447471

448472
const root = createTestTree({ concurrency, timeout, signal });
449-
const testFiles = files ?? createTestFileList();
473+
let testFiles = files ?? createTestFileList();
474+
475+
if (shard) {
476+
testFiles = ArrayPrototypeFilter(testFiles, (_, index) => index % shard.total === shard.index - 1);
477+
}
450478

451479
let postRun = () => root.postRun();
452480
let filesWatcher;

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
596596
"run tests with 'only' option set",
597597
&EnvironmentOptions::test_only,
598598
kAllowedInEnvvar);
599+
AddOption("--test-shard",
600+
"run test at specific shard",
601+
&EnvironmentOptions::test_shard,
602+
kAllowedInEnvvar);
599603
AddOption("--test-udp-no-try-send", "", // For testing only.
600604
&EnvironmentOptions::test_udp_no_try_send);
601605
AddOption("--throw-deprecation",

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class EnvironmentOptions : public Options {
165165
std::vector<std::string> test_reporter_destination;
166166
bool test_only = false;
167167
bool test_udp_no_try_send = false;
168+
std::string test_shard;
168169
bool throw_deprecation = false;
169170
bool trace_atomics_wait = false;
170171
bool trace_deprecation = false;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const test = require('node:test');
3+
4+
test('a.cjs this should pass');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const test = require('node:test');
3+
4+
test('b.cjs this should pass');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const test = require('node:test');
3+
4+
test('c.cjs this should pass');

0 commit comments

Comments
 (0)