ITimeSystem
ITimeSystem wraps everything in the BCL that depends on the wall clock or makes a thread wait. Replace it with a MockTimeSystem in tests and you can fast-forward time without sleeping.
What it covers
| Sub-property | Wraps |
|---|---|
DateTime | DateTime.Now, DateTime.UtcNow, DateTime.Today (plus MinValue, MaxValue, UnixEpoch) |
Stopwatch | System.Diagnostics.Stopwatch |
Task | Task.Delay |
Thread | Thread.Sleep |
PeriodicTimer | System.Threading.PeriodicTimer |
Timer | System.Threading.Timer |
Implementations
| Type | Package | Use it for |
|---|---|---|
RealTimeSystem | Testably.Abstractions | Production |
MockTimeSystem | Testably.Abstractions.Testing | Unit tests |
ITimeSystem timeSystem = new MockTimeSystem();
// any Thread.Sleep / Task.Delay / Stopwatch / Timer call goes through this instance
timeSystem.Thread.Sleep(TimeSpan.FromMinutes(5));
DateTime now = timeSystem.DateTime.Now; // advanced by 5 minutes
The remaining pages cover how to configure the underlying time provider, control auto-advance, drive timers and periodic timers, and react to time-related events.