Skip to content

Commit dd848f2

Browse files
badkeyyaduh95
authored andcommittedOct 19, 2024
lib: test_runner#mock:timers respeced timeout_max behaviour
PR-URL: #55375 Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Claudio Wunder <[email protected]>
1 parent 1ce8928 commit dd848f2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎lib/internal/test_runner/mock/mock_timers.js

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const {
3838
},
3939
} = require('internal/errors');
4040

41+
const { TIMEOUT_MAX } = require('internal/timers');
42+
4143
const PriorityQueue = require('internal/priority_queue');
4244
const nodeTimers = require('timers');
4345
const nodeTimersPromises = require('timers/promises');
@@ -288,6 +290,10 @@ class MockTimers {
288290
}
289291

290292
#createTimer(isInterval, callback, delay, ...args) {
293+
if (delay > TIMEOUT_MAX) {
294+
delay = 1;
295+
}
296+
291297
const timerId = this.#currentTimer++;
292298
const opts = {
293299
__proto__: null,

‎test/parallel/test-runner-mock-timers.js

+16
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ describe('Mock Timers Test Suite', () => {
251251
done();
252252
}), timeout);
253253
});
254+
255+
it('should change timeout to 1ms when it is >= 2 ** 31', (t) => {
256+
t.mock.timers.enable({ apis: ['setTimeout'] });
257+
const fn = t.mock.fn();
258+
global.setTimeout(fn, 2 ** 31);
259+
t.mock.timers.tick(1);
260+
assert.strictEqual(fn.mock.callCount(), 1);
261+
});
262+
263+
it('should change the delay to one if timeout < 0', (t) => {
264+
t.mock.timers.enable({ apis: ['setTimeout'] });
265+
const fn = t.mock.fn();
266+
global.setTimeout(fn, -1);
267+
t.mock.timers.tick(1);
268+
assert.strictEqual(fn.mock.callCount(), 1);
269+
});
254270
});
255271

256272
describe('clearTimeout Suite', () => {

0 commit comments

Comments
 (0)