Skip to content

Commit

Permalink
Fix CA1062 warnings (#2220)
Browse files Browse the repository at this point in the history
Fix warnings for `AsyncNoOpPolicy`.
  • Loading branch information
Zombach authored Jul 21, 2024
1 parent 87cfed7 commit 69e0e80
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Polly/NoOp/AsyncNoOpPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.NoOp;
/// <summary>
/// A noop policy that can be applied to asynchronous delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class AsyncNoOpPolicy : AsyncPolicy, INoOpPolicy
{
internal AsyncNoOpPolicy()
Expand All @@ -14,8 +13,15 @@ internal AsyncNoOpPolicy()
/// <inheritdoc/>
[DebuggerStepThrough]
protected override Task<TResult> ImplementationAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext) =>
NoOpEngine.ImplementationAsync(action, context, cancellationToken);
bool continueOnCapturedContext)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return NoOpEngine.ImplementationAsync(action, context, cancellationToken);
}
}

/// <summary>
Expand All @@ -31,6 +37,13 @@ internal AsyncNoOpPolicy()
/// <inheritdoc/>
[DebuggerStepThrough]
protected override Task<TResult> ImplementationAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext) =>
NoOpEngine.ImplementationAsync(action, context, cancellationToken);
bool continueOnCapturedContext)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return NoOpEngine.ImplementationAsync(action, context, cancellationToken);
}
}
19 changes: 19 additions & 0 deletions test/Polly.Specs/NoOp/NoOpAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

public class NoOpAsyncSpecs
{
[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, Task<EmptyStruct>> action = null!;

var instance = Activator.CreateInstance(typeof(AsyncNoOpPolicy), true)!;
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.WithInnerException<ArgumentNullException>("action");
}

[Fact]
public async Task Should_execute_user_delegate()
{
Expand Down
18 changes: 18 additions & 0 deletions test/Polly.Specs/NoOp/NoOpTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

public class NoOpTResultAsyncSpecs
{
[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, Task<EmptyStruct>> action = null!;

var instance = Activator.CreateInstance(typeof(AsyncNoOpPolicy<EmptyStruct>), true)!;
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.WithInnerException<ArgumentNullException>("action");
}

[Fact]
public async Task Should_execute_user_delegate()
{
Expand Down

0 comments on commit 69e0e80

Please sign in to comment.