Skip to content

Commit 0ef73ff

Browse files
rluvatonjuanarbol
authored andcommitted
test_runner: add shards support
PR-URL: #48639 Backport-PR-URL: #48797 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 07bfcc4 commit 0ef73ff

File tree

19 files changed

+453
-4
lines changed

19 files changed

+453
-4
lines changed

doc/api/cli.md

+22
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

+5
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,11 @@ changes:
874874
If unspecified, subtests inherit this value from their parent.
875875
**Default:** `Infinity`.
876876
* `watch` {boolean} Whether to run in watch mode or not. **Default:** `false`.
877+
* `shard` {Object} Running tests in a specific shard. **Default:** `undefined`.
878+
* `index` {number} is a positive integer between 1 and `<total>`
879+
that specifies the index of the shard to run. This option is _required_.
880+
* `total` {number} is a positive integer that specifies the total number
881+
of shards to split the test files to. This option is _required_.
877882
* Returns: {TestsStream}
878883

879884
```mjs

doc/node.1

+3
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

+36-1
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

+31-3
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');
@@ -460,7 +468,7 @@ function run(options) {
460468
if (options === null || typeof options !== 'object') {
461469
options = kEmptyObject;
462470
}
463-
let { testNamePatterns } = options;
471+
let { testNamePatterns, shard } = options;
464472
const { concurrency, timeout, signal, files, inspectPort, watch, setup } = options;
465473

466474
if (files != null) {
@@ -469,6 +477,22 @@ function run(options) {
469477
if (watch != null) {
470478
validateBoolean(watch, 'options.watch');
471479
}
480+
if (shard != null) {
481+
validateObject(shard, 'options.shard');
482+
// Avoid re-evaluating the shard object in case it's a getter
483+
shard = { __proto__: null, index: shard.index, total: shard.total };
484+
485+
validateInteger(shard.total, 'options.shard.total', 1);
486+
validateInteger(shard.index, 'options.shard.index');
487+
488+
if (shard.index <= 0 || shard.total < shard.index) {
489+
throw new ERR_OUT_OF_RANGE('options.shard.index', `>= 1 && <= ${shard.total} ("options.shard.total")`, shard.index);
490+
}
491+
492+
if (watch) {
493+
throw new ERR_INVALID_ARG_VALUE('options.shard', watch, 'shards not supported with watch mode');
494+
}
495+
}
472496
if (setup != null) {
473497
validateFunction(setup, 'options.setup');
474498
}
@@ -490,7 +514,11 @@ function run(options) {
490514
}
491515

492516
const root = createTestTree({ concurrency, timeout, signal });
493-
const testFiles = files ?? createTestFileList();
517+
let testFiles = files ?? createTestFileList();
518+
519+
if (shard) {
520+
testFiles = ArrayPrototypeFilter(testFiles, (_, index) => index % shard.total === shard.index - 1);
521+
}
494522

495523
let postRun = () => root.postRun();
496524
let filesWatcher;

src/node_options.cc

+4
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

+1
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;
+4
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');
+4
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');
+4
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');
+4
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');
+4
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');
+4
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');
+4
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');
+4
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');
+4
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');
+4
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

+133
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)