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#AsyncCircuitBreakerPolicy #2235

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/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