Notifications
MockTimeSystem.On lets a test observe time-related events from the system under test - useful for asserting "the code asked to sleep for 5 seconds" without relying on side effects.
MockTimeSystem timeSystem = new();
using var sleeps = timeSystem.On.ThreadSleep();
DoWorkThatSleeps(timeSystem);
TimeSpan[] requestedSleeps = sleeps.Wait(count: 1);
await Expect.That(requestedSleeps[0]).IsEqualTo(TimeSpan.FromSeconds(5));
Like the file-system notifications, each method returns an IAwaitableCallback<T> you dispose to unsubscribe and Wait/WaitAsync to block until enough events arrive.
Available callbacks
Method on On | Fires when | T |
|---|---|---|
DateTimeRead() | DateTime.Now, UtcNow or Today is read | DateTime |
TaskDelay() | Task.Delay (any overload) is called | TimeSpan |
ThreadSleep() | Thread.Sleep (any overload) is called | TimeSpan |
TimeChanged() | the simulated time advanced (auto-advance or manual) | DateTime |
PeriodicTimer.WaitingForNextTick() | a PeriodicTimer is waiting for its next tick | IPeriodicTimer |
Each accepts an optional callback (run synchronously when the event fires) and an optional predicate to filter events before they are recorded.
// Only record sleeps longer than 1 second
using var longSleeps = timeSystem.On.ThreadSleep(
predicate: ts => ts > TimeSpan.FromSeconds(1));