Skip to content

Commit

Permalink
lib: test_runner#mock:timers respeced timeout_max behaviour
Browse files Browse the repository at this point in the history
PR-URL: nodejs#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]>
  • Loading branch information
badkeyy authored Oct 15, 2024
1 parent 6abd545 commit 5e76c40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const {
},
} = require('internal/errors');

const { TIMEOUT_MAX } = require('internal/timers');

const PriorityQueue = require('internal/priority_queue');
const nodeTimers = require('timers');
const nodeTimersPromises = require('timers/promises');
Expand Down Expand Up @@ -288,6 +290,10 @@ class MockTimers {
}

#createTimer(isInterval, callback, delay, ...args) {
if (delay > TIMEOUT_MAX) {
delay = 1;
}

const timerId = this.#currentTimer++;
const opts = {
__proto__: null,
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-runner-mock-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ describe('Mock Timers Test Suite', () => {
done();
}), timeout);
});

it('should change timeout to 1ms when it is >= 2 ** 31', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = t.mock.fn();
global.setTimeout(fn, 2 ** 31);
t.mock.timers.tick(1);
assert.strictEqual(fn.mock.callCount(), 1);
});

it('should change the delay to one if timeout < 0', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = t.mock.fn();
global.setTimeout(fn, -1);
t.mock.timers.tick(1);
assert.strictEqual(fn.mock.callCount(), 1);
});
});

describe('clearTimeout Suite', () => {
Expand Down

0 comments on commit 5e76c40

Please sign in to comment.