test_runner: fix clearing final timeout in own callback

PR-URL: https://github.com/nodejs/node/pull/52332
Fixes: https://github.com/nodejs/node/issues/52325
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Ben Richeson authored and Moshe Atlow committed 2024-04-11 10:40:39 +03:00
1 parent 05cb16dc1a
commit 3f8cc88009
2 files changed
+14 -1

No files matched your search

+1 -1
View File
@@ -652,7 +652,7 @@ class MockTimers {
// Check if the timeout was cleared by calling clearTimeout inside its own callback
const afterCallback = this.#executionQueue.peek();
if (afterCallback.id === timer.id) {
if (afterCallback?.id === timer.id) {
this.#executionQueue.shift();
timer.priorityQueuePosition = undefined;
}
+13
View File
@@ -548,6 +548,19 @@ describe('Mock Timers Test Suite', () => {
t.mock.timers.runAll();
assert.strictEqual(f.mock.callCount(), 3);
});
it('should allow clearing timeout inside own callback', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const f = t.mock.fn();
const timer = nodeTimers.setTimeout(() => {
f();
nodeTimers.clearTimeout(timer);
}, 50);
t.mock.timers.runAll();
assert.strictEqual(f.mock.callCount(), 1);
});
});
describe('setInterval Suite', () => {