Skip to content

Commit

Permalink
Fix warning CA1062#AsyncCircuitBreakerPolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
Zombach committed Jul 22, 2024
1 parent 72331dc commit a42c091
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Polly/CircuitBreaker/AsyncCircuitBreakerPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// A circuit-breaker policy that can be applied to async delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class AsyncCircuitBreakerPolicy : AsyncPolicy, ICircuitBreakerPolicy
{
internal readonly ICircuitController<EmptyStruct> BreakerController;
Expand Down Expand Up @@ -99,14 +98,24 @@ public void Reset() =>

/// <inheritdoc/>
[DebuggerStepThrough]
protected override Task<TResult> ImplementationAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext) =>
AsyncCircuitBreakerEngine.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 AsyncCircuitBreakerEngine.ImplementationAsync(
action,
context,
continueOnCapturedContext,
ExceptionPredicates,
ResultPredicates,
BreakerController,
cancellationToken);
}
}
37 changes: 37 additions & 0 deletions test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ public class CircuitBreakerTResultAsyncSpecs : IDisposable
{
#region Configuration 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);

var exceptionsAllowedBeforeBreaking = 1;
var durationOfBreak = TimeSpan.Zero;
Action<DelegateResult<EmptyStruct>, CircuitState, TimeSpan, Context> onBreak = null!;
Action<Context> onReset = null!;
Action onHalfOpen = null!;
ICircuitController<EmptyStruct> breakerController = new ConsecutiveCountCircuitController<EmptyStruct>(
exceptionsAllowedBeforeBreaking,
durationOfBreak,
onBreak,
onReset,
onHalfOpen);

var instance = Activator.CreateInstance(
typeof(AsyncCircuitBreakerPolicy<EmptyStruct>),
flags,
null,
[policyBuilder, breakerController],
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 async Task Should_be_able_to_handle_a_duration_of_timespan_maxvalue()
{
Expand Down

0 comments on commit a42c091

Please sign in to comment.