Skip to content

[Tests] Fix random failure of Debounce_Default - #5478

Closed
NoobMaster (oONoobMasterOo) wants to merge 1 commit into
microsoft:devfrom
oONoobMasterOo:test/fix-flaky-tests
Closed

NoobMaster (oONoobMasterOo) wants to merge 1 commit into
microsoft:devfrom
oONoobMasterOo:test/fix-flaky-tests

Conversation

@oONoobMasterOo

Copy link
Copy Markdown
Contributor

DebounceTests.Debounce_Default fails randomly: it asserts that at least 50 ms elapsed after RunAsync(50, ...), measured with a Stopwatch. Debounce waits with Task.Delay, which relies on the system timer (about 15.6 ms resolution on Windows), so it can complete a few milliseconds before the Stopwatch measures 50 ms.

Measured locally (Windows, 40 runs): the shortest delay was 46.11 ms, and the test failed 1 time out of 20 runs.

The assertion now allows one timer tick (50 - timerResolution, with timerResolution = 16). It still verifies that the action is debounced and not executed immediately.

Unit Tests

Updated Debounce_Default.

Task.Delay relies on the system timer, which can complete a few
milliseconds before a Stopwatch measures the full delay (46 ms measured
for a 50 ms delay on Windows), so the test failed randomly.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused test-only adjustment addresses timing variance while retaining a meaningful minimum-delay check.

No open issues 🎉

What changed in this PR

Reduces intermittent failures in Debounce_Default caused by system-timer resolution.

Changes:

  • Adds a 16 ms tolerance, lowering the minimum elapsed-time assertion to 34 ms.
  • Preserves completion and action-execution checks.
File Description
tests/​Core/​Utilities/​DebounceTests.cs Allows timer-resolution variance in the debounce timing assertion.

Review effort: Balanced

@dvoituron

Copy link
Copy Markdown
Collaborator

Thanks for investigating this. I don’t agree with lowering the threshold from 50 ms to 34 ms to address the flaky test. It weakens the assertion on every platform, including Linux, and could hide a regression in the requested delay.

I’d prefer a deterministic test using a TimeProvider and simulated time, rather than introducing a fixed tolerance based on Windows timer granularity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants