diff --git a/src/Polly/NoOp/NoOpPolicy.cs b/src/Polly/NoOp/NoOpPolicy.cs index 6e97f78e1a1..da034169879 100644 --- a/src/Polly/NoOp/NoOpPolicy.cs +++ b/src/Polly/NoOp/NoOpPolicy.cs @@ -4,7 +4,6 @@ namespace Polly.NoOp; /// /// A no op policy that can be applied to delegates. /// -#pragma warning disable CA1062 // Validate arguments of public methods public class NoOpPolicy : Policy, INoOpPolicy { internal NoOpPolicy() @@ -13,8 +12,15 @@ internal NoOpPolicy() /// [DebuggerStepThrough] - protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) => - NoOpEngine.Implementation(action, context, cancellationToken); + protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) + { + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + + return NoOpEngine.Implementation(action, context, cancellationToken); + } } /// @@ -29,6 +35,13 @@ internal NoOpPolicy() /// [DebuggerStepThrough] - protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) => - NoOpEngine.Implementation(action, context, cancellationToken); + protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) + { + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + + return NoOpEngine.Implementation(action, context, cancellationToken); + } } diff --git a/test/Polly.Specs/NoOp/NoOpSpecs.cs b/test/Polly.Specs/NoOp/NoOpSpecs.cs index 9534ff02192..6ff214de8c7 100644 --- a/test/Polly.Specs/NoOp/NoOpSpecs.cs +++ b/test/Polly.Specs/NoOp/NoOpSpecs.cs @@ -2,6 +2,25 @@ public class NoOpSpecs { + [Fact] + public void Should_throw_when_action_is_null() + { + var flags = BindingFlags.NonPublic | BindingFlags.Instance; + Func 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(); + exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation."); + exceptionAssertions.WithInnerException("action"); + } + [Fact] public void Should_execute_user_delegate() { diff --git a/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs b/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs index ac6bd18c99d..1a961f51838 100644 --- a/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs +++ b/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs @@ -2,6 +2,23 @@ public class NoOpTResultSpecs { + [Fact] + public void Should_throw_when_action_is_null() + { + var flags = BindingFlags.NonPublic | BindingFlags.Instance; + Func 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: "EmptyStruct" }); + var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]); + + var exceptionAssertions = func.Should().Throw(); + exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation."); + exceptionAssertions.WithInnerException("action"); + } + [Fact] public void Should_execute_user_delegate() {