Skip to content

Commit

Permalink
Fix DelayAsync extension (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Aug 30, 2023
1 parent 3dd6a7a commit 58c8fac
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Polly.Core/DelayBackoffType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Polly.Retry;
namespace Polly;

/// <summary>
/// The backoff type used by the strategies.
Expand Down
10 changes: 5 additions & 5 deletions src/Polly.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Polly.CircuitBreaker.OnCircuitOpenedArguments<TResult>.OnCircuitOpenedArguments(
Polly.CircuitBreaker.OnCircuitOpenedArguments<TResult>.OnCircuitOpenedArguments(Polly.ResilienceContext! context, Polly.Outcome<TResult> outcome, System.TimeSpan breakDuration, bool isManual) -> void
Polly.CircuitBreaker.OnCircuitOpenedArguments<TResult>.Outcome.get -> Polly.Outcome<TResult>
Polly.CircuitBreakerResiliencePipelineBuilderExtensions
Polly.DelayBackoffType
Polly.DelayBackoffType.Constant = 0 -> Polly.DelayBackoffType
Polly.DelayBackoffType.Exponential = 2 -> Polly.DelayBackoffType
Polly.DelayBackoffType.Linear = 1 -> Polly.DelayBackoffType
Polly.ExecutionRejectedException
Polly.ExecutionRejectedException.ExecutionRejectedException() -> void
Polly.ExecutionRejectedException.ExecutionRejectedException(string! message) -> void
Expand Down Expand Up @@ -281,10 +285,6 @@ Polly.ResilienceStrategyOptions
Polly.ResilienceStrategyOptions.Name.get -> string?
Polly.ResilienceStrategyOptions.Name.set -> void
Polly.ResilienceStrategyOptions.ResilienceStrategyOptions() -> void
Polly.Retry.DelayBackoffType
Polly.Retry.DelayBackoffType.Constant = 0 -> Polly.Retry.DelayBackoffType
Polly.Retry.DelayBackoffType.Exponential = 2 -> Polly.Retry.DelayBackoffType
Polly.Retry.DelayBackoffType.Linear = 1 -> Polly.Retry.DelayBackoffType
Polly.Retry.OnRetryArguments<TResult>
Polly.Retry.OnRetryArguments<TResult>.AttemptNumber.get -> int
Polly.Retry.OnRetryArguments<TResult>.Context.get -> Polly.ResilienceContext!
Expand All @@ -309,7 +309,7 @@ Polly.Retry.RetryPredicateArguments<TResult>.RetryPredicateArguments(Polly.Resil
Polly.Retry.RetryStrategyOptions
Polly.Retry.RetryStrategyOptions.RetryStrategyOptions() -> void
Polly.Retry.RetryStrategyOptions<TResult>
Polly.Retry.RetryStrategyOptions<TResult>.BackoffType.get -> Polly.Retry.DelayBackoffType
Polly.Retry.RetryStrategyOptions<TResult>.BackoffType.get -> Polly.DelayBackoffType
Polly.Retry.RetryStrategyOptions<TResult>.BackoffType.set -> void
Polly.Retry.RetryStrategyOptions<TResult>.Delay.get -> System.TimeSpan
Polly.Retry.RetryStrategyOptions<TResult>.Delay.set -> void
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core/Retry/RetryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static TimeSpan GetRetryDelay(DelayBackoffType type, bool jitter, int att
}
}

private static TimeSpan GetRetryDelayCore(RetryBackoffType type, bool jitter, int attempt, TimeSpan baseDelay, ref double state, Func<double> randomizer)
private static TimeSpan GetRetryDelayCore(DelayBackoffType type, bool jitter, int attempt, TimeSpan baseDelay, ref double state, Func<double> randomizer)
{
if (baseDelay == TimeSpan.Zero)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Polly.Core/Utils/TimeProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static Task DelayAsync(this TimeProvider timeProvider, TimeSpan delay, Re

context.CancellationToken.ThrowIfCancellationRequested();

if (delay == TimeSpan.MaxValue)
{
delay = System.Threading.Timeout.InfiniteTimeSpan;
}

if (context.IsSynchronous)
{
#pragma warning disable CA1849
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/Retry/RetryHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void GetRetryDelay_Overflow_ReturnsMaxTimeSpan()
{
double state = 0;

RetryHelper.GetRetryDelay(RetryBackoffType.Exponential, false, 1000, TimeSpan.FromDays(1), ref state, _randomizer).Should().Be(TimeSpan.MaxValue);
RetryHelper.GetRetryDelay(DelayBackoffType.Exponential, false, 1000, TimeSpan.FromDays(1), ref state, _randomizer).Should().Be(TimeSpan.MaxValue);
}

[InlineData(1)]
Expand Down
22 changes: 22 additions & 0 deletions test/Polly.Core.Tests/Utils/TimeProviderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,26 @@ await TestUtilities.AssertWithTimeoutAsync(async () =>
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => timeProvider.DelayAsync(delay, context));
});
}

[Fact]
public async Task DelayAsync_MaxTimeSpan_DoesNotThrow()
{
var delay = TimeSpan.MaxValue;

await TestUtilities.AssertWithTimeoutAsync(async () =>
{
using var cancellation = new CancellationTokenSource();
var timeProvider = TimeProvider.System;
var context = ResilienceContextPool.Shared.Get();
context.Initialize<VoidResult>(isSynchronous: false);
context.CancellationToken = cancellation.Token;
var delayTask = timeProvider.DelayAsync(delay, context);
delayTask.Wait(TimeSpan.FromMilliseconds(10)).Should().BeFalse();
cancellation.Cancel();
await delayTask.Invoking(t => t).Should().ThrowAsync<OperationCanceledException>();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Options;
using Polly.DependencyInjection;
using Polly.Registry;
using Polly.Retry;
using Polly.Timeout;

namespace Polly.Extensions.Tests.Issues;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Polly;
using Polly.TestUtils;

namespace Polly.Specs;
Expand Down

0 comments on commit 58c8fac

Please sign in to comment.