Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning CA1062#AsyncFallbackPolicy #2233

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Polly/Fallback/AsyncFallbackPolicy.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 asynchronous delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class AsyncFallbackPolicy : AsyncPolicy, IFallbackPolicy
{
private readonly Func<Exception, Context, Task> _onFallbackAsync;
Expand Down Expand Up @@ -72,9 +71,18 @@ internal AsyncFallbackPolicy(

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

return AsyncFallbackEngine.ImplementationAsync(
action,
context,
ExceptionPredicates,
Expand All @@ -83,4 +91,5 @@ protected override Task<TResult> ImplementationAsync(Func<Context, CancellationT
_fallbackAction,
continueOnCapturedContext,
cancellationToken);
}
}
27 changes: 27 additions & 0 deletions test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ public class FallbackTResultAsyncSpecs
{
#region Configuration guard condition tests

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

var instance = Activator.CreateInstance(
typeof(AsyncFallbackPolicy<EmptyStruct>),
flags,
null,
[policyBuilder, onFallbackAsync, fallbackAction],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "ImplementationAsync", ReturnType.Name: "Task`1" });

var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None, false]);

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