Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: test_runner#mock:timers respeced timeout_max behaviour #55375

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the delay to one if timeout > 2_147_483_647', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = t.mock.fn();
global.setTimeout(fn, 2_147_483_647 + 1);
t.mock.timers.tick(1);
assert.strictEqual(fn.mock.callCount(), 1);
});
badkeyy marked this conversation as resolved.
Show resolved Hide resolved

it('sould change the delay to one if timeout < 0', (t) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar title change (nit)

badkeyy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading