Skip to main content

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 OnFires whenT
DateTimeRead()DateTime.Now, UtcNow or Today is readDateTime
TaskDelay()Task.Delay (any overload) is calledTimeSpan
ThreadSleep()Thread.Sleep (any overload) is calledTimeSpan
TimeChanged()the simulated time advanced (auto-advance or manual)DateTime
PeriodicTimer.*a PeriodicTimer was constructed or tickedvarious

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));