Skip to content

Commit 5e2af43

Browse files
committed
test_runner: add shards support
PR-URL: #48639 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 779043d commit 5e2af43

File tree

19 files changed

+455
-5
lines changed

19 files changed

+455
-5
lines changed

doc/api/cli.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,27 @@ added: v18.0.0
12891289
Configures the test runner to only execute top level tests that have the `only`
12901290
option set.
12911291

1292+
### `--test-shard`
1293+
1294+
<!-- YAML
1295+
added: REPLACEME
1296+
-->
1297+
1298+
Test suite shard to execute in a format of `<index>/<total>`, where
1299+
1300+
`index` is a positive integer, index of divided parts
1301+
`total` is a positive integer, total of divided part
1302+
This command will divide all tests files into `total` equal parts,
1303+
and will run only those that happen to be in an `index` part.
1304+
1305+
For example, to split your tests suite into three parts, use this:
1306+
1307+
```bash
1308+
node --test --test-shard=1/3
1309+
node --test --test-shard=2/3
1310+
node --test --test-shard=3/3
1311+
```
1312+
12921313
### `--throw-deprecation`
12931314

12941315
<!-- YAML
@@ -1952,6 +1973,7 @@ Node.js options that are allowed are:
19521973
* `--test-only`
19531974
* `--test-reporter-destination`
19541975
* `--test-reporter`
1976+
* `--test-shard`
19551977
* `--throw-deprecation`
19561978
* `--title`
19571979
* `--tls-cipher-list`

doc/api/test.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,11 @@ changes:
747747
If unspecified, subtests inherit this value from their parent.
748748
**Default:** `Infinity`.
749749
* `watch` {boolean} Whether to run in watch mode or not. **Default:** `false`.
750+
* `shard` {Object} Running tests in a specific shard. **Default:** `undefined`.
751+
* `index` {number} is a positive integer between 1 and `<total>`
752+
that specifies the index of the shard to run. This option is _required_.
753+
* `total` {number} is a positive integer that specifies the total number
754+
of shards to split the test files to. This option is _required_.
750755
* Returns: {TestsStream}
751756

