Skip to content

Commit

Permalink
Fix CA1062 warnings (#2232)
Browse files Browse the repository at this point in the history
Fix CA1062 warnings for `FallbackPolicy`.
  • Loading branch information
Zombach committed Jul 23, 2024
1 parent f054ef7 commit ff3b1f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Polly/Fallback/FallbackPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.Fallback;
/// <summary>
/// A fallback policy that can be applied to delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class FallbackPolicy : Policy, IFallbackPolicy
{
private readonly Action<Exception, Context> _onFallback;
Expand Down Expand Up @@ -69,13 +68,20 @@ internal FallbackPolicy(

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

return FallbackEngine.Implementation(
action,
context,
ExceptionPredicates,
ResultPredicates,
_onFallback,
_fallbackAction,
cancellationToken);
}
}
27 changes: 27 additions & 0 deletions test/Polly.Specs/Fallback/FallbackTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ public class FallbackTResultSpecs
{
#region Configuration guard condition tests

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
PolicyBuilder<EmptyStruct> policyBuilder = new PolicyBuilder<EmptyStruct>(exception => exception);
Action<DelegateResult<EmptyStruct>, Context> onFallback = (_, _) => { };
Func<DelegateResult<EmptyStruct>, Context, CancellationToken, EmptyStruct> fallbackAction = (_, _, _) => EmptyStruct.Instance;

var instance = Activator.CreateInstance(
typeof(FallbackPolicy<EmptyStruct>),
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<TargetInvocationException>();
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
.Which.ParamName.Should().Be("action");
}

[Fact]
public void Should_throw_when_fallback_action_is_null()
{
Expand Down

0 comments on commit ff3b1f4

Please sign in to comment.