Skip to content

Commit cf78656

Browse files
committed
feat: validate timeout option
PR-URL: nodejs/node#43843 Reviewed-By: Feng Yu <[email protected]> (cherry picked from commit d83446b4c4694322e12d2b7d22592f2be674e580)
1 parent 3814bf0 commit cf78656

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

Diff for: lib/internal/test_runner/test.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/2e682f10b6104373fded64b0e364984b85ae2243/lib/internal/test_runner/test.js
1+
// https://github.com/nodejs/node/blob/d83446b4c4694322e12d2b7d22592f2be674e580/lib/internal/test_runner/test.js
22

33
'use strict'
44

@@ -34,8 +34,13 @@ const {
3434
kEmptyObject
3535
} = require('#internal/util')
3636
const { isPromise } = require('#internal/util/types')
37-
const { isUint32, validateAbortSignal } = require('#internal/validators')
37+
const {
38+
isUint32,
39+
validateAbortSignal,
40+
validateNumber
41+
} = require('#internal/validators')
3842
const { setTimeout } = require('#timers/promises')
43+
const { TIMEOUT_MAX } = require('#internal/timers')
3944
const { cpus } = require('os')
4045
const { bigint: hrtime } = process.hrtime
4146
const kCallbackAndPromisePresent = 'callbackAndPromisePresent'
@@ -151,7 +156,8 @@ class Test extends AsyncResource {
151156
this.concurrency = concurrency
152157
}
153158

154-
if (isUint32(timeout)) {
159+
if (timeout != null && timeout !== Infinity) {
160+
validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX)
155161
this.timeout = timeout
156162
}
157163

Diff for: lib/internal/timers.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const TIMEOUT_MAX = 2 ** 31 - 1
4+
5+
module.exports = {
6+
TIMEOUT_MAX
7+
}

Diff for: lib/internal/validators.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
// https://github.com/nodejs/node/blob/1aab13cad9c800f4121c1d35b554b78c1b17bdbd/lib/internal/validators.js
1+
// https://github.com/nodejs/node/blob/d83446b4c4694322e12d2b7d22592f2be674e580/lib/internal/validators.js
22
function isUint32 (value) {
33
return value === (value >>> 0)
44
}
55

6+
function validateNumber (value, name, min = undefined, max) {
7+
if (typeof value !== 'number') { throw new TypeError(`Expected ${name} to be a number, got ${value}`) }
8+
9+
if ((min != null && value < min) || (max != null && value > max) ||
10+
((min != null || max != null) && Number.isNaN(value))) {
11+
throw new RangeError(`Expected ${name} to be ${
12+
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`
13+
}, got ${value}`)
14+
}
15+
}
16+
617
const validateAbortSignal = (signal, name) => {
718
if (signal !== undefined &&
819
(signal === null ||
@@ -14,5 +25,6 @@ const validateAbortSignal = (signal, name) => {
1425

1526
module.exports = {
1627
isUint32,
17-
validateAbortSignal
28+
validateAbortSignal,
29+
validateNumber
1830
}

Diff for: test/parallel/test-runner-option-validation.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://github.com/nodejs/node/blob/d83446b4c4694322e12d2b7d22592f2be674e580/test/parallel/test-runner-option-validation.js
2+
3+
'use strict'
4+
require('../common')
5+
const assert = require('assert')
6+
const test = require('node:test');
7+
8+
// eslint-disable-next-line symbol-description
9+
[Symbol(), {}, [], () => {}, 1n, true, '1'].forEach((timeout) => {
10+
assert.throws(() => test({ timeout }), { code: 'ERR_INVALID_ARG_TYPE' })
11+
});
12+
[-1, -Infinity, NaN, 2 ** 33, Number.MAX_SAFE_INTEGER].forEach((timeout) => {
13+
assert.throws(() => test({ timeout }), { code: 'ERR_OUT_OF_RANGE' })
14+
});
15+
[null, undefined, Infinity, 0, 1, 1.1].forEach((timeout) => {
16+
// Valid values should not throw.
17+
test({ timeout })
18+
})

0 commit comments

Comments
 (0)