Components: Migrate time-dependent logic to TimeProvider abstraction#12592
Merged
danielchalmers merged 25 commits intodevfrom Feb 2, 2026
Merged
Components: Migrate time-dependent logic to TimeProvider abstraction#12592danielchalmers merged 25 commits intodevfrom
danielchalmers merged 25 commits intodevfrom
Conversation
Co-authored-by: danielchalmers <[email protected]>
Co-authored-by: danielchalmers <[email protected]>
Copilot
AI
changed the title
[WIP] Migrate time-dependent components to TimeProvider
Migrate time-dependent components to TimeProvider abstraction
Feb 1, 2026
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Co-authored-by: danielchalmers <[email protected]>
This comment was marked as outdated.
This comment was marked as outdated.
Summary: - Registered `FakeTimeProvider` for all test contexts in TestContextExtensions.cs. - Updated time-based component tests to use `Context.AdvanceTime`, removed `[NonParallelizable]`, and added render waits where needed in AutocompleteTests.cs, MenuTests.cs, SelectTests.cs, SnackbarTests.cs, CarouselTests.cs, FormTests.cs, DatePickerTests.cs, and DateRangePickerTests.cs. - Replaced `TimeProvider.System` usage with `FakeTimeProvider` in PopoverServiceTests.cs and SnackbarServiceTests.cs
This was referenced Feb 23, 2026
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original prompt
This section details on the original issue you should resolve
<issue_title>Migrate Components to TimeProvider</issue_title>
<issue_description>This issue tracks the migration of time-dependent components to the
TimeProviderabstraction (introduced in .NET 8). This change allows the use ofFakeTimeProviderin unit tests, eliminating flakiness caused byTask.Delay,Stopwatch, and system clock drift. We will remove[NonParallelizable]from tests for any components modified in this issue and ensure the entire suite remains correct when run in parallel.Problem Statement
Key components currently rely on
DateTime.Now,Stopwatch,System.Threading.Timer, or wall-clockTask.Delay.Task.Delay()to simulate debouncing.Implementation Pattern
1. Dependency Injection Rules
Internal services (C# services registered in DI):
TimeProvider(strict DI).Razor components (e.g.,
MudAutocomplete,MudSelect, etc.):[Inject].null!initialization pattern to match existing architecture.Example for components:
Service Registration (Library Citizenship):
Update
AddMudServicesto ensure a provider exists if the host hasn’t registered one:2. Type Migration Rules
Now: Replace all direct “now” reads with
TimeProvider.GetUtcNow()(for components/services using property injection, use the injectedTimeProviderinstance).Internal State: Use
DateTimeOffsetfor all internal timestamps (e.g.,_lastClick,_debounceStart,_activationDate).Timers: Replace
System.Threading.TimerwithITimerviaTimeProvider.CreateTimer().Stopwatch: Remove
Stopwatchusage entirely.ITimer+GetUtcNow()deadline/remaining-time models for UI timeouts.TimeProvider.GetTimestamp()only when monotonic high-precision timing is required.Targets
MudAutocomplete
System.Threading.Timerfor debouncing search input (and may useTimeProviderpartially).ITimercreated viaTimeProvider.fakeTime.Advance().MudMenu
TimeProvidervia[Inject](if not already). Change_lastKeyboardActivationtype fromDateTimetoDateTimeOffset.MudSelect
TimeProvidervia[Inject](if not already). Change_lastSearchTimetype fromDateTimetoDateTimeOffset.Snackbar
Stopwatchto handle pausing (on hover) and resuming.Stopwatchwith a deadline + remaining-time model usingDateTimeOffsetfromTimeProvider.GetUtcNow()and anITimercreated viaTimeProvider.CreateTimer().deadline = now + timeoutand create a one-shot timer fortimeout. On hover/pause, dispose the timer and computeremaining = max(deadline - now, 0). On resume, recreate the one-shot timer forremainingand recomputedeadline = now + remaining. For “infinite” snackbars, keepremaining/deadlineunset and do not create a timer.General Render Cycle
Change: Replace
await Task.Delay(1)with aTimeProvideroverload (no wall-clock delays), e.g.:Benefit: Allows tests to deterministically skip “wait for render” hacks.
Additional Known Call Sites (Explicitly In Scope)
Include the following in the migration work (they currently use banned APIs and should be updated per the rules above):
MudTimeSeriesChart(e.g., default date ranges based onDateTime.Now)MudDatePicker/MudDateRangePicker(e.g.,DateTime.UtcNowdebouncing guards andDateTime.Nowdefault ranges)MudPopoverHolder(e.g., tracking activation time viaDateTime.Now)Usage Audit Requirement
Some components already have
TimeProviderinjected but still use banned APIs in other parts of the same fil...💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.