From 6221ddc952761d6bedb9f9d29b2389a2d3a33c18 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Sun, 21 Jul 2024 19:51:01 +0300 Subject: [PATCH 1/4] Fix warning CA1062#NoOpSpecs --- src/Polly/NoOp/NoOpPolicy.cs | 23 ++++++++++++++++++----- test/Polly.Specs/NoOp/NoOpSpecs.cs | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) 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..084cb4c7726 100644 --- a/test/Polly.Specs/NoOp/NoOpSpecs.cs +++ b/test/Polly.Specs/NoOp/NoOpSpecs.cs @@ -2,6 +2,35 @@ 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"); + + instance = Activator.CreateInstance(typeof(NoOpPolicy), true)!; + instanceType = instance.GetType(); + methods = instanceType.GetMethods(flags); + methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" }); + func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]); + + 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() { From 75b50319dfe37eaa35a429c05ee12283334b4419 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Sun, 21 Jul 2024 19:51:01 +0300 Subject: [PATCH 2/4] Fix warning CA1062#NoOpPolicy --- src/Polly/NoOp/NoOpPolicy.cs | 23 ++++++++++++++++++----- test/Polly.Specs/NoOp/NoOpSpecs.cs | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) 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..084cb4c7726 100644 --- a/test/Polly.Specs/NoOp/NoOpSpecs.cs +++ b/test/Polly.Specs/NoOp/NoOpSpecs.cs @@ -2,6 +2,35 @@ 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"); + + instance = Activator.CreateInstance(typeof(NoOpPolicy), true)!; + instanceType = instance.GetType(); + methods = instanceType.GetMethods(flags); + methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" }); + func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]); + + 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() { From 6b990e55f22c1974bba497f9cc5352911beac0b9 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Sun, 21 Jul 2024 20:09:43 +0300 Subject: [PATCH 3/4] Split Test --- test/Polly.Specs/NoOp/NoOpSpecs.cs | 10 ---------- test/Polly.Specs/NoOp/NoOpTResultSpecs.cs | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/test/Polly.Specs/NoOp/NoOpSpecs.cs b/test/Polly.Specs/NoOp/NoOpSpecs.cs index 084cb4c7726..6ff214de8c7 100644 --- a/test/Polly.Specs/NoOp/NoOpSpecs.cs +++ b/test/Polly.Specs/NoOp/NoOpSpecs.cs @@ -19,16 +19,6 @@ public void Should_throw_when_action_is_null() var exceptionAssertions = func.Should().Throw(); exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation."); exceptionAssertions.WithInnerException("action"); - - instance = Activator.CreateInstance(typeof(NoOpPolicy), true)!; - instanceType = instance.GetType(); - methods = instanceType.GetMethods(flags); - methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" }); - func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]); - - exceptionAssertions = func.Should().Throw(); - exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation."); - exceptionAssertions.WithInnerException("action"); } [Fact] diff --git a/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs b/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs index ac6bd18c99d..5342c7c70f2 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() { From d22bf21023b5fa73dbb71409726bbb5ebe3a5742 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Sun, 21 Jul 2024 20:10:10 +0300 Subject: [PATCH 4/4] fix SA1028 --- test/Polly.Specs/NoOp/NoOpTResultSpecs.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs b/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs index 5342c7c70f2..1a961f51838 100644 --- a/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs +++ b/test/Polly.Specs/NoOp/NoOpTResultSpecs.cs @@ -7,7 +7,7 @@ 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);