Skip to content

Commit cd0d9dd

Browse files
98lenvidanielleadams
authored andcommitted
test_runner: add support for boolean values for concurrency option
PR-URL: #43887 Fixes: #43837 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent f980201 commit cd0d9dd

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

doc/api/test.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,14 @@ changes:
331331
does not have a name.
332332
* `options` {Object} Configuration options for the test. The following
333333
properties are supported:
334-
* `concurrency` {number} The number of tests that can be run at the same time.
334+
* `concurrency` {number|boolean} If a number is provided,
335+
then that many tests would run in parallel.
336+
If truthy, it would run (number of cpu cores - 1)
337+
tests in parallel.
338+
For subtests, it will be `Infinity` tests in parallel.
339+
If falsy, it would only run one test at a time.
335340
If unspecified, subtests inherit this value from their parent.
336-
**Default:** `1`.
341+
**Default:** `false`.
337342
* `only` {boolean} If truthy, and the test context is configured to run
338343
`only` tests, then this test will be run. Otherwise, the test is skipped.
339344
**Default:** `false`.

lib/internal/test_runner/test.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
ArrayPrototypeShift,
55
ArrayPrototypeUnshift,
66
FunctionPrototype,
7+
MathMax,
78
Number,
89
PromisePrototypeThen,
910
PromiseResolve,
@@ -52,8 +53,7 @@ const noop = FunctionPrototype;
5253
const isTestRunner = getOptionValue('--test');
5354
const testOnlyFlag = !isTestRunner && getOptionValue('--test-only');
5455
// TODO(cjihrig): Use uv_available_parallelism() once it lands.
55-
const rootConcurrency = isTestRunner ? cpus().length : 1;
56-
56+
const rootConcurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : 1;
5757
const kShouldAbort = Symbol('kShouldAbort');
5858

5959

@@ -151,6 +151,12 @@ class Test extends AsyncResource {
151151

152152
if (isUint32(concurrency) && concurrency !== 0) {
153153
this.concurrency = concurrency;
154+
} else if (typeof concurrency === 'boolean') {
155+
if (concurrency) {
156+
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
157+
} else {
158+
this.concurrency = 1;
159+
}
154160
}
155161

156162
if (timeout != null && timeout !== Infinity) {
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
require('../common');
3+
const { describe, it } = require('node:test');
4+
const assert = require('assert');
5+
6+
describe('Concurrency option (boolean) = true ', { concurrency: true }, () => {
7+
let isFirstTestOver = false;
8+
it('should start the first test', () => new Promise((resolve) => {
9+
setImmediate(() => { isFirstTestOver = true; resolve(); });
10+
}));
11+
it('should start before the previous test ends', () => {
12+
// Should work even on single core CPUs
13+
assert.strictEqual(isFirstTestOver, false);
14+
});
15+
});
16+
17+
describe(
18+
'Concurrency option (boolean) = false ',
19+
{ concurrency: false },
20+
() => {
21+
let isFirstTestOver = false;
22+
it('should start the first test', () => new Promise((resolve) => {
23+
setImmediate(() => { isFirstTestOver = true; resolve(); });
24+
}));
25+
it('should start after the previous test ends', () => {
26+
assert.strictEqual(isFirstTestOver, true);
27+
});
28+
}
29+
);

0 commit comments

Comments
 (0)