From b098287f562e7bdc62277e348e50e6a91460722b Mon Sep 17 00:00:00 2001 From: Julian Gassner Date: Sun, 13 Oct 2024 15:30:49 +0200 Subject: [PATCH 1/5] lib: test_runner#mock:timers respeced timeout_max behaviour --- lib/internal/test_runner/mock/mock_timers.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index a1e0043cafcf29..1f4852e21e9aec 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -48,6 +48,8 @@ let kResistStopPropagation; let kMock; // Initial epoch to which #now should be set to const kInitialEpoch = 0; +/** @see lib/internal/timers.js#TIMEOUT_MAX */ +const TIMEOUT_MAX = 2 ** 31 - 1; function compareTimersLists(a, b) { return (a.runAt - b.runAt) || (a.id - b.id); @@ -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, From 2c28e94650adbce13bc135e6b67d946d4ae187c3 Mon Sep 17 00:00:00 2001 From: Julian Gassner Date: Sun, 13 Oct 2024 22:40:41 +0200 Subject: [PATCH 2/5] test: added test for mock that covers timeout < 0 and timeout > max integer --- test/parallel/test-runner-mock-timers.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/parallel/test-runner-mock-timers.js b/test/parallel/test-runner-mock-timers.js index e2a86a5263636a..5d0463b87b137d 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 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); + }); + + it('sould 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', () => { From 0c60873dd20f8ac065a3ef5ca2de297c93882049 Mon Sep 17 00:00:00 2001 From: Julian Gassner Date: Sun, 13 Oct 2024 22:41:18 +0200 Subject: [PATCH 3/5] lib: import timeout_max from internal/timers --- lib/internal/test_runner/mock/mock_timers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index 1f4852e21e9aec..9a49cc984ce77d 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'); @@ -48,8 +50,6 @@ let kResistStopPropagation; let kMock; // Initial epoch to which #now should be set to const kInitialEpoch = 0; -/** @see lib/internal/timers.js#TIMEOUT_MAX */ -const TIMEOUT_MAX = 2 ** 31 - 1; function compareTimersLists(a, b) { return (a.runAt - b.runAt) || (a.id - b.id); From 73d4dd2650135f3b3cbc1303d880c3e488b91653 Mon Sep 17 00:00:00 2001 From: BadKey Date: Mon, 14 Oct 2024 18:49:52 +0200 Subject: [PATCH 4/5] test: fix typo Co-authored-by: Colin Ihrig --- test/parallel/test-runner-mock-timers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-runner-mock-timers.js b/test/parallel/test-runner-mock-timers.js index 5d0463b87b137d..f46d8cf09275f9 100644 --- a/test/parallel/test-runner-mock-timers.js +++ b/test/parallel/test-runner-mock-timers.js @@ -260,7 +260,7 @@ describe('Mock Timers Test Suite', () => { assert.strictEqual(fn.mock.callCount(), 1); }); - it('sould change the delay to one if timeout < 0', (t) => { + 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); From 4ca4597b4c1ed001acf1ece29e83386c66ae4301 Mon Sep 17 00:00:00 2001 From: BadKey Date: Mon, 14 Oct 2024 18:50:43 +0200 Subject: [PATCH 5/5] test: rephrase Co-authored-by: Aviv Keller --- test/parallel/test-runner-mock-timers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-runner-mock-timers.js b/test/parallel/test-runner-mock-timers.js index f46d8cf09275f9..9e1bc7e62cc5b2 100644 --- a/test/parallel/test-runner-mock-timers.js +++ b/test/parallel/test-runner-mock-timers.js @@ -252,10 +252,10 @@ describe('Mock Timers Test Suite', () => { }), timeout); }); - it('should change the delay to one if timeout > 2_147_483_647', (t) => { + 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_147_483_647 + 1); + global.setTimeout(fn, 2 ** 31); t.mock.timers.tick(1); assert.strictEqual(fn.mock.callCount(), 1); });