.Net: [.NET] Add TimeProvider injection to TimePlugin for deterministic testing#14112
.Net: [.NET] Add TimeProvider injection to TimePlugin for deterministic testing#14112grvnmttl 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>
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.
|
@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>
|
All four Copilot review items addressed in the latest commit:
|
|
Hi, just a gentle nudge — the CI checks are stuck at All four items from the Copilot review have been addressed. Happy to make any further changes based on your feedback. Thanks! |
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