Skip to content

Commit dd848f2

Browse files
badkeyyaduh95
authored andcommitted
lib: test_runner#mock:timers respeced timeout_max behaviour
PR-URL: #55375 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Claudio Wunder <cwunder@gnome.org>
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy