FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

test_runner: check if timeout was cleared by own callback by Benricheson101 · Pull Request #51673 · nodejs/node · GitHub

/ node Public

test_runner: check if timeout was cleared by own callback - #51673

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
Benricheson101:fix/mock-timer-double-clear
Feb 18, 2024
Merged

test_runner: check if timeout was cleared by own callback#51673
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
Benricheson101:fix/mock-timer-double-clear

Conversation

Copy link
Copy Markdown
Contributor

Fixes calling clearTimeout in a mocked setTimeout callback resulting in two timeouts being removed from the execution queue (the current timeout and the next one in the queue). This happens because clearTimeout removes the timeout at execution queue position 1, followed by the call in tick() to #executionQueue.shift() removing the next item in the queue.

I fixed this by checking if the first item in the execution queue has the same ID as the one that is being executed before calling #executionQueue.shift(). If the IDs aren't the same, it means it was already removed by a call to clearTimeout in the callback.

test('should not affect other timers when clearing timeout inside own callback', (t) => {
  t.mock.timers.enable({ apis: ['setTimeout'] });
  const f = t.mock.fn();

  const timer = setTimeout(() => {
    f();

    // Clearing the already-expired timeout should do nothing.
    // This call to clearTimeout removes the current timeout from the execution queue
    clearTimeout(timer);
  }, 50);

  // Once the above callback runs, the call to `#executionQueue.shift()` removes this timeout from the queue
  setTimeout(f, 50);
  setTimeout(f, 50);

  t.mock.timers.runAll();
  assert.strictEqual(f.mock.callCount(), 3); // AssertionError 2 !== 3
});

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Feb 6, 2024

Copy link
Copy Markdown
Member

Nice catch!

MoLow added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no outstanding review comments, and a CI started. labels Feb 11, 2024
github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 14, 2024

Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Collaborator

MoLow added the commit-queue Add this label to land a pull request using GitHub Actions. label Feb 18, 2024
nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Feb 18, 2024
nodejs-github-bot merged commit 17187dd into nodejs:main Feb 18, 2024

Copy link
Copy Markdown
Collaborator

Landed in 17187dd

targos pushed a commit that referenced this pull request Feb 19, 2024
PR-URL: #51673
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
marco-ippolito mentioned this pull request Mar 1, 2024
richardlau pushed a commit that referenced this pull request Mar 25, 2024
PR-URL: #51673
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
richardlau mentioned this pull request Mar 25, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no outstanding review comments, and a CI started. needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL