From 5f4c4773cafff38a85bd8a59cfb66bef0a61b7b5 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Mon, 22 Jul 2024 21:09:11 +0300 Subject: [PATCH] Fix warning CA1062#FallbackPolicy --- src/Polly/Fallback/FallbackPolicy.cs | 12 ++++++--- .../Fallback/FallbackTResultSpecs.cs | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Polly/Fallback/FallbackPolicy.cs b/src/Polly/Fallback/FallbackPolicy.cs index 67eed44f058..f45cfc9ddba 100644 --- a/src/Polly/Fallback/FallbackPolicy.cs +++ b/src/Polly/Fallback/FallbackPolicy.cs @@ -4,7 +4,6 @@ namespace Polly.Fallback; /// /// A fallback policy that can be applied to delegates. /// -#pragma warning disable CA1062 // Validate arguments of public methods public class FallbackPolicy : Policy, IFallbackPolicy { private readonly Action _onFallback; @@ -69,8 +68,14 @@ internal FallbackPolicy( /// [DebuggerStepThrough] - protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) => - FallbackEngine.Implementation( + protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) + { + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + + return FallbackEngine.Implementation( action, context, ExceptionPredicates, @@ -78,4 +83,5 @@ protected override TResult Implementation(Func action = null!; + PolicyBuilder policyBuilder = new PolicyBuilder(exception => exception); + Action, Context> onFallback = (_, _) => { }; + Func, Context, CancellationToken, EmptyStruct> fallbackAction = (_, _, _) => EmptyStruct.Instance; + + var instance = Activator.CreateInstance( + typeof(FallbackPolicy), + flags, + null, + [policyBuilder, onFallback, fallbackAction], + null)!; + 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.And.InnerException.Should().BeOfType() + .Which.ParamName.Should().Be("action"); + } + [Fact] public void Should_throw_when_fallback_action_is_null() {