Skip to content
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
4 changes: 2 additions & 2 deletions src/Polly/Bulkhead/AsyncBulkheadPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal AsyncBulkheadPolicy(
Func<Context, Task> onBulkheadRejectedAsync)
{
_maxQueueingActions = maxQueueingActions;
_onBulkheadRejectedAsync = onBulkheadRejectedAsync ?? throw new ArgumentNullException(nameof(onBulkheadRejectedAsync));
_onBulkheadRejectedAsync = onBulkheadRejectedAsync;

(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ internal AsyncBulkheadPolicy(
Func<Context, Task> onBulkheadRejectedAsync)
{
_maxQueueingActions = maxQueueingActions;
_onBulkheadRejectedAsync = onBulkheadRejectedAsync ?? throw new ArgumentNullException(nameof(onBulkheadRejectedAsync));
_onBulkheadRejectedAsync = onBulkheadRejectedAsync;

(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Polly/Bulkhead/BulkheadPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal BulkheadPolicy(
Action<Context> onBulkheadRejected)
{
_maxQueueingActions = maxQueueingActions;
_onBulkheadRejected = onBulkheadRejected ?? throw new ArgumentNullException(nameof(onBulkheadRejected));
_onBulkheadRejected = onBulkheadRejected;

(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
}
Expand Down Expand Up @@ -85,7 +85,7 @@ internal BulkheadPolicy(
Action<Context> onBulkheadRejected)
{
_maxQueueingActions = maxQueueingActions;
_onBulkheadRejected = onBulkheadRejected ?? throw new ArgumentNullException(nameof(onBulkheadRejected));
_onBulkheadRejected = onBulkheadRejected;

(_maxParallelizationSemaphore, _maxQueuedActionsSemaphore) = BulkheadSemaphoreFactory.CreateBulkheadSemaphores(maxParallelization, maxQueueingActions);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Polly/Fallback/AsyncFallbackPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public class AsyncFallbackPolicy : AsyncPolicy, IFallbackPolicy
private readonly Func<Exception, Context, Task> _onFallbackAsync;
private readonly Func<Exception, Context, CancellationToken, Task> _fallbackAction;

internal AsyncFallbackPolicy(PolicyBuilder policyBuilder, Func<Exception, Context, Task> onFallbackAsync,
internal AsyncFallbackPolicy(
PolicyBuilder policyBuilder,
Func<Exception, Context, Task> onFallbackAsync,
Func<Exception, Context, CancellationToken, Task> fallbackAction)
: base(policyBuilder)
{
_onFallbackAsync = onFallbackAsync ?? throw new ArgumentNullException(nameof(onFallbackAsync));
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
_onFallbackAsync = onFallbackAsync;
_fallbackAction = fallbackAction;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -65,8 +67,8 @@ internal AsyncFallbackPolicy(
Func<DelegateResult<TResult>, Context, CancellationToken, Task<TResult>> fallbackAction)
: base(policyBuilder)
{
_onFallbackAsync = onFallbackAsync ?? throw new ArgumentNullException(nameof(onFallbackAsync));
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
_onFallbackAsync = onFallbackAsync;
_fallbackAction = fallbackAction;
}

/// <inheritdoc/>
Expand Down
10 changes: 5 additions & 5 deletions src/Polly/Fallback/FallbackPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ internal FallbackPolicy(
Action<Exception, Context, CancellationToken> fallbackAction)
: base(policyBuilder)
{
_onFallback = onFallback ?? throw new ArgumentNullException(nameof(onFallback));
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
_onFallback = onFallback;
_fallbackAction = fallbackAction;
}

/// <inheritdoc/>
[DebuggerStepThrough]
protected override void Implementation(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) =>
FallbackEngine.Implementation<EmptyStruct>(
FallbackEngine.Implementation(
(ctx, token) =>
{
action(ctx, token);
Expand Down Expand Up @@ -62,8 +62,8 @@ internal FallbackPolicy(
Func<DelegateResult<TResult>, Context, CancellationToken, TResult> fallbackAction)
: base(policyBuilder)
{
_onFallback = onFallback ?? throw new ArgumentNullException(nameof(onFallback));
_fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction));
_onFallback = onFallback;
_fallbackAction = fallbackAction;
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/Polly/Polly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyTitle>Polly</AssemblyTitle>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<ProjectType>Library</ProjectType>
<MutationScore>70</MutationScore>
<MutationScore>80</MutationScore>
<IncludePollyUsings>true</IncludePollyUsings>
<!-- We do not plan on enabling nullable annotations for Polly -->
<NoWarn>$(NoWarn);RS0037</NoWarn>
Expand Down
4 changes: 2 additions & 2 deletions src/Polly/RateLimit/AsyncRateLimitPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AsyncRateLimitPolicy : AsyncPolicy, IRateLimitPolicy
private readonly IRateLimiter _rateLimiter;

internal AsyncRateLimitPolicy(IRateLimiter rateLimiter) =>
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
_rateLimiter = rateLimiter;

/// <inheritdoc/>
[DebuggerStepThrough]
Expand Down Expand Up @@ -47,7 +47,7 @@ internal AsyncRateLimitPolicy(
IRateLimiter rateLimiter,
Func<TimeSpan, Context, TResult>? retryAfterFactory)
{
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
_rateLimiter = rateLimiter;
_retryAfterFactory = retryAfterFactory;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity)
throw new ArgumentOutOfRangeException(nameof(onePer), onePer, $"The {nameof(LockFreeTokenBucketRateLimiter)} must specify a positive TimeSpan for how often an execution is permitted.");
}

if (bucketCapacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(bucketCapacity), bucketCapacity, $"{nameof(bucketCapacity)} must be greater than or equal to 1.");
}

_addTokenTickInterval = onePer.Ticks;
_bucketCapacity = bucketCapacity;

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

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

// 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.
Expand Down
4 changes: 2 additions & 2 deletions src/Polly/RateLimit/RateLimitPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class RateLimitPolicy : Policy, IRateLimitPolicy
private readonly IRateLimiter _rateLimiter;

internal RateLimitPolicy(IRateLimiter rateLimiter) =>
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
_rateLimiter = rateLimiter;

/// <inheritdoc/>
[DebuggerStepThrough]
Expand Down Expand Up @@ -37,7 +37,7 @@ internal RateLimitPolicy(
IRateLimiter rateLimiter,
Func<TimeSpan, Context, TResult>? retryAfterFactory)
{
_rateLimiter = rateLimiter ?? throw new ArgumentNullException(nameof(rateLimiter));
_rateLimiter = rateLimiter;
_retryAfterFactory = retryAfterFactory;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Polly/Registry/PolicyRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ public PolicyRegistry()
/// <remarks>This internal constructor exists solely to facilitate testing of the GetEnumerator() methods, which allow us to support collection initialisation syntax.</remarks>
/// </summary>
/// <param name="registry">a dictionary containing keys and policies used for testing.</param>
internal PolicyRegistry(IDictionary<string, IsPolicy> registry) =>
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
internal PolicyRegistry(IDictionary<string, IsPolicy> registry)
{
#pragma warning disable S3236 // Remove this argument from the method call; it hides the caller information.
Debug.Assert(registry != null, "This constructor is for testing only, and should not be called with a null registry.");
#pragma warning restore S3236 // Remove this argument from the method call; it hides the caller information.
_registry = registry;
}

private ConcurrentDictionary<string, IsPolicy> ThrowIfNotConcurrentImplementation()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Polly/Retry/AsyncRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal AsyncRetryPolicy(
_permittedRetryCount = permittedRetryCount;
_sleepDurationsEnumerable = sleepDurationsEnumerable;
_sleepDurationProvider = sleepDurationProvider;
_onRetryAsync = onRetryAsync ?? throw new ArgumentNullException(nameof(onRetryAsync));
_onRetryAsync = onRetryAsync;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -78,7 +78,7 @@ internal AsyncRetryPolicy(
_permittedRetryCount = permittedRetryCount;
_sleepDurationsEnumerable = sleepDurationsEnumerable;
_sleepDurationProvider = sleepDurationProvider;
_onRetryAsync = onRetryAsync ?? throw new ArgumentNullException(nameof(onRetryAsync));
_onRetryAsync = onRetryAsync;
}

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions src/Polly/Retry/RetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal RetryPolicy(
_permittedRetryCount = permittedRetryCount;
_sleepDurationsEnumerable = sleepDurationsEnumerable;
_sleepDurationProvider = sleepDurationProvider;
_onRetry = onRetry ?? throw new ArgumentNullException(nameof(onRetry));
_onRetry = onRetry;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -71,7 +71,7 @@ internal RetryPolicy(
_permittedRetryCount = permittedRetryCount;
_sleepDurationsEnumerable = sleepDurationsEnumerable;
_sleepDurationProvider = sleepDurationProvider;
_onRetry = onRetry ?? throw new ArgumentNullException(nameof(onRetry));
_onRetry = onRetry;
}

/// <inheritdoc/>
Expand Down
8 changes: 4 additions & 4 deletions src/Polly/Timeout/AsyncTimeoutPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ internal AsyncTimeoutPolicy(
TimeoutStrategy timeoutStrategy,
Func<Context, TimeSpan, Task, Exception, Task> onTimeoutAsync)
{
_timeoutProvider = timeoutProvider ?? throw new ArgumentNullException(nameof(timeoutProvider));
_timeoutProvider = timeoutProvider;
_timeoutStrategy = timeoutStrategy;
_onTimeoutAsync = onTimeoutAsync ?? throw new ArgumentNullException(nameof(onTimeoutAsync));
_onTimeoutAsync = onTimeoutAsync;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -58,9 +58,9 @@ internal AsyncTimeoutPolicy(
TimeoutStrategy timeoutStrategy,
Func<Context, TimeSpan, Task, Exception, Task> onTimeoutAsync)
{
_timeoutProvider = timeoutProvider ?? throw new ArgumentNullException(nameof(timeoutProvider));
_timeoutProvider = timeoutProvider;
_timeoutStrategy = timeoutStrategy;
_onTimeoutAsync = onTimeoutAsync ?? throw new ArgumentNullException(nameof(onTimeoutAsync));
_onTimeoutAsync = onTimeoutAsync;
}

/// <inheritdoc/>
Expand Down
6 changes: 3 additions & 3 deletions src/Polly/Timeout/AsyncTimeoutSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(
}

return new AsyncTimeoutPolicy(
timeoutProvider,
timeoutStrategy,
onTimeoutAsync);
timeoutProvider,
timeoutStrategy,
onTimeoutAsync);
}
}
6 changes: 3 additions & 3 deletions src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ public static AsyncTimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<Context, Ti
}

return new AsyncTimeoutPolicy<TResult>(
timeoutProvider,
timeoutStrategy,
onTimeoutAsync);
timeoutProvider,
timeoutStrategy,
onTimeoutAsync);
}
}
8 changes: 4 additions & 4 deletions src/Polly/Timeout/TimeoutPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ internal TimeoutPolicy(
TimeoutStrategy timeoutStrategy,
Action<Context, TimeSpan, Task, Exception> onTimeout)
{
_timeoutProvider = timeoutProvider ?? throw new ArgumentNullException(nameof(timeoutProvider));
_timeoutProvider = timeoutProvider;
_timeoutStrategy = timeoutStrategy;
_onTimeout = onTimeout ?? throw new ArgumentNullException(nameof(onTimeout));
_onTimeout = onTimeout;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -53,9 +53,9 @@ internal TimeoutPolicy(
TimeoutStrategy timeoutStrategy,
Action<Context, TimeSpan, Task, Exception> onTimeout)
{
_timeoutProvider = timeoutProvider ?? throw new ArgumentNullException(nameof(timeoutProvider));
_timeoutProvider = timeoutProvider;
_timeoutStrategy = timeoutStrategy;
_onTimeout = onTimeout ?? throw new ArgumentNullException(nameof(onTimeout));
_onTimeout = onTimeout;
}

/// <inheritdoc/>
Expand Down
6 changes: 3 additions & 3 deletions src/Polly/Timeout/TimeoutSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ public static TimeoutPolicy Timeout(
}

return new TimeoutPolicy(
timeoutProvider,
timeoutStrategy,
onTimeout);
timeoutProvider,
timeoutStrategy,
onTimeout);
}
}
6 changes: 3 additions & 3 deletions src/Polly/Timeout/TimeoutTResultSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ public static TimeoutPolicy<TResult> Timeout<TResult>(
}

return new TimeoutPolicy<TResult>(
timeoutProvider,
timeoutStrategy,
onTimeout);
timeoutProvider,
timeoutStrategy,
onTimeout);
}
}
1 change: 1 addition & 0 deletions src/Polly/Utilities/SystemClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class SystemClock
public static Action<TimeSpan, CancellationToken> Sleep = (timeSpan, cancellationToken) =>
#pragma warning restore S2223 // Non-constant static fields should not be visible
{
// Stryker disable once Boolean : no means to test this
if (cancellationToken.WaitHandle.WaitOne(timeSpan))
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
3 changes: 2 additions & 1 deletion test/Polly.Specs/Bulkhead/BulkheadSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void Should_call_onBulkheadRejected_with_passed_context()
// Time for the other thread to kick up and take the bulkhead.
Within(CohesionTimeLimit, () => Expect(0, () => bulkhead.BulkheadAvailableCount, nameof(bulkhead.BulkheadAvailableCount)));

Should.Throw<BulkheadRejectedException>(() => bulkhead.Execute(_ => { }, contextPassedToExecute));
Should.Throw<BulkheadRejectedException>(() => bulkhead.Execute(_ => { }, contextPassedToExecute))
.Message.ShouldBe("The bulkhead semaphore and queue are full and execution was rejected.");

#if NET
tcs.SetCanceled(CancellationToken);
Expand Down
10 changes: 8 additions & 2 deletions test/Polly.Specs/Caching/AbsoluteTtlSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public void Should_return_zero_ttl_if_configured_to_expire_in_past()
{
AbsoluteTtl ttlStrategy = new AbsoluteTtl(SystemClock.DateTimeOffsetUtcNow().Subtract(TimeSpan.FromTicks(1)));

ttlStrategy.GetTtl(new Context("someOperationKey"), null).Timespan.ShouldBe(TimeSpan.Zero);
var actual = ttlStrategy.GetTtl(new Context("someOperationKey"), null);

actual.Timespan.ShouldBe(TimeSpan.Zero);
actual.SlidingExpiration.ShouldBeFalse();
}

[Fact]
Expand All @@ -44,7 +47,10 @@ public void Should_return_timespan_reflecting_time_until_expiry()
AbsoluteTtl ttlStrategy = new AbsoluteTtl(tomorrow);

SystemClock.DateTimeOffsetUtcNow = () => today;
ttlStrategy.GetTtl(new Context("someOperationKey"), null).Timespan.ShouldBe(TimeSpan.FromDays(1));
var actual = ttlStrategy.GetTtl(new Context("someOperationKey"), null);

actual.Timespan.ShouldBe(TimeSpan.FromDays(1));
actual.SlidingExpiration.ShouldBeFalse();
}

public void Dispose() =>
Expand Down
8 changes: 8 additions & 0 deletions test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public void Should_throw_when_ttl_strategy_is_null()
Should.Throw<ArgumentNullException>(action).ParamName.ShouldBe("ttlStrategy");
}

[Fact]
public void Should_throw_when_cacheProvider_is_null()
{
IAsyncCacheProvider nonGenericCacheProvider = null!;
Action action = () => nonGenericCacheProvider.AsyncFor<string>();
Should.Throw<ArgumentNullException>(action).ParamName.ShouldBe("nonGenericCacheProvider");
}

[Fact]
public void Should_throw_when_cache_key_strategy_is_null()
{
Expand Down
7 changes: 7 additions & 0 deletions test/Polly.Specs/Caching/CacheTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public void Should_throw_when_action_is_null()
.ParamName.ShouldBe("action");
}

[Fact]
public void For_throws_if_cache_provider_is_null()
{
ISyncCacheProvider nonGenericCacheProvider = null!;
Should.Throw<ArgumentNullException>(() => nonGenericCacheProvider.For<string>()).ParamName.ShouldBe("nonGenericCacheProvider");
}

[Fact]
public void Should_throw_when_cache_provider_is_null()
{
Expand Down
Loading