Repository navigation
feat: add an opt-in strict timer order to the MockTimeSystem - #1194
Merged
Merged
Conversation
vbreuss
force-pushed
the
feat/strict-timer-order
branch
from
November 4, 2026 14:40
7e7741c to
168f5ca
Compare
With auto-advance, the thread that moves the clock waits for a timer callback only until it was blocked for one second of real time, as the callback might wait for something that only a later timer delivers, and it waits at most one second for the creator of a timer to finish setting it up, e.g. for `Task.Delay` on the `TimeProvider` to register its cancellation. On a loaded machine, both can let a later timer fire before an earlier one completed, and a wrong guess does not fail: the timers silently fire in another order, e.g. a 30s delay completes before the 10s timeout that should cancel it. Tests on a virtual clock that depend on the order of their timers, like those of aweXpect, then fail only on busy CI runners. `MockTimeSystemOptions.EnableStrictTimerOrder()` (5 seconds) and `EnableStrictTimerOrder(TimeSpan callbackTimeout)` make the thread that moves the clock wait for each callback until it completes or waits for the clock through the `MockTimeSystem`, without the one second heuristic. The timers that move the clock on their own and `AdvanceToNextTimer` also wait up to the timeout for the creator of a timer, and then move on like the real clock does, as a creator may never finish, e.g. a delay with the token of a disposed `CancellationTokenSource`. A callback that blocks on something that only the clock can deliver can never complete in this mode. Instead of moving on silently, the scheduler fails after the timeout with a `TimeoutException` that names the due time of the timer: the code that moves the clock throws it, and so does every later move of the clock (`AdvanceBy`, `SetTo`, auto-advancing `Task.Delay`, `Thread.Sleep` and `PeriodicTimer`), `AdvanceToNextTimer` and `WaitUntilIdle(Async)`, whose waiters are woken. An expired timeout of `AdvanceToNextTimer` fails the scheduler as well. The default mode is unchanged. The scope is deliberately narrower than "callbacks never run concurrently": callbacks still run on their own threads, so that a callback can wait for the clock, and the existing heuristics (timeouts that do not move the clock, the set-up of `Task.Delay` on the `TimeProvider`) stay. Documented limitations: another thread that moves the clock at the same time can still move it past a running callback; when the failure happens in the background loop of auto-advance, awaiting a timer (e.g. a delay on the `TimeProvider`) never completes and only the next move of the clock or `WaitUntilIdle` reports it; a time change callback that moves the clock again holds the clock, so a timer callback that reads it in the meantime fails the strict order. This builds on the idle tracking of the stepping and idle detection, which it is stacked on.
The XML docs of EnableStrictTimerOrder explain what it does, but not when to use it. They now name the typical case: tests that depend on the order of their timers, e.g. a timeout that must cancel a delay, and that fail only on a loaded machine.
Resolves the Sonar issue about an unobserved async result.
vbreuss
force-pushed
the
feat/strict-timer-order
branch
from
November 4, 2026 15:51
70ba869 to
e74914d
Compare
|
|
This is addressed in release v8.0.0. |
This was referenced Nov 11, 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.



With auto-advance, the thread that moves the clock waits for a timer callback only until it was blocked for one second of real time, as the callback might wait for something that only a later timer delivers, and it waits at most one second for the creator of a timer to finish setting it up, e.g. for
Task.Delayon theTimeProviderto register its cancellation. On a loaded machine, both can let a later timer fire before an earlier one completed, and a wrong guess does not fail: the timers silently fire in another order, e.g. a 30s delay completes before the 10s timeout that should cancel it. Tests on a virtual clock that depend on the order of their timers, like those of aweXpect, then fail only on busy CI runners.MockTimeSystemOptions.EnableStrictTimerOrder()(5 seconds) andEnableStrictTimerOrder(TimeSpan callbackTimeout)make the thread that moves the clock wait for each callback until it completes or waits for the clock through theMockTimeSystem, without the one second heuristic. The timers that move the clock on their own andAdvanceToNextTimeralso wait up to the timeout for the creator of a timer, and then move on like the real clock does, as a creator may never finish, e.g. a delay with the token of a disposedCancellationTokenSource.A callback that blocks on something that only the clock can deliver can never complete in this mode. Instead of moving on silently, the scheduler fails after the timeout with a
TimeoutExceptionthat names the due time of the timer: the code that moves the clock throws it, and so does every later move of the clock (AdvanceBy,SetTo, auto-advancingTask.Delay,Thread.SleepandPeriodicTimer),AdvanceToNextTimerandWaitUntilIdle(Async), whose waiters are woken. An expired timeout ofAdvanceToNextTimerfails the scheduler as well. The default mode is unchanged.The scope is deliberately narrower than "callbacks never run concurrently": callbacks still run on their own threads, so that a callback can wait for the clock, and the existing heuristics (timeouts that do not move the clock, the set-up of
Task.Delayon theTimeProvider) stay. Documented limitations: another thread that moves the clock at the same time can still move it past a running callback; when the failure happens in the background loop of auto-advance, awaiting a timer (e.g. a delay on theTimeProvider) never completes and only the next move of the clock orWaitUntilIdlereports it; a time change callback that moves the clock again holds the clock, so a timer callback that reads it in the meantime fails the strict order.