Skip to content

[Debounce] Use a TimeProvider for testing - #5480

Merged
Denis Voituron (dvoituron) merged 3 commits into
microsoft:devfrom
oONoobMasterOo:test/debounce-time-provider
Oct 21, 2026
Merged

Denis Voituron (dvoituron) merged 3 commits into
microsoft:devfrom
oONoobMasterOo:test/debounce-time-provider

Conversation

@oONoobMasterOo

@oONoobMasterOo NoobMaster (oONoobMasterOo) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

📖 Description

DebounceTests.Debounce_Default failed randomly: it measured the delay with a Stopwatch, and Task.Delay can complete a few milliseconds before 50 ms on Windows (system timer resolution).

Changes

  • Debounce waits with Task.Delay(TimeSpan, TimeProvider, CancellationToken).
  • The public constructor uses TimeProvider.System, so the behavior is unchanged. An internal constructor accepts a TimeProvider for the tests.
  • Test dependency added: Microsoft.Extensions.TimeProvider.Testing (FakeTimeProvider).

🎫 Issues

👩‍💻 Reviewer Notes

Test-only purpose; the public Debounce constructor still uses TimeProvider.System.

📑 Test Plan

Unit tests

Updated Debounce_Default: after RunAsync(50, ...), advancing the time by 49 ms doesn't run the action, and advancing 1 more ms runs it. No real waiting, and the exact 50 ms delay is checked: with a delay shortened by 10 ms, the test fails.

  • Full tests/Core suite on this branch: 4903 tests, 0 failed (at a170367fb).

Other checks

  • DebounceTests: 9 / 9 pass.
  • The library builds for net8.0 (Task.Delay(TimeSpan, TimeProvider, CancellationToken) is available since .NET 8).

✅ Checklist

General

  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

AI contribution declaration

AI assistance is allowed; fully AI-generated or autonomous pull requests without meaningful human review are not accepted. See the AI contribution policy.

  • I have read and followed the AI contribution policy.
  • I have personally reviewed and understood all changes, tested them, and take responsibility for this contribution.

Component-specific

  • I have added a new component
  • I have added Unit Tests for my new component
  • I have modified an existing component
  • I have validated the Unit Tests for an existing component

⏭ Next Steps

None.

Debounce_Default measured the delay with a Stopwatch, which randomly failed on Windows because Task.Delay can complete a few milliseconds early. Debounce now waits on a TimeProvider (TimeProvider.System by default), and the test advances a FakeTimeProvider.

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 changes have no identified blocking issues; tests were not executed in this review environment.

No open issues 🎉

What changed in this PR

Replaces wall-clock timing in the debounce delay test with simulated time, addressing the intermittent failure discussed in #5478.

Changes:

  • Adds an internal TimeProvider constructor while retaining TimeProvider.System publicly.
  • Checks the 50 ms delay using FakeTimeProvider.
  • Adds the required test dependency.
File Description
tests/​Core/​Utilities/​DebounceTests.cs Replaces stopwatch timing with simulated-time assertions.
tests/​Core/​Components.Tests.csproj References the time-provider testing package.
src/​Core/​Utilities/​Debounce.cs Supports an injected time provider for delays.
Directory.Packages.props Defines the testing package version.

Review effort: Balanced

@dvoituron

Copy link
Copy Markdown
Collaborator

We've added a new requirement regarding PRs

@oONoobMasterOo

Copy link
Copy Markdown
Contributor Author

We've added a new requirement regarding PRs

Checked

@dvoituron Denis Voituron (dvoituron) changed the title [Tests] Use a TimeProvider in the Debounce delay test [Debounce] Use a TimeProvider for testing Oct 21, 2026
@dvoituron
Denis Voituron (dvoituron) merged commit e01abda into microsoft:dev Oct 21, 2026
4 checks passed
This was referenced Nov 16, 2026
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