feat: bridge ITimeSystem to the BCL System.TimeProvider#1044
Merged
Conversation
Test Results 102 files ± 0 102 suites ±0 2h 24m 42s ⏱️ - 4m 41s Results for commit 0b6961d. ± Comparison against base commit 696ffa9. This pull request removes 109749 and adds 109753 tests. Note that renamed tests count towards both.This pull request removes 12552 skipped tests and adds 12552 skipped tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
5d59c88 to
4e7b61c
Compare
Add an `ITimeSystem.ToTimeProvider()` extension that wraps any time system (in particular a `MockTimeSystem`) as a real `System.TimeProvider`, so it can be injected into code that consumes the built-in .NET time abstraction (`HttpClient`, `System.Threading.Channels`, `Microsoft.Extensions.*`). Advancing the mock clock is observable through `GetUtcNow()` and `GetTimestamp()`, and `CreateTimer(...)` is routed through the mock timer. The adapter overrides `GetUtcNow`, `GetTimestamp`/`TimestampFrequency` (which keeps the base `GetElapsedTime` correct because the mock stopwatch is self-consistent) and `CreateTimer`, adapting the time system's `ITimer` to `System.Threading.ITimer`. It is gated behind a new `FEATURE_TIMEPROVIDER` flag (net8.0+, where `System.TimeProvider` is intrinsic, so no additional package dependency is required). Placed in `Testably.Abstractions.Testing` because that project references the interface via a package reference and because the primary use case is feeding mock time into code that already takes `System.TimeProvider`. Note: `System.TimeProvider` must be fully qualified throughout, because the unqualified name binds to the existing static `Testably.Abstractions.Testing.TimeProvider` factory even from nested namespaces.
4e7b61c to
0b6961d
Compare
|
|
This is addressed in release v7.0.0. |
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.

Add an
ITimeSystem.ToTimeProvider()extension that wraps any time system (in particular aMockTimeSystem) as a realSystem.TimeProvider, so it can be injected into code that consumes the built-in .NET time abstraction (HttpClient,System.Threading.Channels,Microsoft.Extensions.*). Advancing the mock clock is observable throughGetUtcNow()andGetTimestamp(), andCreateTimer(...)is routed through the mock timer.The adapter overrides
GetUtcNow,GetTimestamp/TimestampFrequency(which keeps the baseGetElapsedTimecorrect because the mock stopwatch is self-consistent) andCreateTimer, adapting the time system'sITimertoSystem.Threading.ITimer. It is gated behind a newFEATURE_TIMEPROVIDERflag (net8.0+, whereSystem.TimeProvideris intrinsic, so no additional package dependency is required).Placed in
Testably.Abstractions.Testingbecause that project references the interface via a package reference and because the primary use case is feeding mock time into code that already takesSystem.TimeProvider.Note:
System.TimeProvidermust be fully qualified throughout, because the unqualified name binds to the existing staticTestably.Abstractions.Testing.TimeProviderfactory even from nested namespaces.System.TimeProvider#1040