Skip to content

Commit

Permalink
Fix FakeTimeProvider namespace, add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Oct 9, 2023
1 parent a959ae4 commit 3e4e2fc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
using System.Threading;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.Time.Testing;
namespace Microsoft.Extensions.TimeProvider.Testing;

/// <summary>
/// Represents a synthetic time provider that can be used to enable deterministic behavior in tests.
/// </summary>
public class FakeTimeProvider : TimeProvider
public class FakeTimeProvider : System.TimeProvider
{
internal readonly HashSet<Waiter> Waiters = new();
private DateTimeOffset _now = new(2000, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
Expand Down Expand Up @@ -111,7 +111,7 @@ public void SetUtcNow(DateTimeOffset value)
/// <param name="delta">The amount of time to advance the clock by.</param>
/// <remarks>
/// Advancing time affects the timers created from this provider, and all other operations that are directly or
/// indirectly using this provider as a time source. Whereas when using <see cref="TimeProvider.System"/>, time
/// indirectly using this provider as a time source. Whereas when using <see cref="System.TimeProvider.System"/>, time
/// marches forward automatically in hardware, for the fake time provider the application is responsible for
/// doing this explicitly by calling this method.
/// </remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,64 @@
"Name": "Microsoft.Extensions.TimeProvider.Testing, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
"Types": [
{
"Type": "class Microsoft.Extensions.Time.Testing.FakeTimeProvider : System.TimeProvider",
"Type": "class Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider : System.TimeProvider",
"Stage": "Stable",
"Methods": [
{
"Member": "Microsoft.Extensions.Time.Testing.FakeTimeProvider.FakeTimeProvider();",
"Member": "Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.FakeTimeProvider();",
"Stage": "Stable"
},
{
"Member": "Microsoft.Extensions.Time.Testing.FakeTimeProvider.FakeTimeProvider(System.DateTimeOffset startDateTime);",
"Member": "Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.FakeTimeProvider(System.DateTimeOffset startDateTime);",
"Stage": "Stable"
},
{
"Member": "void Microsoft.Extensions.Time.Testing.FakeTimeProvider.Advance(System.TimeSpan delta);",
"Member": "void Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.Advance(System.TimeSpan delta);",
"Stage": "Stable"
},
{
"Member": "override System.Threading.ITimer Microsoft.Extensions.Time.Testing.FakeTimeProvider.CreateTimer(System.Threading.TimerCallback callback, object? state, System.TimeSpan dueTime, System.TimeSpan period);",
"Member": "override System.Threading.ITimer Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.CreateTimer(System.Threading.TimerCallback callback, object? state, System.TimeSpan dueTime, System.TimeSpan period);",
"Stage": "Stable"
},
{
"Member": "override long Microsoft.Extensions.Time.Testing.FakeTimeProvider.GetTimestamp();",
"Member": "override long Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.GetTimestamp();",
"Stage": "Stable"
},
{
"Member": "override System.DateTimeOffset Microsoft.Extensions.Time.Testing.FakeTimeProvider.GetUtcNow();",
"Member": "override System.DateTimeOffset Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.GetUtcNow();",
"Stage": "Stable"
},
{
"Member": "void Microsoft.Extensions.Time.Testing.FakeTimeProvider.SetLocalTimeZone(System.TimeZoneInfo localTimeZone);",
"Member": "void Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.SetLocalTimeZone(System.TimeZoneInfo localTimeZone);",
"Stage": "Stable"
},
{
"Member": "void Microsoft.Extensions.Time.Testing.FakeTimeProvider.SetUtcNow(System.DateTimeOffset value);",
"Member": "void Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.SetUtcNow(System.DateTimeOffset value);",
"Stage": "Stable"
},
{
"Member": "override string Microsoft.Extensions.Time.Testing.FakeTimeProvider.ToString();",
"Member": "override string Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.ToString();",
"Stage": "Stable"
}
],
"Properties": [
{
"Member": "System.TimeSpan Microsoft.Extensions.Time.Testing.FakeTimeProvider.AutoAdvanceAmount { get; set; }",
"Member": "System.TimeSpan Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.AutoAdvanceAmount { get; set; }",
"Stage": "Stable"
},
{
"Member": "override System.TimeZoneInfo Microsoft.Extensions.Time.Testing.FakeTimeProvider.LocalTimeZone { get; }",
"Member": "override System.TimeZoneInfo Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.LocalTimeZone { get; }",
"Stage": "Stable"
},
{
"Member": "System.DateTimeOffset Microsoft.Extensions.Time.Testing.FakeTimeProvider.Start { get; }",
"Member": "System.DateTimeOffset Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.Start { get; }",
"Stage": "Stable"
},
{
"Member": "override long Microsoft.Extensions.Time.Testing.FakeTimeProvider.TimestampFrequency { get; }",
"Member": "override long Microsoft.Extensions.TimeProvider.Testing.FakeTimeProvider.TimestampFrequency { get; }",
"Stage": "Stable"
}
]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
README
# Microsoft.Extensions.TimeProvider.Testing

Provides a `FakeTimeProvider` for testing components that depend on `System.TimeProvider`.

## Install the package

From the command-line:

```dotnetcli
dotnet add package Microsoft.Extensions.TimeProvider.Testing
```

Or directly in the C# project file:

```xml
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="[CURRENTVERSION]" />
</ItemGroup>
```

## FakeTimeProvider

`FakeTimeProvider` can be used to manually adjust time to test time dependent components in a deterministic way.

`FakeTimeProvider` derives from TimeProvider and adds the following APIs:

```csharp
public FakeTimeProvider(DateTimeOffset startDateTime)
public DateTimeOffset Start { get; }
public TimeSpan AutoAdvanceAmount
public void SetUtcNow(DateTimeOffset value)
public void Advance(TimeSpan delta)
public void SetLocalTimeZone(TimeZoneInfo localTimeZone)
```

For example:

```csharp
var timeProvider = new FakeTimeProvider();
var myComponent = new MyComponent(timeProvider);
timeProvider.Advance(TimeSpan.FromSeconds(5));
myComponent.CheckState();
```

## Feedback & Contributing

For any feedback or contributions, please visit us in [our GitHub repo](https://github.com/dotnet/extensions).
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Threading.Tasks;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.Time.Testing;
namespace Microsoft.Extensions.TimeProvider.Testing;

// This implements the timer abstractions and is a thin wrapper around a waiter object.
// The main role of this type is to create the waiter, add it to the waiter list, and ensure it gets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Threading;

namespace Microsoft.Extensions.Time.Testing;
namespace Microsoft.Extensions.TimeProvider.Testing;

// We keep all timer state here in order to prevent Timer instances from being self-referential,
// which would block them being collected when someone forgets to call Dispose on the timer. With
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
using Microsoft.Extensions.Http.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Time.Testing;
using Microsoft.Extensions.TimeProvider.Testing;
using Microsoft.Net.Http.Headers;
using Microsoft.Shared.Text;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Compliance.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Time.Testing;
using Microsoft.Extensions.TimeProvider.Testing;

namespace Microsoft.AspNetCore.Diagnostics.Logging.Test.Controllers;

Expand Down

0 comments on commit 3e4e2fc

Please sign in to comment.