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#BulkheadPolicy #2236

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
34 changes: 29 additions & 5 deletions src/Polly/Bulkhead/BulkheadPolicy.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 BulkheadPolicy : Policy, IBulkheadPolicy
{
private readonly SemaphoreSlim _maxParallelizationSemaphore;
Expand All @@ -25,8 +24,21 @@ internal BulkheadPolicy(

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

return BulkheadEngine.Implementation(
action,
context,
_onBulkheadRejected,
_maxParallelizationSemaphore,
_maxQueuedActionsSemaphore,
cancellationToken);
}

/// <summary>
/// Gets the number of slots currently available for executing actions through the bulkhead.
Expand Down Expand Up @@ -73,8 +85,20 @@ internal BulkheadPolicy(

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

return BulkheadEngine.Implementation(
action,
context,
_onBulkheadRejected,
_maxParallelizationSemaphore,
_maxQueuedActionsSemaphore, 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/BulkheadSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ public BulkheadSpecs(ITestOutputHelper testOutputHelper)

#region Configuration

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

var instance = Activator.CreateInstance(
typeof(BulkheadPolicy),
flags,
null,
[maxParallelization, maxQueueingActions, onBulkheadRejected],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "TResult" });
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));

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

#region Configuration

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

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