Repository navigation
feat!: replace ITimeSystem with TimeProvider - #2148
Conversation
The clock of an evaluation was the custom `ITimeSystem` in `aweXpect.Core.Internal`, experimental behind the diagnostic `aweXpect5001`. It is now the standard `System.TimeProvider`, so a test can pass the clock it already uses, e.g. the `FakeTimeProvider` of `Microsoft.Extensions.TimeProvider.Testing`, and share it with the code under test instead of writing an adapter. `WithTimeProvider(timeProvider)` in the namespace `aweXpect` replaces `WithTimeSystem(timeSystem)`. As it only takes a standard type, it is stable public API and no longer experimental; it still applies to every expectation combined in `Expect.ThatAll` or `Expect.ThatAny`. `TimeProvider.System` is the default and keeps the waits and timeouts of the real clock unchanged: it still uses `Task.Delay` and `CancellationTokenSource.CancelAfter` directly. Any other provider gets a timer from `CreateTimer` with the exact delay on every target framework, also for an infinite wait. Extensions that wait for something outside aweXpect with a timeout of their own, e.g. a mock that waits for an interaction `Within(timeout)`, could only cancel their wait with `CancelAfter` on the real clock, while `GetTimestamp` and `GetElapsedTime` of the evaluation context measured on the evaluation clock. On a virtual clock the timeout elapsed in real time and `HasWaitElapsed` compared it with virtual time. The new `CancelAfter` of `IEvaluationContext` cancels on the clock of the evaluation, so the timeout and the measured time agree. The test helper `VirtualTimeSystem` is now the `TimeProvider` `VirtualTimeProvider` with the same semantics: a timer whose state is a `CancellationTokenSource` is a scheduled cancellation, every other timer is a wait. BREAKING CHANGE: `WithTimeSystem(…)` and `ITimeSystem` are removed; use `WithTimeProvider(…)` with a `TimeProvider` instead. The diagnostic `aweXpect5001` no longer exists. `aweXpect.Core` depends on `Microsoft.Bcl.TimeProvider` for `netstandard2.0`.
`WithTimeProvider(…)` is an extension method on every expectation. In the namespace `aweXpect`, which every test imports for `Expect.That`, it showed up on every result although only tests that run on a virtual clock need it. In `aweXpect.Core`, the namespace of `Expectation`, it only shows up where the test imports it.
|
🚀 BenchmarksCompared to aweXpectCompared to Relative performance (how many times as long the other library takes as aweXpect, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V45 2.60GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 10.0.303 [Host] : .NET 8.0.33 (8.0.33, 8.0.3326.52219), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
aweXpect.JsonCompared to Relative performance (how many times as long the other library takes as aweXpect, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V45 4.02GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 10.0.303 [Host] : .NET 8.0.33 (8.0.33, 8.0.3326.52219), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
aweXpect.WebCompared to ✅ No changes in memory or relative performance (6 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 2.45GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 10.0.303 [Host] : .NET 8.0.33 (8.0.33, 8.0.3326.52219), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
aweXpect.XmlCompared to Memory
All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 2.45GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 10.0.303 [Host] : .NET 8.0.33 (8.0.33, 8.0.3326.52219), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
aweXpect.ReflectionCompared to ✅ No changes in memory or relative performance (6 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 3.22GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 10.0.303 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
|
… with TimeProvider (#2148) by Valentin Breuß
… with TimeProvider (#2148) by Valentin Breuß
… with TimeProvider (#2148) by Valentin Breuß
… with TimeProvider (#2148) by Valentin Breuß
…eSystem with TimeProvider (#2148) by Valentin Breuß
…eSystem with TimeProvider (#2148) by Valentin Breuß
…m with TimeProvider (#2148) by Valentin Breuß
…m with TimeProvider (#2148) by Valentin Breuß
…h TimeProvider (#2148) by Valentin Breuß
…h TimeProvider (#2148) by Valentin Breuß
aweXpect 4.0.0-pre.3 replaces the experimental `ITimeSystem` with the standard `TimeProvider` (Testably/aweXpect#2148). It also adds `CancelAfter` on `IEvaluationContext`, which cancels on the clock of the evaluation, for extensions that wait with a timeout of their own, like a mock that waits for an interaction `Within(timeout)`. The `Within` timeout and the `Then` budget used `CancellationTokenSource.CancelAfter` and so always elapsed in real time, while the elapsed time was measured on the clock of the evaluation. With `WithTimeProvider(…)` and a virtual clock, they now elapse on that clock as well, so the timeout and the measured time agree. On the default real clock the behaviour does not change. The test helper `VirtualTimeSystem` is replaced by a `TimeProvider`, `VirtualTimeProvider`, whose timers fire when the test advances the clock. Two new tests check that `Within` and the `Then` budget elapse on the given clock; without the change they wait 30 seconds of real time.



The clock of an evaluation was the custom
ITimeSysteminaweXpect.Core.Internal, experimental behind the diagnosticaweXpect5001. It is now the standardSystem.TimeProvider, so a test can pass the clock it already uses, e.g. theFakeTimeProviderofMicrosoft.Extensions.TimeProvider.Testing, and share it with the code under test instead of writing an adapter.WithTimeProvider(timeProvider)in the namespaceaweXpect.CorereplacesWithTimeSystem(timeSystem). As it only takes a standard type, it is stable public API and no longer experimental; it still applies to every expectation combined inExpect.ThatAllorExpect.ThatAny.TimeProvider.Systemis the default and keeps the waits and timeouts of the real clock unchanged: it still usesTask.DelayandCancellationTokenSource.CancelAfterdirectly. Any other provider gets a timer fromCreateTimerwith the exact delay on every target framework, also for an infinite wait.Extensions that wait for something outside aweXpect with a timeout of their own, e.g. a mock that waits for an interaction
Within(timeout), could only cancel their wait withCancelAfteron the real clock, whileGetTimestampandGetElapsedTimeof the evaluation context measured on the evaluation clock. On a virtual clock the timeout elapsed in real time andHasWaitElapsedcompared it with virtual time. The newCancelAfterofIEvaluationContextcancels on the clock of the evaluation, so the timeout and the measured time agree.The test helper
VirtualTimeSystemis now theTimeProviderVirtualTimeProviderwith the same semantics: a timer whose state is aCancellationTokenSourceis a scheduled cancellation, every other timer is a wait.BREAKING CHANGE:
WithTimeSystem(…)andITimeSystemare removed; useWithTimeProvider(…)with aTimeProviderinstead. The diagnosticaweXpect5001no longer exists.aweXpect.Coredepends onMicrosoft.Bcl.TimeProviderfornetstandard2.0.