-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b954f1
commit 540e308
Showing
6 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Polly.Specs.Utilities; | ||
|
||
public class SystemClockSpecs | ||
{ | ||
[Fact] | ||
public void Sleep_ShouldNotThrow_WhenCancellationNotRequested() => | ||
SystemClock.Sleep.Invoking(s => | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
s(TimeSpan.FromMilliseconds(1), cts.Token); | ||
}).Should().NotThrow(); | ||
|
||
[Fact] | ||
public void Sleep_ShouldThrow_WhenCancellationRequested() => | ||
SystemClock.Sleep.Invoking(s => | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
cts.Cancel(); | ||
s(TimeSpan.FromMilliseconds(1), cts.Token); | ||
}).Should().Throw<OperationCanceledException>(); | ||
|
||
[Fact] | ||
public async Task SleepAsync_ShouldNotThrow_WhenCancellationNotRequested() => | ||
await SystemClock.SleepAsync.Invoking(async s => | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
await s(TimeSpan.FromMilliseconds(1), cts.Token); | ||
}).Should().NotThrowAsync(); | ||
|
||
[Fact] | ||
public async Task SleepAsync_ShouldThrow_WhenCancellationRequested() => | ||
await SystemClock.SleepAsync.Invoking(async s => | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
cts.Cancel(); | ||
await s(TimeSpan.FromMilliseconds(1), cts.Token); | ||
}).Should().ThrowAsync<OperationCanceledException>(); | ||
|
||
[Fact] | ||
public void Reset_ShouldResetToDefaultImplementations() | ||
{ | ||
SystemClock.Sleep = (_, _) => { }; | ||
SystemClock.SleepAsync = (_, _) => Task.CompletedTask; | ||
SystemClock.UtcNow = () => new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); | ||
SystemClock.DateTimeOffsetUtcNow = () => new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); | ||
SystemClock.CancelTokenAfter = (_, _) => { }; | ||
|
||
SystemClock.Reset(); | ||
|
||
SystemClock.UtcNow().Should().NotBe(new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); | ||
SystemClock.DateTimeOffsetUtcNow().Should().NotBe(new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc))); | ||
|
||
SystemClock.CancelTokenAfter.Invoking(s => | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
s(cts, TimeSpan.FromMilliseconds(1)); | ||
Task.Delay(10).Wait(); | ||
cts.Token.IsCancellationRequested.Should().BeTrue(); | ||
}).Should().NotThrow(); | ||
} | ||
} |