|
| 1 | +[](https://github.com/egil/TimeProviderExtensions/releases) |
| 2 | +[](https://www.nuget.org/packages/TimeProviderExtensions/) |
| 3 | +[](https://github.com/egil/TimeProviderExtensions/issues) |
| 4 | + |
1 | 5 | # TimeProvider Extensions |
2 | 6 |
|
3 | | -Extensions for [`System.TimeProvider`](https://learn.microsoft.com/en-us/dotnet/api/system.timeprovider) API. It includes a version of the `TimeProvider` type, named `ManualTimeProvider`, that allows you to control the progress of time during testing deterministically. |
| 7 | +Extensions for the [`System.TimeProvider`](https://learn.microsoft.com/en-us/dotnet/api/system.timeprovider) API. It includes: |
| 8 | + |
| 9 | +- A test/fake version of `TimeProvider` type, named `ManualTimeProvider`, that allows you to control the progress of time during testing deterministically. |
| 10 | +- A backported version of `PeriodicTimer` that supports `TimeProvider` in .NET 6. |
| 11 | + |
| 12 | +## Quick start |
| 13 | + |
| 14 | +This describes how to get started: |
| 15 | + |
| 16 | +1. Get the latest release from https://www.nuget.org/packages/TimeProviderExtensions. |
| 17 | + |
| 18 | +2. Take a dependency on `TimeProvider` in your code. Inject the production version of `TimeProvider` available via the [`TimeProvider.System`](https://learn.microsoft.com/en-us/dotnet/api/system.timeprovider.system?#system-timeprovider-system) property during production. |
| 19 | + |
| 20 | +3. During testing, inject the `ManualTimeProvider` from this library. With `ManualTimeProvider`, you can move advance time by calling `Advance(TimeSpan)` or `SetUtcNow(DateTimeOffset)` and jump ahead in time using `Jump(TimeSpan)` or `Jump(DateTimeOffset)`. This allows you to write tests that run fast and predictably, even if the system under test pauses execution for multiple minutes using e.g. `TimeProvider.Delay(TimeSpan)`, the replacement for `Task.Delay(TimeSpan)`. |
| 21 | + |
| 22 | +Read the rest of this README for further details and examples. |
4 | 23 |
|
5 | | -An instance of `TimeProvider` for production use is available on the [`TimeProvider.System`](https://learn.microsoft.com/en-us/dotnet/api/system.timeprovider.system?#system-timeprovider-system) property, and `ManualTimeProvider` can be used during testing. |
| 24 | +## ManualTimeProvider API |
6 | 25 |
|
7 | | -During testing, you can move time forward by calling `Advance(TimeSpan)` or `SetUtcNow(DateTimeOffset)` on `ManualTimeProvider`. This allows you to write tests that run fast and predictably, even if the system under test pauses execution for multiple minutes using e.g. `TimeProvider.Delay(TimeSpan)`, the replacement for `Task.Delay(TimeSpan)`. |
| 26 | +The ManualTimeProvider represents a synthetic time provider that can be used to enable deterministic behavior in tests. |
8 | 27 |
|
9 | 28 | ## Difference between `ManualTimeProvider` and `FakeTimeProvider` |
10 | 29 |
|
@@ -49,11 +68,11 @@ To support testing this scenario, `ManualtTimeProvider` includes a method that w |
49 | 68 | fixture.Customize<ManualTimeProvider>(x => x.With(tp => tp.AutoAdvanceAmount, TimeSpan.Zero)); |
50 | 69 | ``` |
51 | 70 |
|
52 | | -## Installation |
| 71 | +## Installation and Usage |
53 | 72 |
|
54 | 73 | Get the latest release from https://www.nuget.org/packages/TimeProviderExtensions |
55 | 74 |
|
56 | | -## Set up in production |
| 75 | +### Set up in production |
57 | 76 |
|
58 | 77 | To use in production, pass in `TimeProvider.System` to the types that depend on `TimeProvider`. |
59 | 78 | This can be done directly or via an IoC Container, e.g. .NETs built-in `IServiceCollection` like so: |
@@ -84,7 +103,7 @@ public class MyService |
84 | 103 |
|
85 | 104 | This allows you to explicitly pass in a `ManualTimeProvider` during testing. |
86 | 105 |
|
87 | | -## Example - control time during tests |
| 106 | +### Example - control time during tests |
88 | 107 |
|
89 | 108 | If a system under test (SUT) uses things like `Task.Delay`, `DateTimeOffset.UtcNow`, `Task.WaitAsync`, or `PeriodicTimer`, |
90 | 109 | it becomes hard to create tests that run fast and predictably. |
|
0 commit comments