Skip to content

Commit

Permalink
Fix CA1062 warnings (#2219)
Browse files Browse the repository at this point in the history
Fix CA1062 warnings for `NoOpPolicy`.
  • Loading branch information
Zombach authored Jul 21, 2024
1 parent 994193e commit 87cfed7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Polly/NoOp/NoOpPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.NoOp;
/// <summary>
/// A no op policy that can be applied to delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class NoOpPolicy : Policy, INoOpPolicy
{
internal NoOpPolicy()
Expand All @@ -13,8 +12,15 @@ internal NoOpPolicy()

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

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

/// <summary>
Expand All @@ -29,6 +35,13 @@ internal NoOpPolicy()

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

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

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

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

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

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

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

[Fact]
public void Should_execute_user_delegate()
{
Expand Down

0 comments on commit 87cfed7

Please sign in to comment.