Skip to content

Commit

Permalink
accept non-UInt32 values as valid timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jul 16, 2022
1 parent 44debf0 commit 5ef6f6d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const {
kEmptyObject,
} = require('internal/util');
const { isPromise } = require('internal/util/types');
const { isUint32, validateUint32 } = require('internal/validators');
const { isUint32, validateNumber } = require('internal/validators');
const { setTimeout } = require('timers/promises');
const { TIMEOUT_MAX } = require('internal/timers');
const { cpus } = require('os');
const { bigint: hrtime } = process.hrtime;
const kCallbackAndPromisePresent = 'callbackAndPromisePresent';
Expand Down Expand Up @@ -139,7 +140,7 @@ class Test extends AsyncResource {
}

if (timeout != null && timeout !== Infinity) {
validateUint32(timeout, 'options.timeout');
validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX);
this.timeout = timeout;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
NumberIsInteger,
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
NumberParseInt,
Expand Down Expand Up @@ -115,9 +116,14 @@ function validateString(value, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
}

function validateNumber(value, name) {
function validateNumber(value, name, min = undefined, max) {
if (typeof value !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);

if ((min != null && value < min) || (max != null && value > max) ||
((min != null || max != null) && NumberIsNaN(value))) {
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
}
}

const validateOneOf = hideStackFrames((value, name, oneOf) => {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-runner-option-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const test = require('node:test');
[Symbol(), {}, [], () => {}, 1n, true, '1'].forEach((timeout) => {
assert.throws(() => test({ timeout }), { code: 'ERR_INVALID_ARG_TYPE' });
});
[-1, 2 ** 33, 1.1, -Infinity, NaN].forEach((timeout) => {
[-1, 2 ** 33, -Infinity, NaN].forEach((timeout) => {
assert.throws(() => test({ timeout }), { code: 'ERR_OUT_OF_RANGE' });
});
[null, undefined, Infinity, 0, 1].forEach((timeout) => {
[null, undefined, Infinity, 0, 1, 1.1].forEach((timeout) => {
// Valid values should not throw.
test({ timeout });
});

0 comments on commit 5ef6f6d

Please sign in to comment.