From dd848f2d1e5b60394c43597c339bb01fabf64b9c Mon Sep 17 00:00:00 2001 From: BadKey Date: Tue, 15 Oct 2024 15:43:24 +0200 Subject: [PATCH] lib: test_runner#mock:timers respeced timeout_max behaviour PR-URL: https://github.com/nodejs/node/pull/55375 Reviewed-By: Marco Ippolito Reviewed-By: Colin Ihrig Reviewed-By: Benjamin Gruenbaum Reviewed-By: Claudio Wunder --- lib/internal/test_runner/mock/mock_timers.js | 6 ++++++ test/parallel/test-runner-mock-timers.js | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index 7c75892b95735c..ed3108f5539b09 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -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'); @@ -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, diff --git a/test/parallel/test-runner-mock-timers.js b/test/parallel/test-runner-mock-timers.js index e2a86a5263636a..9e1bc7e62cc5b2 100644 --- a/test/parallel/test-runner-mock-timers.js +++ b/test/parallel/test-runner-mock-timers.js @@ -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', () => {