Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky test #2457

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Polly.Core.Tests.Issues;

public partial class IssuesTests
{
[Fact(Timeout = 15_000)]
[Fact(Timeout = 30_000)]
public async Task InfiniteRetry_Delay_Does_Not_Overflow_2163()
{
// Arrange
Expand Down Expand Up @@ -35,7 +35,8 @@ public async Task InfiniteRetry_Delay_Does_Not_Overflow_2163()
var strategy = new RetryResilienceStrategy<bool>(options, timeProvider, telemetry);
var pipeline = strategy.AsPipeline();

using var cts = new CancellationTokenSource(Debugger.IsAttached ? TimeSpan.MaxValue : TimeSpan.FromSeconds(10));
using var cts = new CancellationTokenSource(
Debugger.IsAttached ? TimeSpan.MaxValue : TimeSpan.FromSeconds(20));

// Act
var executing = pipeline.ExecuteAsync((_) =>
Expand Down
1 change: 1 addition & 0 deletions test/Polly.Specs/Timeout/TimeoutSpecsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Polly.Specs.Timeout;
/// 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.
/// </remarks>
/// </summary>
[Collection(Constants.SystemClockDependentTestCollection)]
public abstract class TimeoutSpecsBase : IDisposable
{
// xUnit creates a new class instance per test, so these variables are isolated per test.
Expand Down
19 changes: 15 additions & 4 deletions test/Polly.Specs/Utilities/SystemClockSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
namespace Polly.Specs.Utilities;

[Collection(Constants.SystemClockDependentTestCollection)]
public class SystemClockSpecs
{
private readonly Action<TimeSpan, CancellationToken> _sleep;
private readonly Func<TimeSpan, CancellationToken, Task> _sleepAsync;

public SystemClockSpecs()
{
SystemClock.Reset();
_sleep = SystemClock.Sleep;
_sleepAsync = SystemClock.SleepAsync;
}

[Fact]
public void Sleep_ShouldNotThrow_WhenCancellationNotRequested() =>
SystemClock.Sleep.Invoking(s =>
_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 =>
_sleep.Invoking(s =>
{
using var cts = new CancellationTokenSource();
cts.Cancel();
Expand All @@ -21,15 +32,15 @@ public void Sleep_ShouldThrow_WhenCancellationRequested() =>

[Fact]
public async Task SleepAsync_ShouldNotThrow_WhenCancellationNotRequested() =>
await SystemClock.SleepAsync.Invoking(async s =>
await _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 =>
await _sleepAsync.Invoking(async s =>
{
using var cts = new CancellationTokenSource();
cts.Cancel();
Expand Down
Loading