Skip to content
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
17 changes: 14 additions & 3 deletions src/Polly/Retry/RetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.Retry;
/// <summary>
/// A retry policy that can be applied to synchronous delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class RetryPolicy : Policy, IRetryPolicy
{
private readonly Action<Exception, TimeSpan, int, Context> _onRetry;
Expand All @@ -29,6 +28,11 @@ internal RetryPolicy(
/// <inheritdoc/>
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

var sleepDurationProvider = _sleepDurationProvider != null
? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx)
: (Func<int, DelegateResult<TResult>, Context, TimeSpan>?)null;
Expand Down Expand Up @@ -72,8 +76,14 @@ internal RetryPolicy(

/// <inheritdoc/>
[DebuggerStepThrough]
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
RetryEngine.Implementation(
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return RetryEngine.Implementation(
action,
context,
ExceptionPredicates,
Expand All @@ -83,4 +93,5 @@ protected override TResult Implementation(Func<Context, CancellationToken, TResu
_permittedRetryCount,
_sleepDurationsEnumerable,
_sleepDurationProvider);
}
}
30 changes: 30 additions & 0 deletions test/Polly.Specs/Retry/RetrySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

public class RetrySpecs
{
[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
var policyBuilder = new PolicyBuilder(exception => exception);
Action<Exception, TimeSpan, int, Context> onRetry = (_, _, _, _) => { };
int permittedRetryCount = int.MaxValue;
IEnumerable<TimeSpan>? sleepDurationsEnumerable = null;
Func<int, Exception, Context, TimeSpan>? sleepDurationProvider = null;

var instance = Activator.CreateInstance(
typeof(RetryPolicy),
flags,
null,
[policyBuilder, onRetry, permittedRetryCount, sleepDurationsEnumerable, sleepDurationProvider],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "TResult" });
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));

var func = () => generic.Invoke(instance, [action, new Context(), CancellationToken.None]);

var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
.Which.ParamName.Should().Be("action");
}

[Fact]
public void Should_throw_when_retry_count_is_less_than_zero_without_context()
{
Expand Down
29 changes: 29 additions & 0 deletions test/Polly.Specs/Retry/RetryTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@ namespace Polly.Specs.Retry;

public class RetryTResultSpecs
{
[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
var policyBuilder = new PolicyBuilder<EmptyStruct>(exception => exception);
Action<DelegateResult<EmptyStruct>, TimeSpan, int, Context> onRetry = (_, _, _, _) => { };
int permittedRetryCount = int.MaxValue;
IEnumerable<TimeSpan>? sleepDurationsEnumerable = null;
Func<int, DelegateResult<EmptyStruct>, Context, TimeSpan>? sleepDurationProvider = null;

var instance = Activator.CreateInstance(
typeof(RetryPolicy<EmptyStruct>),
flags,
null,
[policyBuilder, onRetry, permittedRetryCount, sleepDurationsEnumerable, sleepDurationProvider],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" });

var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]);

var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
.Which.ParamName.Should().Be("action");
}

[Fact]
public void Should_throw_when_retry_count_is_less_than_zero_without_context()
{
Expand Down