From f501db85560bc3f821b4ebc115ec7477f7f45ba9 Mon Sep 17 00:00:00 2001 From: Yuya Inoue <65857152+inoway46@users.noreply.github.com> Date: Tue, 10 Nov 2026 06:26:34 +0900 Subject: [PATCH] test: deflake test-runner-mock-timers-scheduler The first test case checked that the mocked `scheduler.wait(4000)` resolved within 100ms of real wall-clock time. On slow or busy CI machines the time between `Date.now()` and the promise callback can exceed 100ms even though the mock works, so the test failed with "time should be advanced less than the 100ms". Replace the wall-clock assertion with a deterministic check: after `tick(4000)`, wait one turn of the event loop and assert the promise has settled. A real 4000ms timer cannot fire within that window, so the check still proves the mocked timer was used. This matches how the other cases in the file verify the mock. Refs: https://github.com/nodejs/reliability/issues/1354 Refs: https://github.com/nodejs/reliability/issues/1278 Signed-off-by: inoway46 PR-URL: https://github.com/nodejs/node/pull/67296 Refs: https://github.com/nodejs/node/actions/runs/42153384377 Reviewed-By: Matteo Collina Reviewed-By: Chemi Atlow --- .../test-runner-mock-timers-scheduler.js | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-runner-mock-timers-scheduler.js b/test/parallel/test-runner-mock-timers-scheduler.js index fa019221fc8..7f6d66e0734 100644 --- a/test/parallel/test-runner-mock-timers-scheduler.js +++ b/test/parallel/test-runner-mock-timers-scheduler.js @@ -7,22 +7,20 @@ const { it, describe } = require('node:test'); const nodeTimersPromises = require('node:timers/promises'); describe('Mock Timers Scheduler Test Suite', () => { - it('should advance in time and trigger timers when calling the .tick function', (t) => { + it('should advance in time and trigger timers when calling the .tick function', async (t) => { t.mock.timers.enable({ apis: ['scheduler.wait'] }); - const now = Date.now(); - const durationAtMost = 100; + const fn = t.mock.fn(); - const p = nodeTimersPromises.scheduler.wait(4000); + nodeTimersPromises.scheduler.wait(4000).then(fn).then(common.mustCall()); t.mock.timers.tick(4000); - return p.then(common.mustCall((result) => { - assert.strictEqual(result, undefined); - assert.ok( - Date.now() - now < durationAtMost, - `time should be advanced less than the ${durationAtMost}ms` - ); - })); + // A real 4000ms timer cannot fire within a single turn of the event loop, + // so the promise being settled here proves the mocked timer was used. + await nodeTimersPromises.setImmediate(); + + assert.strictEqual(fn.mock.callCount(), 1); + assert.deepStrictEqual(fn.mock.calls[0].arguments, [undefined]); }); it('should advance in time and trigger timers when calling the .tick function multiple times', async (t) => {