Skip to content

Commit

Permalink
Fix CA1062 warnings (#2228)
Browse files Browse the repository at this point in the history
Fix warnings for `AsyncTimeoutPolicy`.
  • Loading branch information
Zombach authored Jul 22, 2024
1 parent 911be28 commit 72331dc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Polly/Timeout/AsyncTimeoutPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// A timeout policy which can be applied to async delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class AsyncTimeoutPolicy : AsyncPolicy, ITimeoutPolicy
{
private readonly Func<Context, TimeSpan> _timeoutProvider;
Expand All @@ -26,15 +25,22 @@ protected override Task<TResult> ImplementationAsync<TResult>(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
CancellationToken cancellationToken,
bool continueOnCapturedContext) =>
AsyncTimeoutEngine.ImplementationAsync(
bool continueOnCapturedContext)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return AsyncTimeoutEngine.ImplementationAsync(
action,
context,
_timeoutProvider,
_timeoutStrategy,
_onTimeoutAsync,
continueOnCapturedContext,
cancellationToken);
}
}

/// <summary>
Expand Down Expand Up @@ -63,13 +69,20 @@ protected override Task<TResult> ImplementationAsync(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
CancellationToken cancellationToken,
bool continueOnCapturedContext) =>
AsyncTimeoutEngine.ImplementationAsync(
bool continueOnCapturedContext)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return AsyncTimeoutEngine.ImplementationAsync(
action,
context,
_timeoutProvider,
_timeoutStrategy,
_onTimeoutAsync,
continueOnCapturedContext,
cancellationToken);
}
}
28 changes: 28 additions & 0 deletions test/Polly.Specs/Timeout/TimeoutAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ public class TimeoutAsyncSpecs : TimeoutSpecsBase
{
#region Configuration

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, Task<EmptyStruct>> action = null!;
Func<Context, TimeSpan> timeoutProvider = (_) => TimeSpan.Zero;
TimeoutStrategy timeoutStrategy = TimeoutStrategy.Optimistic;
Func<Context, TimeSpan, Task, Exception, Task> onTimeoutAsync = (_, _, _, _) => Task.CompletedTask;

var instance = Activator.CreateInstance(
typeof(AsyncTimeoutPolicy),
flags,
null,
[timeoutProvider, timeoutStrategy, onTimeoutAsync],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "ImplementationAsync", ReturnType.Name: "Task`1" });
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));

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

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_timeout_is_zero_by_timespan()
{
Expand Down
27 changes: 27 additions & 0 deletions test/Polly.Specs/Timeout/TimeoutTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ public class TimeoutTResultAsyncSpecs : TimeoutSpecsBase
{
#region Configuration

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, Task<EmptyStruct>> action = null!;
Func<Context, TimeSpan> timeoutProvider = (_) => TimeSpan.Zero;
TimeoutStrategy timeoutStrategy = TimeoutStrategy.Optimistic;
Func<Context, TimeSpan, Task, Exception, Task> onTimeoutAsync = (_, _, _, _) => Task.CompletedTask;

var instance = Activator.CreateInstance(
typeof(AsyncTimeoutPolicy<EmptyStruct>),
flags,
null,
[timeoutProvider, timeoutStrategy, onTimeoutAsync],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "ImplementationAsync", ReturnType.Name: "Task`1" });

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

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_timeout_is_zero_by_timespan()
{
Expand Down

0 comments on commit 72331dc

Please sign in to comment.