Skip to content

Commit 51a0310

Browse files
aduh95ruyadorno
authored andcommitted
test_runner: validate concurrency option
PR-URL: #43976 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 2cab7bb commit 51a0310

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

lib/internal/test_runner/test.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const { once } = require('events');
1919
const { AbortController } = require('internal/abort_controller');
2020
const {
2121
codes: {
22+
ERR_INVALID_ARG_TYPE,
2223
ERR_TEST_FAILURE,
2324
},
2425
kIsNodeError,
@@ -33,9 +34,9 @@ const {
3334
} = require('internal/util');
3435
const { isPromise } = require('internal/util/types');
3536
const {
36-
isUint32,
3737
validateAbortSignal,
3838
validateNumber,
39+
validateUint32,
3940
} = require('internal/validators');
4041
const { setTimeout } = require('timers/promises');
4142
const { TIMEOUT_MAX } = require('internal/timers');
@@ -149,14 +150,23 @@ class Test extends AsyncResource {
149150
this.timeout = parent.timeout;
150151
}
151152

152-
if (isUint32(concurrency) && concurrency !== 0) {
153-
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-
}
153+
switch (typeof concurrency) {
154+
case 'number':
155+
validateUint32(concurrency, 'options.concurrency', 1);
156+
this.concurrency = concurrency;
157+
break;
158+
159+
case 'boolean':
160+
if (concurrency) {
161+
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
162+
} else {
163+
this.concurrency = 1;
164+
}
165+
break;
166+
167+
default:
168+
if (concurrency != null)
169+
throw new ERR_INVALID_ARG_TYPE('options.concurrency', ['boolean', 'number'], concurrency);
160170
}
161171

162172
if (timeout != null && timeout !== Infinity) {

test/parallel/test-runner-option-validation.js

+11
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ const test = require('node:test');
1313
// Valid values should not throw.
1414
test({ timeout });
1515
});
16+
17+
[Symbol(), {}, [], () => {}, 1n, '1'].forEach((concurrency) => {
18+
assert.throws(() => test({ concurrency }), { code: 'ERR_INVALID_ARG_TYPE' });
19+
});
20+
[-1, 0, 1.1, -Infinity, NaN, 2 ** 33, Number.MAX_SAFE_INTEGER].forEach((concurrency) => {
21+
assert.throws(() => test({ concurrency }), { code: 'ERR_OUT_OF_RANGE' });
22+
});
23+
[null, undefined, 1, 2 ** 31, true, false].forEach((concurrency) => {
24+
// Valid values should not throw.
25+
test({ concurrency });
26+
});

0 commit comments

Comments
 (0)