mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
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 <inoueyuya416@gmail.com> PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
eb3ae472b6
commit
f501db8556
1 file changed
+9
-11
@@ -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) => {
|
||||
|
||||
Reference in new issue
Block a user