.Net: [.NET] Add TimeProvider injection to TimePlugin for deterministic testing#14112
Open
grvnmttl wants to merge 2 commits into
Open
.Net: [.NET] Add TimeProvider injection to TimePlugin for deterministic testing#14112grvnmttl wants to merge 2 commits into
grvnmttl wants to merge 2 commits into
Conversation
… tests Replaces direct DateTimeOffset.Now calls with an injected TimeProvider, defaulting to TimeProvider.System so existing callers are unaffected. Expands TimePluginTests from 5 fuzzy real-clock tests to 26 deterministic tests covering all public methods. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes TimePlugin deterministic-test friendly by introducing TimeProvider injection and updating all “current time” computations to use the injected provider rather than directly reading the system clock. It also rewrites the unit tests to use a fixed-time provider and validate formatted outputs deterministically.
Changes:
- Add
TimeProvidersupport toTimePluginand route time reads through it (GetLocalNow()/GetUtcNow()). - Replace real-clock dependent tests with deterministic tests using an inline fixed
TimeProviderimplementation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| dotnet/src/Plugins/Plugins.Core/TimePlugin.cs | Introduces TimeProvider-based clock access and updates time/date formatting methods accordingly. |
| dotnet/src/Plugins/Plugins.UnitTests/Core/TimePluginTests.cs | Replaces non-deterministic tests with fixed-time, culture-stable assertions across plugin methods. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Author
|
@microsoft-github-policy-service agree |
- Fix TimeZoneName() to use _timeProvider.LocalTimeZone instead of TimeZoneInfo.Local, so a custom provider's timezone is respected - Add deterministic TimeZoneName test (395 tests total, all passing) - Add Microsoft.Bcl.TimeProvider polyfill for netstandard2.0 target Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
|
All four Copilot review items addressed in the latest commit:
|
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.
Closes #14111
Summary
TimeProviderconstructor parameter toTimePlugin(defaults toTimeProvider.System, so all existing callers are unaffected)DateTimeOffset.Now/DateTimeOffset.UtcNowcalls with_timeProvider.GetLocalNow()/_timeProvider.GetUtcNow()TimePluginTestsfrom 5 real-clock tests to 26 deterministic tests covering every public method, using an inlineFixedUtcTimeProvidersubclass (no new test dependencies)Key decisions
System.TimeProvider(not a customIClock)FixedUtcTimeProvidersubclass in testsMicrosoft.Extensions.TimeProvider.Testingas a new dependency; 3-line subclass is clearerLocalTimeZone = TimeZoneInfo.Utcon the test providerTimeProvider.Systemin constructornew TimePlugin()) work unchangedTest results