Skip to content

Commit d19069a

Browse files
Fix flaky tests (#2457)
Attempt to fix flaky tests.
1 parent dc4010a commit d19069a

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

test/Polly.Core.Tests/Issues/IssuesTests.InfiniteRetry_2163.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Polly.Core.Tests.Issues;
55

66
public partial class IssuesTests
77
{
8-
[Fact(Timeout = 15_000)]
8+
[Fact(Timeout = 30_000)]
99
public async Task InfiniteRetry_Delay_Does_Not_Overflow_2163()
1010
{
1111
// Arrange
@@ -35,7 +35,8 @@ public async Task InfiniteRetry_Delay_Does_Not_Overflow_2163()
3535
var strategy = new RetryResilienceStrategy<bool>(options, timeProvider, telemetry);
3636
var pipeline = strategy.AsPipeline();
3737

38-
using var cts = new CancellationTokenSource(Debugger.IsAttached ? TimeSpan.MaxValue : TimeSpan.FromSeconds(10));
38+
using var cts = new CancellationTokenSource(
39+
Debugger.IsAttached ? TimeSpan.MaxValue : TimeSpan.FromSeconds(20));
3940

4041
// Act
4142
var executing = pipeline.ExecuteAsync((_) =>

test/Polly.Specs/Timeout/TimeoutSpecsBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Polly.Specs.Timeout;
88
/// For tests, rather than letting .NET's timers drive the timing out of CancellationTokens, we override SystemClock.CancelTokenAfter and SystemClock.Sleep to make the tests run fast.
99
/// </remarks>
1010
/// </summary>
11+
[Collection(Constants.SystemClockDependentTestCollection)]
1112
public abstract class TimeoutSpecsBase : IDisposable
1213
{
1314
// xUnit creates a new class instance per test, so these variables are isolated per test.

test/Polly.Specs/Utilities/SystemClockSpecs.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
namespace Polly.Specs.Utilities;
22

3+
[Collection(Constants.SystemClockDependentTestCollection)]
34
public class SystemClockSpecs
45
{
6+
private readonly Action<TimeSpan, CancellationToken> _sleep;
7+
private readonly Func<TimeSpan, CancellationToken, Task> _sleepAsync;
8+
9+
public SystemClockSpecs()
10+
{
11+
SystemClock.Reset();
12+
_sleep = SystemClock.Sleep;
13+
_sleepAsync = SystemClock.SleepAsync;
14+
}
15+
516
[Fact]
617
public void Sleep_ShouldNotThrow_WhenCancellationNotRequested() =>
7-
SystemClock.Sleep.Invoking(s =>
18+
_sleep.Invoking(s =>
819
{
920
using var cts = new CancellationTokenSource();
1021
s(TimeSpan.FromMilliseconds(1), cts.Token);
1122
}).Should().NotThrow();
1223

1324
[Fact]
1425
public void Sleep_ShouldThrow_WhenCancellationRequested() =>
15-
SystemClock.Sleep.Invoking(s =>
26+
_sleep.Invoking(s =>
1627
{
1728
using var cts = new CancellationTokenSource();
1829
cts.Cancel();
@@ -21,15 +32,15 @@ public void Sleep_ShouldThrow_WhenCancellationRequested() =>
2132

2233
[Fact]
2334
public async Task SleepAsync_ShouldNotThrow_WhenCancellationNotRequested() =>
24-
await SystemClock.SleepAsync.Invoking(async s =>
35+
await _sleepAsync.Invoking(async s =>
2536
{
2637
using var cts = new CancellationTokenSource();
2738
await s(TimeSpan.FromMilliseconds(1), cts.Token);
2839
}).Should().NotThrowAsync();
2940

3041
[Fact]
3142
public async Task SleepAsync_ShouldThrow_WhenCancellationRequested() =>
32-
await SystemClock.SleepAsync.Invoking(async s =>
43+
await _sleepAsync.Invoking(async s =>
3344
{
3445
using var cts = new CancellationTokenSource();
3546
cts.Cancel();

0 commit comments

Comments
 (0)