Skip to content

Commit

Permalink
Fix CA1062 warnings (#2237)
Browse files Browse the repository at this point in the history
Fix CA1062 warnings for `AsyncBulkheadPolicy`.
  • Loading branch information
Zombach authored Jul 23, 2024
1 parent b5f347c commit c332b15
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/Polly/Bulkhead/AsyncBulkheadPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.Bulkhead;
/// <summary>
/// A bulkhead-isolation policy which can be applied to delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class AsyncBulkheadPolicy : AsyncPolicy, IBulkheadPolicy
{
private readonly SemaphoreSlim _maxParallelizationSemaphore;
Expand Down Expand Up @@ -35,9 +34,26 @@ internal AsyncBulkheadPolicy(

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

return AsyncBulkheadEngine.ImplementationAsync(
action,
context,
_onBulkheadRejectedAsync,
_maxParallelizationSemaphore,
_maxQueuedActionsSemaphore,
continueOnCapturedContext,
cancellationToken);
}

/// <inheritdoc/>
public void Dispose()
Expand Down Expand Up @@ -71,8 +87,26 @@ internal AsyncBulkheadPolicy(

/// <inheritdoc/>
[DebuggerStepThrough]
protected override Task<TResult> ImplementationAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
AsyncBulkheadEngine.ImplementationAsync(action, context, _onBulkheadRejectedAsync, _maxParallelizationSemaphore, _maxQueuedActionsSemaphore, continueOnCapturedContext, cancellationToken);
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 AsyncBulkheadEngine.ImplementationAsync(
action,
context,
_onBulkheadRejectedAsync,
_maxParallelizationSemaphore,
_maxQueuedActionsSemaphore,
continueOnCapturedContext,
cancellationToken);
}

/// <summary>
/// Gets the number of slots currently available for executing actions through the bulkhead.
Expand Down
28 changes: 28 additions & 0 deletions test/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ public class BulkheadAsyncSpecs(ITestOutputHelper testOutputHelper) : BulkheadSp
{
#region Configuration

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, Task<EmptyStruct>> action = null!;
var maxParallelization = 1;
var maxQueueingActions = 1;
Func<Context, Task> onBulkheadRejectedAsync = (_) => Task.CompletedTask;

var instance = Activator.CreateInstance(
typeof(AsyncBulkheadPolicy),
flags,
null,
[maxParallelization, maxQueueingActions, onBulkheadRejectedAsync],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "ImplementationAsync", ReturnType.Name: "Task`1" });
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));

var func = () => generic.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_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
{
Expand Down
27 changes: 27 additions & 0 deletions test/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ public BulkheadTResultAsyncSpecs(ITestOutputHelper testOutputHelper)

#region Configuration

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, Task<EmptyStruct>> action = null!;
var maxParallelization = 1;
var maxQueueingActions = 1;
Func<Context, Task> onBulkheadRejectedAsync = (_) => Task.CompletedTask;

var instance = Activator.CreateInstance(
typeof(AsyncBulkheadPolicy<EmptyStruct>),
flags,
null,
[maxParallelization, maxQueueingActions, onBulkheadRejectedAsync],
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_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
{
Expand Down

0 comments on commit c332b15

Please sign in to comment.