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 warning CA1062#AsyncTimeoutPolicy #2228

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
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