Skip to content

Commit 1f232ca

Browse files
Improve Polly coverage (#2505)
Extend coverage for unit tests and mutations with low-hanging fruit parameter validation and redundant code.
1 parent 32064ed commit 1f232ca

41 files changed

Lines changed: 886 additions & 57 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Polly/Bulkhead/AsyncBulkheadPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal AsyncBulkheadPolicy(
1919
Func<Context, Task> onBulkheadRejectedAsync)
2020
{
2121
_maxQueueingActions = maxQueueingActions;
22-
_onBulkheadRejectedAsync = onBulkheadRejectedAsync ?? throw new ArgumentNullException(nameof(onBulkheadRejectedAsync));
22+
_onBulkheadRejectedAsync = onBulkheadRejectedAsync;
2323

2424
(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
2525
}
@@ -87,7 +87,7 @@ internal AsyncBulkheadPolicy(
8787
Func<Context, Task> onBulkheadRejectedAsync)
8888
{
8989
_maxQueueingActions = maxQueueingActions;
90-
_onBulkheadRejectedAsync = onBulkheadRejectedAsync ?? throw new ArgumentNullException(nameof(onBulkheadRejectedAsync));
90+
_onBulkheadRejectedAsync = onBulkheadRejectedAsync;
9191

9292
(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
9393
}

src/Polly/Bulkhead/BulkheadPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal BulkheadPolicy(
1919
Action<Context> onBulkheadRejected)
2020
{
2121
_maxQueueingActions = maxQueueingActions;
22-
_onBulkheadRejected = onBulkheadRejected ?? throw new ArgumentNullException(nameof(onBulkheadRejected));
22+
_onBulkheadRejected = onBulkheadRejected;
2323

2424
(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
2525
}
@@ -85,7 +85,7 @@ internal BulkheadPolicy(
8585
Action<Context> onBulkheadRejected)
8686
{
8787
_maxQueueingActions = maxQueueingActions;
88-
_onBulkheadRejected = onBulkheadRejected ?? throw new ArgumentNullException(nameof(onBulkheadRejected));
88+
_onBulkheadRejected = onBulkheadRejected;
8989

9090
(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
9191
}

src/Polly/Fallback/AsyncFallbackPolicy.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ public class AsyncFallbackPolicy : AsyncPolicy, IFallbackPolicy
99
private readonly Func<Exception, Context, Task> _onFallbackAsync;
1010
private readonly Func<Exception, Context, CancellationToken, Task> _fallbackAction;
1111

12-
internal AsyncFallbackPolicy(PolicyBuilder policyBuilder, Func<Exception, Context, Task> onFallbackAsync,
12+
internal AsyncFallbackPolicy(
13+
PolicyBuilder policyBuilder,
14+
Func<Exception, Context, Task> onFallbackAsync,
1315
Func<Exception, Context, CancellationToken, Task> fallbackAction)
1416
: base(policyBuilder)
1517
{
16-
_onFallbackAsync = onFallbackAsync ?? throw new ArgumentNullException(nameof(onFallbackAsync));
17-
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
18+
_onFallbackAsync = onFallbackAsync;
19+
_fallbackAction = fallbackAction;
1820
}
1921

2022
/// <inheritdoc/>
@@ -65,8 +67,8 @@ internal AsyncFallbackPolicy(
6567
Func<DelegateResult<TResult>, Context, CancellationToken, Task<TResult>> fallbackAction)
6668
: base(policyBuilder)
6769
{
68-
_onFallbackAsync = onFallbackAsync ?? throw new ArgumentNullException(nameof(onFallbackAsync));
69-
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
70+
_onFallbackAsync = onFallbackAsync;
71+
_fallbackAction = fallbackAction;
7072
}
7173

7274
/// <inheritdoc/>

src/Polly/Fallback/FallbackPolicy.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ internal FallbackPolicy(
1515
Action<Exception, Context, CancellationToken> fallbackAction)
1616
: base(policyBuilder)
1717
{
18-
_onFallback = onFallback ?? throw new ArgumentNullException(nameof(onFallback));
19-
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
18+
_onFallback = onFallback;
19+
_fallbackAction = fallbackAction;
2020
}
2121

2222
/// <inheritdoc/>
2323
[DebuggerStepThrough]
2424
protected override void Implementation(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) =>
25-
FallbackEngine.Implementation<EmptyStruct>(
25+
FallbackEngine.Implementation(
2626
(ctx, token) =>
2727
{
2828
action(ctx, token);
@@ -62,8 +62,8 @@ internal FallbackPolicy(
6262
Func<DelegateResult<TResult>, Context, CancellationToken, TResult> fallbackAction)
6363
: base(policyBuilder)
6464
{
65-
_onFallback = onFallback ?? throw new ArgumentNullException(nameof(onFallback));
66-
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
65+
_onFallback = onFallback;
66+
_fallbackAction = fallbackAction;
6767
}
6868

6969
/// <inheritdoc/>

src/Polly/Polly.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<AssemblyTitle>Polly</AssemblyTitle>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<ProjectType>Library</ProjectType>
8-
<MutationScore>70</MutationScore>
8+
<MutationScore>80</MutationScore>
99
<IncludePollyUsings>true</IncludePollyUsings>
1010
<!-- We do not plan on enabling nullable annotations for Polly -->
1111
<NoWarn>$(NoWarn);RS0037</NoWarn>

src/Polly/RateLimit/AsyncRateLimitPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AsyncRateLimitPolicy : AsyncPolicy, IRateLimitPolicy
99
private readonly IRateLimiter _rateLimiter;
1010

1111
internal AsyncRateLimitPolicy(IRateLimiter rateLimiter) =>
12-
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
12+
_rateLimiter = rateLimiter;
1313

1414
/// <inheritdoc/>
1515
[DebuggerStepThrough]
@@ -47,7 +47,7 @@ internal AsyncRateLimitPolicy(
4747
IRateLimiter rateLimiter,
4848
Func<TimeSpan, Context, TResult>? retryAfterFactory)
4949
{
50-
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
50+
_rateLimiter = rateLimiter;
5151
_retryAfterFactory = retryAfterFactory;
5252
}
5353

src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity)
3333
throw new ArgumentOutOfRangeException(nameof(onePer), onePer, $"The {nameof(LockFreeTokenBucketRateLimiter)} must specify a positive TimeSpan for how often an execution is permitted.");
3434
}
3535

36-
if (bucketCapacity <= 0)
37-
{
38-
throw new ArgumentOutOfRangeException(nameof(bucketCapacity), bucketCapacity, $"{nameof(bucketCapacity)} must be greater than or equal to 1.");
39-
}
40-
4136
_addTokenTickInterval = onePer.Ticks;
4237
_bucketCapacity = bucketCapacity;
4338

@@ -82,6 +77,7 @@ public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity)
8277
long tokensToAdd = Math.Min(_bucketCapacity, tokensMissedAdding);
8378

8479
// Work out when tokens would next be due to be added, if we add these tokens.
80+
// stryker disable once all : no means to test this
8581
long newAddNextTokenAtTicks = currentAddNextTokenAtTicks + (tokensToAdd * _addTokenTickInterval);
8682

8783
// But if we were way overdue refilling the bucket (there was inactivity for a while), that value would be out-of-date: the next time we add tokens must be at least addTokenTickInterval from now.

src/Polly/RateLimit/RateLimitPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class RateLimitPolicy : Policy, IRateLimitPolicy
99
private readonly IRateLimiter _rateLimiter;
1010

1111
internal RateLimitPolicy(IRateLimiter rateLimiter) =>
12-
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
12+
_rateLimiter = rateLimiter;
1313

1414
/// <inheritdoc/>
1515
[DebuggerStepThrough]
@@ -37,7 +37,7 @@ internal RateLimitPolicy(
3737
IRateLimiter rateLimiter,
3838
Func<TimeSpan, Context, TResult>? retryAfterFactory)
3939
{
40-
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
40+
_rateLimiter = rateLimiter;
4141
_retryAfterFactory = retryAfterFactory;
4242
}
4343

src/Polly/Registry/PolicyRegistry.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ public PolicyRegistry()
2525
/// <remarks>This internal constructor exists solely to facilitate testing of the GetEnumerator() methods, which allow us to support collection initialisation syntax.</remarks>
2626
/// </summary>
2727
/// <param name="registry">a dictionary containing keys and policies used for testing.</param>
28-
internal PolicyRegistry(IDictionary<string, IsPolicy> registry) =>
29-
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
28+
internal PolicyRegistry(IDictionary<string, IsPolicy> registry)
29+
{
30+
#pragma warning disable S3236 // Remove this argument from the method call; it hides the caller information.
31+
Debug.Assert(registry != null, "This constructor is for testing only, and should not be called with a null registry.");
32+
#pragma warning restore S3236 // Remove this argument from the method call; it hides the caller information.
33+
_registry = registry;
34+
}
3035

3136
private ConcurrentDictionary<string, IsPolicy> ThrowIfNotConcurrentImplementation()
3237
{

src/Polly/Retry/AsyncRetryPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal AsyncRetryPolicy(
2121
_permittedRetryCount = permittedRetryCount;
2222
_sleepDurationsEnumerable = sleepDurationsEnumerable;
2323
_sleepDurationProvider = sleepDurationProvider;
24-
_onRetryAsync = onRetryAsync ?? throw new ArgumentNullException(nameof(onRetryAsync));
24+
_onRetryAsync = onRetryAsync;
2525
}
2626

2727
/// <inheritdoc/>
@@ -78,7 +78,7 @@ internal AsyncRetryPolicy(
7878
_permittedRetryCount = permittedRetryCount;
7979
_sleepDurationsEnumerable = sleepDurationsEnumerable;
8080
_sleepDurationProvider = sleepDurationProvider;
81-
_onRetryAsync = onRetryAsync ?? throw new ArgumentNullException(nameof(onRetryAsync));
81+
_onRetryAsync = onRetryAsync;
8282
}
8383

8484
/// <inheritdoc/>

0 commit comments

Comments
 (0)