Skip to content

Commit 7667a8f

Browse files
inoway46aduh95
authored andcommitted
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: nodejs/reliability#1354 Refs: nodejs/reliability#1278 Signed-off-by: inoway46 <inoueyuya416@gmail.com> PR-URL: #67296 Refs: https://github.com/nodejs/node/actions/runs/42153384377 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent b7aab20 commit 7667a8f

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

‎test/parallel/test-runner-mock-timers-scheduler.js‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@ const { it, describe } = require('node:test');
77
const nodeTimersPromises = require('node:timers/promises');
88

99
describe('Mock Timers Scheduler Test Suite', () => {
10-
it('should advance in time and trigger timers when calling the .tick function', (t) => {
10+
it('should advance in time and trigger timers when calling the .tick function', async (t) => {
1111
t.mock.timers.enable({ apis: ['scheduler.wait'] });
1212

13-
const now = Date.now();
14-
const durationAtMost = 100;
13+
const fn = t.mock.fn();
1514

16-
const p = nodeTimersPromises.scheduler.wait(4000);
15+
nodeTimersPromises.scheduler.wait(4000).then(fn).then(common.mustCall());
1716
t.mock.timers.tick(4000);
1817

19-
return p.then(common.mustCall((result) => {
20-
assert.strictEqual(result, undefined);
21-
assert.ok(
22-
Date.now() - now < durationAtMost,
23-
`time should be advanced less than the ${durationAtMost}ms`
24-
);
25-
}));
18+
// A real 4000ms timer cannot fire within a single turn of the event loop,
19+
// so the promise being settled here proves the mocked timer was used.
20+
await nodeTimersPromises.setImmediate();
21+
22+
assert.strictEqual(fn.mock.callCount(), 1);
23+
assert.deepStrictEqual(fn.mock.calls[0].arguments, [undefined]);
2624
});
2725

2826
it('should advance in time and trigger timers when calling the .tick function multiple times', async (t) => {

0 commit comments

Comments
 (0)