Skip to content

Commit

Permalink
test_runner: validate concurrency option
Browse files Browse the repository at this point in the history
PR-URL: #43976
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
aduh95 committed Jul 26, 2022
1 parent 09c8df0 commit 60da0a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { once } = require('events');
const { AbortController } = require('internal/abort_controller');
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_TEST_FAILURE,
},
kIsNodeError,
Expand All @@ -33,9 +34,9 @@ const {
} = require('internal/util');
const { isPromise } = require('internal/util/types');
const {
isUint32,
validateAbortSignal,
validateNumber,
validateUint32,
} = require('internal/validators');
const { setTimeout } = require('timers/promises');
const { TIMEOUT_MAX } = require('internal/timers');
Expand Down Expand Up @@ -149,14 +150,23 @@ class Test extends AsyncResource {
this.timeout = parent.timeout;
}

if (isUint32(concurrency) && concurrency !== 0) {
this.concurrency = concurrency;
} else if (typeof concurrency === 'boolean') {
if (concurrency) {
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
} else {
this.concurrency = 1;
}
switch (typeof concurrency) {
case 'number':
validateUint32(concurrency, 'options.concurrency', 1);
this.concurrency = concurrency;
break;

case 'boolean':
if (concurrency) {
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
} else {
this.concurrency = 1;
}
break;

default:
if (concurrency != null)
throw new ERR_INVALID_ARG_TYPE('options.concurrency', ['boolean', 'number'], concurrency);
}

if (timeout != null && timeout !== Infinity) {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-runner-option-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ const test = require('node:test');
// Valid values should not throw.
test({ timeout });
});

[Symbol(), {}, [], () => {}, 1n, '1'].forEach((concurrency) => {
assert.throws(() => test({ concurrency }), { code: 'ERR_INVALID_ARG_TYPE' });
});
[-1, 0, 1.1, -Infinity, NaN, 2 ** 33, Number.MAX_SAFE_INTEGER].forEach((concurrency) => {
assert.throws(() => test({ concurrency }), { code: 'ERR_OUT_OF_RANGE' });
});
[null, undefined, 1, 2 ** 31, true, false].forEach((concurrency) => {
// Valid values should not throw.
test({ concurrency });
});

0 comments on commit 60da0a1

Please sign in to comment.