752757
```mjs

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ The destination for the corresponding test reporter.
409409
Configures the test runner to only execute top level tests that have the `only`
410410
option set.
411411
.
412+
.It Fl -test-shard
413+
Test suite shard to execute in a format of <index>/<total>.
414+
.
412415
.It Fl -throw-deprecation
413416
Throw errors for deprecations.
414417
.

lib/internal/main/test_runner.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ const { getOptionValue } = require('internal/options');
77
const { isUsingInspector } = require('internal/util/inspector');
88
const { run } = require('internal/test_runner/runner');
99
const { setupTestReporters } = require('internal/test_runner/utils');
10+
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;
1021

1122
prepareMainThreadExecution(false);
1223
markBootstrapComplete();
@@ -21,7 +32,32 @@ if (isUsingInspector()) {
2132
inspectPort = process.debugPort;
2233
}
2334

24-
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 })
2561
.once('test:fail', () => {
26-
process.exitCode = 1;
62+
process.exitCode = kGenericUserError;
2763
});

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');
@@ -450,7 +458,7 @@ function run(options) {
450458
if (options === null || typeof options !== 'object') {
451459
options = kEmptyObject;
452460
}
453-
let { testNamePatterns } = options;
461+
let { testNamePatterns, shard } = options;
454462
const { concurrency, timeout, signal, files, inspectPort, watch, setup } = options;
455463

456464
if (files != null) {
@@ -459,6 +467,22 @@ function run(options) {
459467
if (watch != null) {
460468
validateBoolean(watch, 'options.watch');
461469
}
470+
if (shard != null) {
471+
validateObject(shard, 'options.shard');
472+
// Avoid re-evaluating the shard object in case it's a getter
473+
shard = { __proto__: null, index: shard.index, total: shard.total };
474+
475+
validateInteger(shard.total, 'options.shard.total', 1);
476+
validateInteger(shard.index, 'options.shard.index');
477+
478+
if (shard.index <= 0 || shard.total < shard.index) {
479+
throw new ERR_OUT_OF_RANGE('options.shard.index', `>= 1 && <= ${shard.total} ("options.shard.total")`, shard.index);
480+
}
481+
482+
if (watch) {
483+
throw new ERR_INVALID_ARG_VALUE('options.shard', watch, 'shards not supported with watch mode');
484+
}
485+
}
462486
if (setup != null) {
463487
validateFunction(setup, 'options.setup');
464488
}
@@ -480,7 +504,11 @@ function run(options) {
480504
}
481505

482506
const root = createTestTree({ concurrency, timeout, signal });
483-
const testFiles = files ?? createTestFileList();
507+
let testFiles = files ?? createTestFileList();
508+
509+
if (shard) {
510+
testFiles = ArrayPrototypeFilter(testFiles, (_, index) => index % shard.total === shard.index - 1);
511+
}
484512

485513
let postRun = () => root.postRun();
486514
let filesWatcher;

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
581581
"run tests with 'only' option set",
582582
&EnvironmentOptions::test_only,
583583
kAllowedInEnvvar);
584+
AddOption("--test-shard",
585+
"run test at specific shard",
586+
&EnvironmentOptions::test_shard,
587+
kAllowedInEnvvar);
584588
AddOption("--test-udp-no-try-send", "", // For testing only.
585589
&EnvironmentOptions::test_udp_no_try_send);
586590
AddOption("--throw-deprecation",

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class EnvironmentOptions : public Options {
161161
std::vector<std::string> test_reporter_destination;
162162
bool test_only = false;
163163
bool test_udp_no_try_send = false;
164+
std::string test_shard;
164165
bool throw_deprecation = false;
165166
bool trace_atomics_wait = false;
166167
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');
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('d.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('e.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('f.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('g.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('h.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('i.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('j.cjs this should pass');

test/parallel/test-runner-cli.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require('../common');
44
const assert = require('assert');
55
const { spawnSync } = require('child_process');
66
const { join } = require('path');
7+
const { readdirSync } = require('fs');
78
const fixtures = require('../common/fixtures');
89
const testFixtures = fixtures.path('test-runner');
910

@@ -210,3 +211,135 @@ const testFixtures = fixtures.path('test-runner');
210211
const stdout = child.stdout.toString();
211212
assert.match(stdout, /ok 1 - this should pass/);
212213
}
214+
215+
{
216+
// --test-shard option validation
217+
const args = ['--test', '--test-shard=1', join(testFixtures, 'index.js')];
218+
const child = spawnSync(process.execPath, args, { cwd: testFixtures });
219+
220+
assert.strictEqual(child.status, 1);
221+
assert.strictEqual(child.signal, null);
222+
assert.match(child.stderr.toString(), /The argument '--test-shard' must be in the form of <index>\/<total>\. Received '1'/);
223+
const stdout = child.stdout.toString();
224+
assert.strictEqual(stdout, '');
225+
}
226+
227+
{
228+
// --test-shard option validation
229+
const args = ['--test', '--test-shard=1/2/3', join(testFixtures, 'index.js')];
230+
const child = spawnSync(process.execPath, args, { cwd: testFixtures });
231+
232+
assert.strictEqual(child.status, 1);
233+
assert.strictEqual(child.signal, null);
234+
assert.match(child.stderr.toString(), /The argument '--test-shard' must be in the form of <index>\/<total>\. Received '1\/2\/3'/);
235+
const stdout = child.stdout.toString();
236+
assert.strictEqual(stdout, '');
237+
}
238+
239+
{
240+
// --test-shard option validation
241+
const args = ['--test', '--test-shard=0/3', join(testFixtures, 'index.js')];
242+
const child = spawnSync(process.execPath, args, { cwd: testFixtures });
243+
244+
assert.strictEqual(child.status, 1);
245+
assert.strictEqual(child.signal, null);
246+
assert.match(child.stderr.toString(), /The value of "options\.shard\.index" is out of range\. It must be >= 1 && <= 3 \("options\.shard\.total"\)\. Received 0/);
247+
const stdout = child.stdout.toString();
248+
assert.strictEqual(stdout, '');
249+
}
250+
251+
{
252+
// --test-shard option validation
253+
const args = ['--test', '--test-shard=0xf/20abcd', join(testFixtures, 'index.js')];
254+
const child = spawnSync(process.execPath, args, { cwd: testFixtures });
255+
256+
assert.strictEqual(child.status, 1);
257+
assert.strictEqual(child.signal, null);
258+
assert.match(child.stderr.toString(), /The argument '--test-shard' must be in the form of <index>\/<total>\. Received '0xf\/20abcd'/);
259+
const stdout = child.stdout.toString();
260+
assert.strictEqual(stdout, '');
261+
}
262+
263+
{
264+
// --test-shard option validation
265+
const args = ['--test', '--test-shard=hello', join(testFixtures, 'index.js')];
266+
const child = spawnSync(process.execPath, args, { cwd: testFixtures });
267+
268+
assert.strictEqual(child.status, 1);
269+
assert.strictEqual(child.signal, null);
270+
assert.match(child.stderr.toString(), /The argument '--test-shard' must be in the form of <index>\/<total>\. Received 'hello'/);
271+
const stdout = child.stdout.toString();
272+
assert.strictEqual(stdout, '');
273+
}
274+
275+
{
276+
// --test-shard option, first shard
277+
const shardsTestPath = join(testFixtures, 'shards');
278+
const allShardsTestsFiles = readdirSync(shardsTestPath).map((file) => join(shardsTestPath, file));
279+
const args = [
280+
'--test',
281+
'--test-shard=1/2',
282+
...allShardsTestsFiles,
283+
];
284+
const child = spawnSync(process.execPath, args);
285+
286+
assert.strictEqual(child.status, 0);
287+
assert.strictEqual(child.signal, null);
288+
assert.strictEqual(child.stderr.toString(), '');
289+
const stdout = child.stdout.toString();
290+
assert.match(stdout, /# Subtest: a\.cjs this should pass/);
291+
assert.match(stdout, /ok 1 - a\.cjs this should pass/);
292+
293+
assert.match(stdout, /# Subtest: c\.cjs this should pass/);
294+
assert.match(stdout, /ok 2 - c\.cjs this should pass/);
295+
296+
assert.match(stdout, /# Subtest: e\.cjs this should pass/);
297+
assert.match(stdout, /ok 3 - e\.cjs this should pass/);
298+
299+
assert.match(stdout, /# Subtest: g\.cjs this should pass/);
300+
assert.match(stdout, /ok 4 - g\.cjs this should pass/);
301+
302+
assert.match(stdout, /# Subtest: i\.cjs this should pass/);
303+
assert.match(stdout, /ok 5 - i\.cjs this should pass/);
304+
305+
assert.match(stdout, /# tests 5/);
306+
assert.match(stdout, /# pass 5/);
307+
assert.match(stdout, /# fail 0/);
308+
assert.match(stdout, /# skipped 0/);
309+
}
310+
311+
{
312+
// --test-shard option, last shard
313+
const shardsTestPath = join(testFixtures, 'shards');
314+
const allShardsTestsFiles = readdirSync(shardsTestPath).map((file) => join(shardsTestPath, file));
315+
const args = [
316+
'--test',
317+
'--test-shard=2/2',
318+
...allShardsTestsFiles,
319+
];
320+
const child = spawnSync(process.execPath, args);
321+
322+
assert.strictEqual(child.status, 0);
323+
assert.strictEqual(child.signal, null);
324+
assert.strictEqual(child.stderr.toString(), '');
325+
const stdout = child.stdout.toString();
326+
assert.match(stdout, /# Subtest: b\.cjs this should pass/);
327+
assert.match(stdout, /ok 1 - b\.cjs this should pass/);
328+
329+
assert.match(stdout, /# Subtest: d\.cjs this should pass/);
330+
assert.match(stdout, /ok 2 - d\.cjs this should pass/);
331+
332+
assert.match(stdout, /# Subtest: f\.cjs this should pass/);
333+
assert.match(stdout, /ok 3 - f\.cjs this should pass/);
334+
335+
assert.match(stdout, /# Subtest: h\.cjs this should pass/);
336+
assert.match(stdout, /ok 4 - h\.cjs this should pass/);
337+
338+
assert.match(stdout, /# Subtest: j\.cjs this should pass/);
339+
assert.match(stdout, /ok 5 - j\.cjs this should pass/);
340+
341+
assert.match(stdout, /# tests 5/);
342+
assert.match(stdout, /# pass 5/);
343+
assert.match(stdout, /# fail 0/);
344+
assert.match(stdout, /# skipped 0/);
345+
}

0 commit comments

Comments
 (0)