| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0761407 commit 9ff0df1
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -622,8 +622,12 @@ class MockTimers { | |||
| 622 | 622 | if (timer.runAt > this.#now) break; | |
| 623 | 623 | FunctionPrototypeApply(timer.callback, undefined, timer.args); | |
| 624 | 624 | ||
| 625 | - this.#executionQueue.shift(); | ||
| 626 | - timer.priorityQueuePosition = undefined; | ||
| 625 | + // Check if the timeout was cleared by calling clearTimeout inside its own callback | ||
| 626 | + const afterCallback = this.#executionQueue.peek(); | ||
| 627 | + if (afterCallback.id === timer.id) { | ||
| 628 | + this.#executionQueue.shift(); | ||
| 629 | + timer.priorityQueuePosition = undefined; | ||
| 630 | + } | ||
| 627 | 631 | ||
| 628 | 632 | if (timer.interval !== undefined) { | |
| 629 | 633 | timer.runAt += timer.interval; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -532,6 +532,22 @@ describe('Mock Timers Test Suite', () => { | |||
| 532 | 532 | await nodeTimersPromises.setImmediate(); // let promises settle | |
| 533 | 533 | assert.strictEqual(f2.mock.callCount(), 1); | |
| 534 | 534 | }); | |
| 535 | + | ||
| 536 | + it('should not affect other timers when clearing timeout inside own callback', (t) => { | ||
| 537 | + t.mock.timers.enable({ apis: ['setTimeout'] }); | ||
| 538 | + const f = t.mock.fn(); | ||
| 539 | + | ||
| 540 | + const timer = nodeTimers.setTimeout(() => { | ||
| 541 | + f(); | ||
| 542 | + // Clearing the already-expired timeout should do nothing | ||
| 543 | + nodeTimers.clearTimeout(timer); | ||
| 544 | + }, 50); | ||
| 545 | + nodeTimers.setTimeout(f, 50); | ||
| 546 | + nodeTimers.setTimeout(f, 50); | ||
| 547 | + | ||
| 548 | + t.mock.timers.runAll(); | ||
| 549 | + assert.strictEqual(f.mock.callCount(), 3); | ||
| 550 | + }); | ||
| 535 | 551 | }); | |
| 536 | 552 | ||
| 537 | 553 | describe('setInterval Suite', () => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments