Skip to content

Commit 626f6d1

Browse files
authored
Fix CA1062 warnings (#2236)
Fix CA1062 warnings for `BulkheadPolicy`.
1 parent c572c4c commit 626f6d1

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

src/Polly/Bulkhead/BulkheadPolicy.cs

+29-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace Polly.Bulkhead;
44
/// <summary>
55
/// A bulkhead-isolation policy which can be applied to delegates.
66
/// </summary>
7-
#pragma warning disable CA1062 // Validate arguments of public methods
87
public class BulkheadPolicy : Policy, IBulkheadPolicy
98
{
109
private readonly SemaphoreSlim _maxParallelizationSemaphore;
@@ -25,8 +24,21 @@ internal BulkheadPolicy(
2524

2625
/// <inheritdoc/>
2726
[DebuggerStepThrough]
28-
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
29-
BulkheadEngine.Implementation(action, context, _onBulkheadRejected, _maxParallelizationSemaphore, _maxQueuedActionsSemaphore, cancellationToken);
27+
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
28+
{
29+
if (action is null)
30+
{
31+
throw new ArgumentNullException(nameof(action));
32+
}
33+
34+
return BulkheadEngine.Implementation(
35+
action,
36+
context,
37+
_onBulkheadRejected,
38+
_maxParallelizationSemaphore,
39+
_maxQueuedActionsSemaphore,
40+
cancellationToken);
41+
}
3042

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

7486
/// <inheritdoc/>
7587
[DebuggerStepThrough]
76-
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
77-
BulkheadEngine.Implementation(action, context, _onBulkheadRejected, _maxParallelizationSemaphore, _maxQueuedActionsSemaphore, cancellationToken);
88+
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
89+
{
90+
if (action is null)
91+
{
92+
throw new ArgumentNullException(nameof(action));
93+
}
94+
95+
return BulkheadEngine.Implementation(
96+
action,
97+
context,
98+
_onBulkheadRejected,
99+
_maxParallelizationSemaphore,
100+
_maxQueuedActionsSemaphore, cancellationToken);
101+
}
78102

79103
/// <summary>
80104
/// Gets the number of slots currently available for executing actions through the bulkhead.

test/Polly.Specs/Bulkhead/BulkheadSpecs.cs

+28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@ public BulkheadSpecs(ITestOutputHelper testOutputHelper)
1010

1111
#region Configuration
1212

13+
[Fact]
14+
public void Should_throw_when_action_is_null()
15+
{
16+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
17+
Func<Context, CancellationToken, EmptyStruct> action = null!;
18+
var maxParallelization = 1;
19+
var maxQueueingActions = 1;
20+
Action<Context> onBulkheadRejected = _ => { };
21+
22+
var instance = Activator.CreateInstance(
23+
typeof(BulkheadPolicy),
24+
flags,
25+
null,
26+
[maxParallelization, maxQueueingActions, onBulkheadRejected],
27+
null)!;
28+
var instanceType = instance.GetType();
29+
var methods = instanceType.GetMethods(flags);
30+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "TResult" });
31+
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));
32+
33+
var func = () => generic.Invoke(instance, [action, new Context(), CancellationToken.None]);
34+
35+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
36+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
37+
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
38+
.Which.ParamName.Should().Be("action");
39+
}
40+
1341
[Fact]
1442
public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
1543
{

test/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs

+27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@ public BulkheadTResultSpecs(ITestOutputHelper testOutputHelper)
1010

1111
#region Configuration
1212

13+
[Fact]
14+
public void Should_throw_when_action_is_null()
15+
{
16+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
17+
Func<Context, CancellationToken, EmptyStruct> action = null!;
18+
var maxParallelization = 1;
19+
var maxQueueingActions = 1;
20+
Action<Context> onBulkheadRejected = _ => { };
21+
22+
var instance = Activator.CreateInstance(
23+
typeof(BulkheadPolicy<EmptyStruct>),
24+
flags,
25+
null,
26+
[maxParallelization, maxQueueingActions, onBulkheadRejected],
27+
null)!;
28+
var instanceType = instance.GetType();
29+
var methods = instanceType.GetMethods(flags);
30+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" });
31+
32+
var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]);
33+
34+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
35+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
36+
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
37+
.Which.ParamName.Should().Be("action");
38+
}
39+
1340
[Fact]
1441
public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
1542
{

0 commit comments

Comments
 (0)