diff --git a/src/Polly/AsyncPolicy.ContextAndKeys.cs b/src/Polly/AsyncPolicy.ContextAndKeys.cs index 09fe154bc7b..ec0e813c149 100644 --- a/src/Polly/AsyncPolicy.ContextAndKeys.cs +++ b/src/Polly/AsyncPolicy.ContextAndKeys.cs @@ -9,7 +9,8 @@ public abstract partial class AsyncPolicy /// The unique, used-definable key to assign to this instance. public AsyncPolicy WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; @@ -22,7 +23,8 @@ public AsyncPolicy WithPolicyKey(string policyKey) /// The unique, used-definable key to assign to this instance. IAsyncPolicy IAsyncPolicy.WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; @@ -38,7 +40,8 @@ public abstract partial class AsyncPolicy /// The unique, used-definable key to assign to this instance. public AsyncPolicy WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; @@ -51,7 +54,8 @@ public AsyncPolicy WithPolicyKey(string policyKey) /// The unique, used-definable key to assign to this instance. IAsyncPolicy IAsyncPolicy.WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; diff --git a/src/Polly/AsyncPolicy.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.ExecuteOverloads.cs index 0d35960c437..2040b08e4b7 100644 --- a/src/Polly/AsyncPolicy.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.ExecuteOverloads.cs @@ -93,7 +93,8 @@ public Task ExecuteAsync(Func action, IDiction [DebuggerStepThrough] public async Task ExecuteAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -213,7 +214,8 @@ public Task ExecuteAsync(Func ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -329,7 +331,8 @@ public Task ExecuteAndCaptureAsync(Func ExecuteAndCaptureAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); try { @@ -453,7 +456,8 @@ public Task> ExecuteAndCaptureAsync(Func> ExecuteAndCaptureAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); try { diff --git a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs index 1daa607943e..fedc85c45a4 100644 --- a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs @@ -102,7 +102,8 @@ public Task ExecuteAsync(Func [DebuggerStepThrough] public async Task ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -220,7 +221,8 @@ public Task> ExecuteAndCaptureAsync(Func> ExecuteAndCaptureAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); try { diff --git a/src/Polly/Bulkhead/AsyncBulkheadEngine.cs b/src/Polly/Bulkhead/AsyncBulkheadEngine.cs index 1476fcb5879..7640dc5216d 100644 --- a/src/Polly/Bulkhead/AsyncBulkheadEngine.cs +++ b/src/Polly/Bulkhead/AsyncBulkheadEngine.cs @@ -3,7 +3,7 @@ namespace Polly.Bulkhead; internal static class AsyncBulkheadEngine { - internal static async Task ImplementationAsync( + internal static async Task ImplementationAsync( Func> action, Context context, Func onBulkheadRejectedAsync, diff --git a/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs b/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs index 36f904f5380..9e01a11930b 100644 --- a/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs +++ b/src/Polly/Bulkhead/AsyncBulkheadSyntax.cs @@ -59,9 +59,12 @@ public static AsyncBulkheadPolicy BulkheadAsync( int maxQueuingActions, Func onBulkheadRejectedAsync) { - if (maxParallelization <= 0) throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); - if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); - if (onBulkheadRejectedAsync == null) throw new ArgumentNullException(nameof(onBulkheadRejectedAsync)); + if (maxParallelization <= 0) + throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + if (maxQueuingActions < 0) + throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + if (onBulkheadRejectedAsync == null) + throw new ArgumentNullException(nameof(onBulkheadRejectedAsync)); return new AsyncBulkheadPolicy( maxParallelization, diff --git a/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs b/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs index f3335538c10..77990236f53 100644 --- a/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs +++ b/src/Polly/Bulkhead/AsyncBulkheadTResultSyntax.cs @@ -56,9 +56,12 @@ public static AsyncBulkheadPolicy BulkheadAsync(int maxParalle /// onBulkheadRejectedAsync public static AsyncBulkheadPolicy BulkheadAsync(int maxParallelization, int maxQueuingActions, Func onBulkheadRejectedAsync) { - if (maxParallelization <= 0) throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); - if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); - if (onBulkheadRejectedAsync == null) throw new ArgumentNullException(nameof(onBulkheadRejectedAsync)); + if (maxParallelization <= 0) + throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + if (maxQueuingActions < 0) + throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + if (onBulkheadRejectedAsync == null) + throw new ArgumentNullException(nameof(onBulkheadRejectedAsync)); return new AsyncBulkheadPolicy( maxParallelization, diff --git a/src/Polly/Bulkhead/BulkheadSyntax.cs b/src/Polly/Bulkhead/BulkheadSyntax.cs index ed928e105ae..2b15e6c757b 100644 --- a/src/Polly/Bulkhead/BulkheadSyntax.cs +++ b/src/Polly/Bulkhead/BulkheadSyntax.cs @@ -56,9 +56,12 @@ public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActi /// onBulkheadRejected public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActions, Action onBulkheadRejected) { - if (maxParallelization <= 0) throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); - if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); - if (onBulkheadRejected == null) throw new ArgumentNullException(nameof(onBulkheadRejected)); + if (maxParallelization <= 0) + throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + if (maxQueuingActions < 0) + throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + if (onBulkheadRejected == null) + throw new ArgumentNullException(nameof(onBulkheadRejected)); return new BulkheadPolicy( maxParallelization, diff --git a/src/Polly/Bulkhead/BulkheadTResultSyntax.cs b/src/Polly/Bulkhead/BulkheadTResultSyntax.cs index a8e43ae824a..55445998e36 100644 --- a/src/Polly/Bulkhead/BulkheadTResultSyntax.cs +++ b/src/Polly/Bulkhead/BulkheadTResultSyntax.cs @@ -56,9 +56,12 @@ public static BulkheadPolicy Bulkhead(int maxParallelization, /// onBulkheadRejected public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActions, Action onBulkheadRejected) { - if (maxParallelization <= 0) throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); - if (maxQueuingActions < 0) throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); - if (onBulkheadRejected == null) throw new ArgumentNullException(nameof(onBulkheadRejected)); + if (maxParallelization <= 0) + throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero."); + if (maxQueuingActions < 0) + throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero."); + if (onBulkheadRejected == null) + throw new ArgumentNullException(nameof(onBulkheadRejected)); return new BulkheadPolicy( maxParallelization, diff --git a/src/Polly/Caching/AsyncCachePolicy.cs b/src/Polly/Caching/AsyncCachePolicy.cs index e2b6445c921..a4e0b925388 100644 --- a/src/Polly/Caching/AsyncCachePolicy.cs +++ b/src/Polly/Caching/AsyncCachePolicy.cs @@ -42,9 +42,7 @@ protected override Task ImplementationAsync( Func action, Context context, CancellationToken cancellationToken, - bool continueOnCapturedContext) => - // Pass-through/NOOP policy action, for void-returning executions through the cache policy. - action(context, cancellationToken); + bool continueOnCapturedContext) => action(context, cancellationToken); // Pass-through/NOOP policy action, for void-returning executions through the cache policy. /// [DebuggerStepThrough] diff --git a/src/Polly/Caching/AsyncCacheSyntax.cs b/src/Polly/Caching/AsyncCacheSyntax.cs index 7353ad46939..b7a5702d117 100644 --- a/src/Polly/Caching/AsyncCacheSyntax.cs +++ b/src/Polly/Caching/AsyncCacheSyntax.cs @@ -64,9 +64,12 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, Tim /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); Action emptyDelegate = (_, _) => { }; @@ -105,9 +108,12 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, Tim /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); Action emptyDelegate = (_, _) => { }; @@ -296,13 +302,19 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); - if (onCacheGet == null) throw new ArgumentNullException(nameof(onCacheGet)); - if (onCacheMiss == null) throw new ArgumentNullException(nameof(onCacheMiss)); - if (onCachePut == null) throw new ArgumentNullException(nameof(onCachePut)); + if (onCacheGet == null) + throw new ArgumentNullException(nameof(onCacheGet)); + if (onCacheMiss == null) + throw new ArgumentNullException(nameof(onCacheMiss)); + if (onCachePut == null) + throw new ArgumentNullException(nameof(onCachePut)); return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/AsyncCacheTResultSyntax.cs b/src/Polly/Caching/AsyncCacheTResultSyntax.cs index da92d36c82a..6a7752186bc 100644 --- a/src/Polly/Caching/AsyncCacheTResultSyntax.cs +++ b/src/Polly/Caching/AsyncCacheTResultSyntax.cs @@ -16,7 +16,8 @@ public partial class Policy /// cacheProvider public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -35,7 +36,8 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider /// ttlStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -55,7 +57,8 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -76,7 +79,8 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -96,7 +100,8 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Func cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } @@ -117,7 +122,8 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy, onCacheError); } @@ -149,7 +155,8 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -182,7 +189,8 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -217,7 +225,8 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -253,7 +262,8 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -288,7 +298,8 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -324,7 +335,8 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return CacheAsync(cacheProvider.AsyncFor(), ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -767,13 +779,19 @@ public static AsyncCachePolicy CacheAsync( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - - if (onCacheGet == null) throw new ArgumentNullException(nameof(onCacheGet)); - if (onCacheMiss == null) throw new ArgumentNullException(nameof(onCacheMiss)); - if (onCachePut == null) throw new ArgumentNullException(nameof(onCachePut)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + + if (onCacheGet == null) + throw new ArgumentNullException(nameof(onCacheGet)); + if (onCacheMiss == null) + throw new ArgumentNullException(nameof(onCacheMiss)); + if (onCachePut == null) + throw new ArgumentNullException(nameof(onCachePut)); return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/CacheSyntax.cs b/src/Polly/Caching/CacheSyntax.cs index d104ed2d98a..68fe93eecc0 100644 --- a/src/Polly/Caching/CacheSyntax.cs +++ b/src/Polly/Caching/CacheSyntax.cs @@ -64,9 +64,12 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); Action emptyDelegate = (_, _) => { }; @@ -105,9 +108,12 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); Action emptyDelegate = (_, _) => { }; @@ -308,13 +314,19 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); - if (onCacheGet == null) throw new ArgumentNullException(nameof(onCacheGet)); - if (onCacheMiss == null) throw new ArgumentNullException(nameof(onCacheMiss)); - if (onCachePut == null) throw new ArgumentNullException(nameof(onCachePut)); + if (onCacheGet == null) + throw new ArgumentNullException(nameof(onCacheGet)); + if (onCacheMiss == null) + throw new ArgumentNullException(nameof(onCacheMiss)); + if (onCachePut == null) + throw new ArgumentNullException(nameof(onCachePut)); return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/CacheTResultSyntax.cs b/src/Polly/Caching/CacheTResultSyntax.cs index 9e3ed6666d0..737a27671cd 100644 --- a/src/Polly/Caching/CacheTResultSyntax.cs +++ b/src/Polly/Caching/CacheTResultSyntax.cs @@ -16,7 +16,8 @@ public partial class Policy /// cacheProvider public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -35,7 +36,8 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid /// ttlStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError); } @@ -55,7 +57,8 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -76,7 +79,8 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -96,7 +100,8 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, Func cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError); } @@ -117,7 +122,8 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy, onCacheError); } @@ -149,7 +155,8 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -182,7 +189,8 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -217,7 +225,8 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -253,7 +262,8 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -288,7 +298,8 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -324,7 +335,8 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -772,13 +784,19 @@ public static CachePolicy Cache( Action? onCacheGetError, Action? onCachePutError) { - if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); - if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); - if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - - if (onCacheGet == null) throw new ArgumentNullException(nameof(onCacheGet)); - if (onCacheMiss == null) throw new ArgumentNullException(nameof(onCacheMiss)); - if (onCachePut == null) throw new ArgumentNullException(nameof(onCachePut)); + if (cacheProvider == null) + throw new ArgumentNullException(nameof(cacheProvider)); + if (ttlStrategy == null) + throw new ArgumentNullException(nameof(ttlStrategy)); + if (cacheKeyStrategy == null) + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + + if (onCacheGet == null) + throw new ArgumentNullException(nameof(onCacheGet)); + if (onCacheMiss == null) + throw new ArgumentNullException(nameof(onCacheMiss)); + if (onCachePut == null) + throw new ArgumentNullException(nameof(onCachePut)); return new CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/ContextualTtl.cs b/src/Polly/Caching/ContextualTtl.cs index b02b7cba9ba..c45e23a327b 100644 --- a/src/Polly/Caching/ContextualTtl.cs +++ b/src/Polly/Caching/ContextualTtl.cs @@ -27,7 +27,8 @@ public class ContextualTtl : ITtlStrategy /// TimeSpan. public Ttl GetTtl(Context context, object? result) { - if (!context.ContainsKey(TimeSpanKey)) return _noTtl; + if (!context.ContainsKey(TimeSpanKey)) + return _noTtl; bool sliding = false; diff --git a/src/Polly/Caching/GenericCacheProvider.cs b/src/Polly/Caching/GenericCacheProvider.cs index b35bdd56406..73b617b667f 100644 --- a/src/Polly/Caching/GenericCacheProvider.cs +++ b/src/Polly/Caching/GenericCacheProvider.cs @@ -15,7 +15,7 @@ internal GenericCacheProvider(ISyncCacheProvider nonGenericCacheProvider) => (bool, TCacheFormat?) ISyncCacheProvider.TryGet(string key) { (bool cacheHit, object? result) = _wrappedCacheProvider.TryGet(key); - return (cacheHit, (TCacheFormat?) (result ?? default(TCacheFormat))); + return (cacheHit, (TCacheFormat?)(result ?? default(TCacheFormat))); } void ISyncCacheProvider.Put(string key, TCacheFormat? value, Ttl ttl) => diff --git a/src/Polly/Caching/RelativeTtl.cs b/src/Polly/Caching/RelativeTtl.cs index b8acfb64049..de192d9a8d9 100644 --- a/src/Polly/Caching/RelativeTtl.cs +++ b/src/Polly/Caching/RelativeTtl.cs @@ -14,7 +14,8 @@ public class RelativeTtl : ITtlStrategy /// The timespan for which to consider the cache item valid. public RelativeTtl(TimeSpan ttl) { - if (ttl < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(ttl), "The ttl for items to cache must be greater than zero."); + if (ttl < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(ttl), "The ttl for items to cache must be greater than zero."); this.ttl = ttl; } diff --git a/src/Polly/Caching/ResultTtl.cs b/src/Polly/Caching/ResultTtl.cs index d4192fc2c40..957a821f2a9 100644 --- a/src/Polly/Caching/ResultTtl.cs +++ b/src/Polly/Caching/ResultTtl.cs @@ -15,7 +15,8 @@ public class ResultTtl : ITtlStrategy /// The function to calculate the TTL for which cache items should be considered valid. public ResultTtl(Func ttlFunc) { - if (ttlFunc == null) throw new ArgumentNullException(nameof(ttlFunc)); + if (ttlFunc == null) + throw new ArgumentNullException(nameof(ttlFunc)); _ttlFunc = (_, result) => ttlFunc(result); } diff --git a/src/Polly/Caching/SlidingTtl.cs b/src/Polly/Caching/SlidingTtl.cs index e8b5cd84363..7ef4feb3462 100644 --- a/src/Polly/Caching/SlidingTtl.cs +++ b/src/Polly/Caching/SlidingTtl.cs @@ -14,7 +14,8 @@ public class SlidingTtl : ITtlStrategy /// The sliding timespan for which cache items should be considered valid. public SlidingTtl(TimeSpan slidingTtl) { - if (slidingTtl < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(slidingTtl), "The ttl for items to cache must be greater than zero."); + if (slidingTtl < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(slidingTtl), "The ttl for items to cache must be greater than zero."); ttl = new Ttl(slidingTtl, true); } diff --git a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs index 1b865a9e5f7..51e71f4102d 100644 --- a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerSyntax.cs @@ -211,19 +211,35 @@ public static CircuitBreakerPolicy AdvancedCircuitBreaker(this PolicyBuilder pol /// onReset /// onHalfOpen /// (see "Release It!" by Michael T. Nygard fi) - public static CircuitBreakerPolicy AdvancedCircuitBreaker(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action onBreak, Action onReset, Action onHalfOpen) + public static CircuitBreakerPolicy AdvancedCircuitBreaker( + this PolicyBuilder policyBuilder, + double failureThreshold, + TimeSpan samplingDuration, + int minimumThroughput, + TimeSpan durationOfBreak, + Action onBreak, + Action onReset, + Action onHalfOpen) { var resolutionOfCircuit = TimeSpan.FromTicks(AdvancedCircuitController.ResolutionOfCircuitTimer); - if (failureThreshold <= 0) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); - if (failureThreshold > 1) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); - if (samplingDuration < resolutionOfCircuit) throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); - if (minimumThroughput <= 1) throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (failureThreshold <= 0) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + if (failureThreshold > 1) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + if (samplingDuration < resolutionOfCircuit) + throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + if (minimumThroughput <= 1) + throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs index d34c57e4af0..91c5a11d649 100644 --- a/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/AdvancedCircuitBreakerTResultSyntax.cs @@ -213,19 +213,35 @@ public static CircuitBreakerPolicy AdvancedCircuitBreaker(this /// onReset /// onHalfOpen /// (see "Release It!" by Michael T. Nygard fi) - public static CircuitBreakerPolicy AdvancedCircuitBreaker(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action, CircuitState, TimeSpan, Context> onBreak, Action onReset, Action onHalfOpen) + public static CircuitBreakerPolicy AdvancedCircuitBreaker( + this PolicyBuilder policyBuilder, + double failureThreshold, + TimeSpan samplingDuration, + int minimumThroughput, + TimeSpan durationOfBreak, + Action, CircuitState, TimeSpan, Context> onBreak, + Action onReset, + Action onHalfOpen) { var resolutionOfCircuit = TimeSpan.FromTicks(AdvancedCircuitController.ResolutionOfCircuitTimer); - if (failureThreshold <= 0) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); - if (failureThreshold > 1) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); - if (samplingDuration < resolutionOfCircuit) throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); - if (minimumThroughput <= 1) throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (failureThreshold <= 0) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + if (failureThreshold > 1) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + if (samplingDuration < resolutionOfCircuit) + throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + if (minimumThroughput <= 1) + throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs index f9d25e21f4b..968d60a7aed 100644 --- a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerSyntax.cs @@ -215,19 +215,34 @@ public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyB /// onBreak /// onReset /// onHalfOpen - public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action onBreak, Action onReset, Action onHalfOpen) + public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync( + this PolicyBuilder policyBuilder, + double failureThreshold, + TimeSpan samplingDuration, + int minimumThroughput, + TimeSpan durationOfBreak, + Action onBreak, + Action onReset, Action onHalfOpen) { var resolutionOfCircuit = TimeSpan.FromTicks(AdvancedCircuitController.ResolutionOfCircuitTimer); - if (failureThreshold <= 0) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); - if (failureThreshold > 1) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); - if (samplingDuration < resolutionOfCircuit) throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); - if (minimumThroughput <= 1) throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (failureThreshold <= 0) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + if (failureThreshold > 1) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + if (samplingDuration < resolutionOfCircuit) + throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + if (minimumThroughput <= 1) + throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs index 03fafcb5818..a31f374338c 100644 --- a/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncAdvancedCircuitBreakerTResultSyntax.cs @@ -219,15 +219,23 @@ public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync.ResolutionOfCircuitTimer); - if (failureThreshold <= 0) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); - if (failureThreshold > 1) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); - if (samplingDuration < resolutionOfCircuit) throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); - if (minimumThroughput <= 1) throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (failureThreshold <= 0) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero."); + if (failureThreshold > 1) + throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one."); + if (samplingDuration < resolutionOfCircuit) + throw new ArgumentOutOfRangeException(nameof(samplingDuration), $"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer."); + if (minimumThroughput <= 1) + throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); var breakerController = new AdvancedCircuitController( failureThreshold, diff --git a/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs index 5156eccb7c5..04f244fe7cb 100644 --- a/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncCircuitBreakerSyntax.cs @@ -187,12 +187,17 @@ public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder p /// onHalfOpen public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action onBreak, Action onReset, Action onHalfOpen) { - if (exceptionsAllowedBeforeBreaking <= 0) throw new ArgumentOutOfRangeException(nameof(exceptionsAllowedBeforeBreaking), "Value must be greater than zero."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (exceptionsAllowedBeforeBreaking <= 0) + throw new ArgumentOutOfRangeException(nameof(exceptionsAllowedBeforeBreaking), "Value must be greater than zero."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); var breakerController = new ConsecutiveCountCircuitController( exceptionsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs index 2c1805aca28..4885ab1acb3 100644 --- a/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/AsyncCircuitBreakerTResultSyntax.cs @@ -187,12 +187,17 @@ public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(th /// onHalfOpen public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int handledEventsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action, CircuitState, TimeSpan, Context> onBreak, Action onReset, Action onHalfOpen) { - if (handledEventsAllowedBeforeBreaking <= 0) throw new ArgumentOutOfRangeException(nameof(handledEventsAllowedBeforeBreaking), "Value must be greater than zero."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (handledEventsAllowedBeforeBreaking <= 0) + throw new ArgumentOutOfRangeException(nameof(handledEventsAllowedBeforeBreaking), "Value must be greater than zero."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); var breakerController = new ConsecutiveCountCircuitController( handledEventsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs b/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs index 1e600ad19ae..a321cf994c5 100644 --- a/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs +++ b/src/Polly/CircuitBreaker/CircuitBreakerSyntax.cs @@ -188,12 +188,17 @@ public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuild /// onHalfOpen public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action onBreak, Action onReset, Action onHalfOpen) { - if (exceptionsAllowedBeforeBreaking <= 0) throw new ArgumentOutOfRangeException(nameof(exceptionsAllowedBeforeBreaking), "Value must be greater than zero."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (exceptionsAllowedBeforeBreaking <= 0) + throw new ArgumentOutOfRangeException(nameof(exceptionsAllowedBeforeBreaking), "Value must be greater than zero."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); var breakerController = new ConsecutiveCountCircuitController( exceptionsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs b/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs index b8ae019780e..99873ab0421 100644 --- a/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs +++ b/src/Polly/CircuitBreaker/CircuitBreakerTResultSyntax.cs @@ -188,12 +188,17 @@ public static CircuitBreakerPolicy CircuitBreaker(this PolicyB /// onHalfOpen public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int handledEventsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action, CircuitState, TimeSpan, Context> onBreak, Action onReset, Action onHalfOpen) { - if (handledEventsAllowedBeforeBreaking <= 0) throw new ArgumentOutOfRangeException(nameof(handledEventsAllowedBeforeBreaking), "Value must be greater than zero."); - if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); + if (handledEventsAllowedBeforeBreaking <= 0) + throw new ArgumentOutOfRangeException(nameof(handledEventsAllowedBeforeBreaking), "Value must be greater than zero."); + if (durationOfBreak < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero."); - if (onBreak == null) throw new ArgumentNullException(nameof(onBreak)); - if (onReset == null) throw new ArgumentNullException(nameof(onReset)); - if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen)); + if (onBreak == null) + throw new ArgumentNullException(nameof(onBreak)); + if (onReset == null) + throw new ArgumentNullException(nameof(onReset)); + if (onHalfOpen == null) + throw new ArgumentNullException(nameof(onHalfOpen)); ICircuitController breakerController = new ConsecutiveCountCircuitController( handledEventsAllowedBeforeBreaking, diff --git a/src/Polly/CircuitBreaker/CircuitStateController.cs b/src/Polly/CircuitBreaker/CircuitStateController.cs index 4d24444a82e..ddd10d70c00 100644 --- a/src/Polly/CircuitBreaker/CircuitStateController.cs +++ b/src/Polly/CircuitBreaker/CircuitStateController.cs @@ -148,7 +148,11 @@ public void OnActionPreExecute() case CircuitState.Closed: break; case CircuitState.HalfOpen: - if (!PermitHalfOpenCircuitTest()) { throw GetBreakingException(); } + if (!PermitHalfOpenCircuitTest()) + { + throw GetBreakingException(); + } + break; case CircuitState.Open: throw GetBreakingException(); diff --git a/src/Polly/Context.Dictionary.cs b/src/Polly/Context.Dictionary.cs index c8a42c0b679..ece544b1e72 100644 --- a/src/Polly/Context.Dictionary.cs +++ b/src/Polly/Context.Dictionary.cs @@ -25,11 +25,12 @@ public Context(string operationKey, IDictionary contextData) internal Context(IDictionary contextData) : this() { - if (contextData == null) throw new ArgumentNullException(nameof(contextData)); + if (contextData == null) + throw new ArgumentNullException(nameof(contextData)); wrappedDictionary = new Dictionary(contextData); } -#region IDictionary implementation + #region IDictionary implementation /// public ICollection Keys => WrappedDictionary.Keys; @@ -41,7 +42,7 @@ internal Context(IDictionary contextData) public int Count => WrappedDictionary.Count; /// - bool ICollection>.IsReadOnly => ((IDictionary) WrappedDictionary).IsReadOnly; + bool ICollection>.IsReadOnly => ((IDictionary)WrappedDictionary).IsReadOnly; /// public object this[string key] @@ -68,7 +69,7 @@ public bool TryGetValue(string key, out object value) => /// void ICollection>.Add(KeyValuePair item) => - ((IDictionary) WrappedDictionary).Add(item); + ((IDictionary)WrappedDictionary).Add(item); /// public void Clear() => @@ -76,15 +77,15 @@ public void Clear() => /// bool ICollection>.Contains(KeyValuePair item) => - ((IDictionary) WrappedDictionary).Contains(item); + ((IDictionary)WrappedDictionary).Contains(item); /// void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) => - ((IDictionary) WrappedDictionary).CopyTo(array, arrayIndex); + ((IDictionary)WrappedDictionary).CopyTo(array, arrayIndex); /// bool ICollection>.Remove(KeyValuePair item) => - ((IDictionary) WrappedDictionary).Remove(item); + ((IDictionary)WrappedDictionary).Remove(item); /// public IEnumerator> GetEnumerator() => @@ -96,56 +97,56 @@ IEnumerator IEnumerable.GetEnumerator() => /// public void Add(object key, object value) => - ((IDictionary) WrappedDictionary).Add(key, value); + ((IDictionary)WrappedDictionary).Add(key, value); /// public bool Contains(object key) => - ((IDictionary) WrappedDictionary).Contains(key); + ((IDictionary)WrappedDictionary).Contains(key); /// IDictionaryEnumerator IDictionary.GetEnumerator() => - ((IDictionary) WrappedDictionary).GetEnumerator(); + ((IDictionary)WrappedDictionary).GetEnumerator(); /// public void Remove(object key) => - ((IDictionary) WrappedDictionary).Remove(key); + ((IDictionary)WrappedDictionary).Remove(key); /// public void CopyTo(Array array, int index) => - ((IDictionary) WrappedDictionary).CopyTo(array, index); + ((IDictionary)WrappedDictionary).CopyTo(array, index); #endregion #region IReadOnlyDictionary implementation - IEnumerable IReadOnlyDictionary.Keys => ((IReadOnlyDictionary) WrappedDictionary).Keys; + IEnumerable IReadOnlyDictionary.Keys => ((IReadOnlyDictionary)WrappedDictionary).Keys; - IEnumerable IReadOnlyDictionary.Values => ((IReadOnlyDictionary) WrappedDictionary).Values; + IEnumerable IReadOnlyDictionary.Values => ((IReadOnlyDictionary)WrappedDictionary).Values; #endregion #region IDictionary implementation /// - bool IDictionary.IsFixedSize => ((IDictionary) WrappedDictionary).IsFixedSize; + bool IDictionary.IsFixedSize => ((IDictionary)WrappedDictionary).IsFixedSize; /// - bool IDictionary.IsReadOnly => ((IDictionary) WrappedDictionary).IsReadOnly; + bool IDictionary.IsReadOnly => ((IDictionary)WrappedDictionary).IsReadOnly; - ICollection IDictionary.Keys => ((IDictionary) WrappedDictionary).Keys; + ICollection IDictionary.Keys => ((IDictionary)WrappedDictionary).Keys; - ICollection IDictionary.Values => ((IDictionary) WrappedDictionary).Values; + ICollection IDictionary.Values => ((IDictionary)WrappedDictionary).Values; /// - bool ICollection.IsSynchronized => ((IDictionary) WrappedDictionary).IsSynchronized; + bool ICollection.IsSynchronized => ((IDictionary)WrappedDictionary).IsSynchronized; /// - object ICollection.SyncRoot => ((IDictionary) WrappedDictionary).SyncRoot; + object ICollection.SyncRoot => ((IDictionary)WrappedDictionary).SyncRoot; /// object IDictionary.this[object key] { - get => ((IDictionary) WrappedDictionary)[key]; - set => ((IDictionary) WrappedDictionary)[key] = value; + get => ((IDictionary)WrappedDictionary)[key]; + set => ((IDictionary)WrappedDictionary)[key] = value; } #endregion diff --git a/src/Polly/Context.cs b/src/Polly/Context.cs index 1ef17826f0a..e66f3916370 100644 --- a/src/Polly/Context.cs +++ b/src/Polly/Context.cs @@ -49,7 +49,8 @@ public Guid CorrelationId { get { - if (!_correlationId.HasValue) { _correlationId = Guid.NewGuid(); } + _correlationId ??= Guid.NewGuid(); + return _correlationId.Value; } } diff --git a/src/Polly/Fallback/AsyncFallbackSyntax.cs b/src/Polly/Fallback/AsyncFallbackSyntax.cs index fc92303b087..c11741c422c 100644 --- a/src/Polly/Fallback/AsyncFallbackSyntax.cs +++ b/src/Polly/Fallback/AsyncFallbackSyntax.cs @@ -16,7 +16,8 @@ public static class AsyncFallbackSyntax /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); Func doNothing = _ => TaskHelper.EmptyTask; return policyBuilder.FallbackAsync( @@ -35,8 +36,10 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction, Func onFallbackAsync) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return policyBuilder.FallbackAsync( (_, _, ct) => fallbackAction(ct), @@ -54,8 +57,10 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction, Func onFallbackAsync) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return policyBuilder.FallbackAsync((_, ctx, ct) => fallbackAction(ctx, ct), onFallbackAsync); } @@ -71,8 +76,10 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func fallbackAction, Func onFallbackAsync) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return new AsyncFallbackPolicy(policyBuilder, onFallbackAsync, fallbackAction); } @@ -106,7 +113,8 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func> fallbackAction) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); Func, Task> doNothing = _ => TaskHelper.EmptyTask; return policyBuilder.FallbackAsync( @@ -124,7 +132,8 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, TResult fallbackValue, Func, Task> onFallbackAsync) { - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return policyBuilder.FallbackAsync( (_, _, _) => Task.FromResult(fallbackValue), @@ -142,8 +151,10 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func> fallbackAction, Func, Task> onFallbackAsync) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return policyBuilder.FallbackAsync( (_, _, ct) => fallbackAction(ct), @@ -160,7 +171,8 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, TResult fallbackValue, Func, Context, Task> onFallbackAsync) { - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return policyBuilder.FallbackAsync( (_, _, _) => Task.FromResult(fallbackValue), @@ -178,8 +190,10 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func> fallbackAction, Func, Context, Task> onFallbackAsync) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return policyBuilder.FallbackAsync((_, ctx, ct) => fallbackAction(ctx, ct), onFallbackAsync); } @@ -195,8 +209,10 @@ public static AsyncFallbackPolicy FallbackAsync(this PolicyBui /// The policy instance. public static AsyncFallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func, Context, CancellationToken, Task> fallbackAction, Func, Context, Task> onFallbackAsync) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallbackAsync == null) throw new ArgumentNullException(nameof(onFallbackAsync)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallbackAsync == null) + throw new ArgumentNullException(nameof(onFallbackAsync)); return new AsyncFallbackPolicy( policyBuilder, diff --git a/src/Polly/Fallback/FallbackSyntax.cs b/src/Polly/Fallback/FallbackSyntax.cs index d5744535b40..d1551c2e2fa 100644 --- a/src/Polly/Fallback/FallbackSyntax.cs +++ b/src/Polly/Fallback/FallbackSyntax.cs @@ -16,7 +16,8 @@ public static class FallbackSyntax /// The policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); Action doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -31,7 +32,8 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action f /// The policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); Action doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -48,8 +50,10 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, ActionThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, _, _) => fallbackAction(), (exception, _) => onFallback(exception)); } @@ -65,8 +69,10 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action f /// The policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, _, ct) => fallbackAction(ct), (exception, _) => onFallback(exception)); } @@ -82,8 +88,10 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, ActionThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, ctx, _) => fallbackAction(ctx), onFallback); } @@ -99,8 +107,10 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, ActionThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, ctx, ct) => fallbackAction(ctx, ct), onFallback); } @@ -116,8 +126,10 @@ public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, ActionThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action fallbackAction, Action onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return new FallbackPolicy( policyBuilder, @@ -152,7 +164,8 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Func fallbackAction) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); Action> doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -167,7 +180,8 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Func fallbackAction) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); Action> doNothing = _ => { }; return policyBuilder.Fallback(fallbackAction, doNothing); @@ -183,7 +197,8 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, TResult fallbackValue, Action> onFallback) { - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, _, _) => fallbackValue, (outcome, _) => onFallback(outcome)); } @@ -199,8 +214,10 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action> onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, _, _) => fallbackAction(), (outcome, _) => onFallback(outcome)); } @@ -216,8 +233,10 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action> onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, _, ct) => fallbackAction(ct), (outcome, _) => onFallback(outcome)); } @@ -232,7 +251,8 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, TResult fallbackValue, Action, Context> onFallback) { - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, _, _) => fallbackValue, onFallback); } @@ -248,8 +268,10 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action, Context> onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, ctx, _) => fallbackAction(ctx), onFallback); } @@ -265,8 +287,10 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Func fallbackAction, Action, Context> onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return policyBuilder.Fallback((_, ctx, ct) => fallbackAction(ctx, ct), onFallback); } @@ -282,8 +306,10 @@ public static FallbackPolicy Fallback(this PolicyBuilderThe policy instance. public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Func, Context, CancellationToken, TResult> fallbackAction, Action, Context> onFallback) { - if (fallbackAction == null) throw new ArgumentNullException(nameof(fallbackAction)); - if (onFallback == null) throw new ArgumentNullException(nameof(onFallback)); + if (fallbackAction == null) + throw new ArgumentNullException(nameof(fallbackAction)); + if (onFallback == null) + throw new ArgumentNullException(nameof(onFallback)); return new FallbackPolicy( policyBuilder, diff --git a/src/Polly/NoOp/AsyncNoOpPolicy.cs b/src/Polly/NoOp/AsyncNoOpPolicy.cs index 20a38963644..f75cddafd7e 100644 --- a/src/Polly/NoOp/AsyncNoOpPolicy.cs +++ b/src/Polly/NoOp/AsyncNoOpPolicy.cs @@ -13,7 +13,7 @@ internal AsyncNoOpPolicy() /// [DebuggerStepThrough] - protected override Task ImplementationAsync( Func> action, Context context, CancellationToken cancellationToken, + protected override Task ImplementationAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => NoOpEngine.ImplementationAsync(action, context, cancellationToken); } diff --git a/src/Polly/Policy.ContextAndKeys.cs b/src/Polly/Policy.ContextAndKeys.cs index 486934ac9ef..f1fbbc18eb0 100644 --- a/src/Polly/Policy.ContextAndKeys.cs +++ b/src/Polly/Policy.ContextAndKeys.cs @@ -9,7 +9,8 @@ public abstract partial class Policy /// The unique, used-definable key to assign to this instance. public Policy WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; @@ -22,7 +23,8 @@ public Policy WithPolicyKey(string policyKey) /// The unique, used-definable key to assign to this instance. ISyncPolicy ISyncPolicy.WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; @@ -38,7 +40,8 @@ public abstract partial class Policy /// The unique, used-definable key to assign to this instance. public Policy WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; @@ -51,7 +54,8 @@ public Policy WithPolicyKey(string policyKey) /// The unique, used-definable key to assign to this instance. ISyncPolicy ISyncPolicy.WithPolicyKey(string policyKey) { - if (policyKeyInternal != null) throw PolicyKeyMustBeImmutableException(nameof(policyKey)); + if (policyKeyInternal != null) + throw PolicyKeyMustBeImmutableException(nameof(policyKey)); policyKeyInternal = policyKey; return this; diff --git a/src/Polly/Policy.ExecuteOverloads.cs b/src/Polly/Policy.ExecuteOverloads.cs index edfa5f41d4f..fbaf63f6415 100644 --- a/src/Polly/Policy.ExecuteOverloads.cs +++ b/src/Polly/Policy.ExecuteOverloads.cs @@ -59,7 +59,8 @@ public void Execute(Action action, IDictionary action, Context context, CancellationToken cancellationToken) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -150,7 +151,8 @@ public TResult Execute(Func action [DebuggerStepThrough] public TResult Execute(Func action, Context context, CancellationToken cancellationToken) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -232,7 +234,8 @@ public PolicyResult ExecuteAndCapture(Action action, [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action action, Context context, CancellationToken cancellationToken) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); try { @@ -311,7 +314,8 @@ public PolicyResult ExecuteAndCapture(Func ExecuteAndCapture(Func action, Context context, CancellationToken cancellationToken) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); try { diff --git a/src/Polly/Policy.TResult.ExecuteOverloads.cs b/src/Polly/Policy.TResult.ExecuteOverloads.cs index 0ee2110329c..b118b195bd1 100644 --- a/src/Polly/Policy.TResult.ExecuteOverloads.cs +++ b/src/Polly/Policy.TResult.ExecuteOverloads.cs @@ -73,7 +73,8 @@ public TResult Execute(Func action, IDictio [DebuggerStepThrough] public TResult Execute(Func action, Context context, CancellationToken cancellationToken) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); @@ -154,7 +155,8 @@ public PolicyResult ExecuteAndCapture(Func ExecuteAndCapture(Func action, Context context, CancellationToken cancellationToken) { - if (context == null) throw new ArgumentNullException(nameof(context)); + if (context == null) + throw new ArgumentNullException(nameof(context)); try { diff --git a/src/Polly/PolicyBase.ContextAndKeys.cs b/src/Polly/PolicyBase.ContextAndKeys.cs index 8827cb266d8..05e866655cb 100644 --- a/src/Polly/PolicyBase.ContextAndKeys.cs +++ b/src/Polly/PolicyBase.ContextAndKeys.cs @@ -12,7 +12,7 @@ public abstract partial class PolicyBase /// public string PolicyKey => policyKeyInternal ?? (policyKeyInternal = GetType().Name + "-" + KeyHelper.GuidPart()); - internal static ArgumentException PolicyKeyMustBeImmutableException(string policyKeyParamName) => new ("PolicyKey cannot be changed once set; or (when using the default value after the PolicyKey property has been accessed.", policyKeyParamName); + internal static ArgumentException PolicyKeyMustBeImmutableException(string policyKeyParamName) => new("PolicyKey cannot be changed once set; or (when using the default value after the PolicyKey property has been accessed.", policyKeyParamName); /// /// Restores the supplied keys to the execution . diff --git a/src/Polly/PolicyBuilder.OrSyntax.cs b/src/Polly/PolicyBuilder.OrSyntax.cs index 9edbf05b7f8..43e5ab12922 100644 --- a/src/Polly/PolicyBuilder.OrSyntax.cs +++ b/src/Polly/PolicyBuilder.OrSyntax.cs @@ -73,8 +73,11 @@ internal static ExceptionPredicate HandleInner(Func predicate) private static Exception HandleInnerNested(Func predicate, Exception current) { - if (current == null) return null; - if (predicate(current)) return current; + if (current == null) + return null; + if (predicate(current)) + return current; + return HandleInnerNested(predicate, current.InnerException); } diff --git a/src/Polly/Polly.csproj b/src/Polly/Polly.csproj index a1685156cc6..e0c5dccb09f 100644 --- a/src/Polly/Polly.csproj +++ b/src/Polly/Polly.csproj @@ -6,7 +6,7 @@ Library 70 true - $(NoWarn);IDE0011;SA1501;S103;IDE0055;S3872;SA1402;SA1414;S3215;S2955 + $(NoWarn);IDE0011;S103;S3872;SA1402;SA1414;S3215;S2955 $(NoWarn);IDE1006;CA1062;S107;CA1068;S4039;CA1000;CA1063;CA1031;CA1051 $(NoWarn);SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;SA1618;CA1033 $(NoWarn);SA1515;S4023;CA1010;S2681;S3442;S3880;CA1064;SA1649;SA1625;SA1623;SA1118 diff --git a/src/Polly/RateLimit/AsyncRateLimitPolicy.cs b/src/Polly/RateLimit/AsyncRateLimitPolicy.cs index c98173e5ee3..86e8b0c2949 100644 --- a/src/Polly/RateLimit/AsyncRateLimitPolicy.cs +++ b/src/Polly/RateLimit/AsyncRateLimitPolicy.cs @@ -14,7 +14,7 @@ internal AsyncRateLimitPolicy(IRateLimiter rateLimiter) => /// [DebuggerStepThrough] - protected override Task ImplementationAsync(Func> action, Context context, CancellationToken cancellationToken, + protected override Task ImplementationAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => AsyncRateLimitEngine.ImplementationAsync(_rateLimiter, null, action, context, cancellationToken, continueOnCapturedContext); } diff --git a/src/Polly/RateLimit/AsyncRateLimitSyntax.cs b/src/Polly/RateLimit/AsyncRateLimitSyntax.cs index 10d7e119cde..7901f7524ce 100644 --- a/src/Polly/RateLimit/AsyncRateLimitSyntax.cs +++ b/src/Polly/RateLimit/AsyncRateLimitSyntax.cs @@ -28,9 +28,12 @@ public static AsyncRateLimitPolicy RateLimitAsync( TimeSpan perTimeSpan, int maxBurst) { - if (numberOfExecutions < 1) throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); - if (perTimeSpan <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); - if (maxBurst < 1) throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + if (numberOfExecutions < 1) + throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + if (perTimeSpan <= TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + if (maxBurst < 1) + throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs b/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs index 0b283f15bd0..284d2d9fa07 100644 --- a/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs +++ b/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs @@ -64,9 +64,12 @@ public static AsyncRateLimitPolicy RateLimitAsync( int maxBurst, Func? retryAfterFactory) { - if (numberOfExecutions < 1) throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); - if (perTimeSpan <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); - if (maxBurst < 1) throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + if (numberOfExecutions < 1) + throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + if (perTimeSpan <= TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + if (maxBurst < 1) + throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs index dc33e21459a..917fd37f3c7 100644 --- a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs +++ b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs @@ -27,8 +27,10 @@ internal sealed class LockFreeTokenBucketRateLimiter : IRateLimiter /// public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity) { - if (onePer <= TimeSpan.Zero) 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."); + if (onePer <= TimeSpan.Zero) + 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; this.bucketCapacity = bucketCapacity; @@ -66,12 +68,9 @@ public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity) // Time to add tokens to the bucket! - // We definitely need to add one token. In fact, if we haven't hit this bit of code for a while, we might be due to add a bunch of tokens. - long tokensMissedAdding = - // Passing addNextTokenAtTicks merits one token - 1 + - // And any whole token tick intervals further each merit another. - (-ticksTillAddNextToken / addTokenTickInterval); + // We definitely need to add one token. In fact, if we haven't hit this bit of code for a while, we might be due to add a bunch of tokens. + // Passing addNextTokenAtTicks merits one token and any whole token tick intervals further each merit another. + long tokensMissedAdding = 1 + (-ticksTillAddNextToken / addTokenTickInterval); // We mustn't exceed bucket capacity though. long tokensToAdd = Math.Min(bucketCapacity, tokensMissedAdding); @@ -103,9 +102,9 @@ public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity) // We want any thread refilling the bucket to have a chance to do so before we try to grab the next token. #if NETSTANDARD2_0 - Thread.Sleep(0); + Thread.Sleep(0); #else - spinner.SpinOnce(); + spinner.SpinOnce(); #endif } } diff --git a/src/Polly/RateLimit/RateLimitRejectedException.cs b/src/Polly/RateLimit/RateLimitRejectedException.cs index 85d38a2f78a..ec85760f6eb 100644 --- a/src/Polly/RateLimit/RateLimitRejectedException.cs +++ b/src/Polly/RateLimit/RateLimitRejectedException.cs @@ -60,7 +60,8 @@ private static string DefaultMessage(TimeSpan retryAfter) => private void SetRetryAfter(TimeSpan retryAfter) { - if (retryAfter < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(retryAfter), retryAfter, $"The {nameof(retryAfter)} parameter must be a TimeSpan greater than or equal to TimeSpan.Zero."); + if (retryAfter < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(retryAfter), retryAfter, $"The {nameof(retryAfter)} parameter must be a TimeSpan greater than or equal to TimeSpan.Zero."); RetryAfter = retryAfter; } diff --git a/src/Polly/RateLimit/RateLimitSyntax.cs b/src/Polly/RateLimit/RateLimitSyntax.cs index 90a449717ca..8da7ec127e3 100644 --- a/src/Polly/RateLimit/RateLimitSyntax.cs +++ b/src/Polly/RateLimit/RateLimitSyntax.cs @@ -28,9 +28,12 @@ public static RateLimitPolicy RateLimit( TimeSpan perTimeSpan, int maxBurst) { - if (numberOfExecutions < 1) throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); - if (perTimeSpan <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); - if (maxBurst < 1) throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + if (numberOfExecutions < 1) + throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + if (perTimeSpan <= TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + if (maxBurst < 1) + throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/RateLimit/RateLimitTResultSyntax.cs b/src/Polly/RateLimit/RateLimitTResultSyntax.cs index 6aa805b6431..dd7312ca036 100644 --- a/src/Polly/RateLimit/RateLimitTResultSyntax.cs +++ b/src/Polly/RateLimit/RateLimitTResultSyntax.cs @@ -64,9 +64,12 @@ public static RateLimitPolicy RateLimit( int maxBurst, Func? retryAfterFactory) { - if (numberOfExecutions < 1) throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); - if (perTimeSpan <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); - if (maxBurst < 1) throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); + if (numberOfExecutions < 1) + throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1."); + if (perTimeSpan <= TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan."); + if (maxBurst < 1) + throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1."); var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions); diff --git a/src/Polly/Registry/PolicyRegistry.cs b/src/Polly/Registry/PolicyRegistry.cs index fb57d7da094..b9c874ea55a 100644 --- a/src/Polly/Registry/PolicyRegistry.cs +++ b/src/Polly/Registry/PolicyRegistry.cs @@ -92,7 +92,7 @@ public IsPolicy this[string key] /// is null. /// The given key was not present in the registry. public TPolicy Get(string key) - where TPolicy : IsPolicy => (TPolicy) _registry[key]; + where TPolicy : IsPolicy => (TPolicy)_registry[key]; /// /// Gets the policy stored under the provided key, casting to . @@ -154,7 +154,7 @@ public bool TryRemove(string key, out TPolicy policy) var registry = ThrowIfNotConcurrentImplementation(); bool got = registry.TryRemove(key, out IsPolicy value); - policy = got ? (TPolicy) value : default; + policy = got ? (TPolicy)value : default; return got; } @@ -191,7 +191,7 @@ public TPolicy GetOrAdd(string key, Func policyFactory { var registry = ThrowIfNotConcurrentImplementation(); - return (TPolicy) registry.GetOrAdd(key, k => policyFactory(k)); + return (TPolicy)registry.GetOrAdd(key, k => policyFactory(k)); } /// @@ -207,7 +207,7 @@ public TPolicy GetOrAdd(string key, TPolicy policy) { var registry = ThrowIfNotConcurrentImplementation(); - return (TPolicy) registry.GetOrAdd(key, policy); + return (TPolicy)registry.GetOrAdd(key, policy); } /// @@ -226,7 +226,7 @@ public TPolicy AddOrUpdate(string key, Func addPolicyF { var registry = ThrowIfNotConcurrentImplementation(); - return (TPolicy) registry.AddOrUpdate(key, k => addPolicyFactory(k), (k, e) => updatePolicyFactory(k, (TPolicy)e)); + return (TPolicy)registry.AddOrUpdate(key, k => addPolicyFactory(k), (k, e) => updatePolicyFactory(k, (TPolicy)e)); } /// diff --git a/src/Polly/ResultPredicates.cs b/src/Polly/ResultPredicates.cs index 6be1aa99fe9..ebe34ed8541 100644 --- a/src/Polly/ResultPredicates.cs +++ b/src/Polly/ResultPredicates.cs @@ -20,7 +20,8 @@ internal void Add(ResultPredicate predicate) /// The value to assess against the predicates. public bool AnyMatch(TResult result) { - if (_predicates == null) return false; + if (_predicates == null) + return false; return _predicates.Any(predicate => predicate(result)); } diff --git a/src/Polly/Retry/AsyncRetryEngine.cs b/src/Polly/Retry/AsyncRetryEngine.cs index da3844a140f..6580a49c169 100644 --- a/src/Polly/Retry/AsyncRetryEngine.cs +++ b/src/Polly/Retry/AsyncRetryEngine.cs @@ -64,7 +64,10 @@ internal static async Task ImplementationAsync( outcome = new DelegateResult(handledException); } - if (tryCount < int.MaxValue) { tryCount++; } + if (tryCount < int.MaxValue) + { + tryCount++; + } TimeSpan waitDuration = sleepDurationsEnumerator?.Current ?? (sleepDurationProvider?.Invoke(tryCount, outcome, context) ?? TimeSpan.Zero); diff --git a/src/Polly/Retry/AsyncRetrySyntax.cs b/src/Polly/Retry/AsyncRetrySyntax.cs index 8bcecee23d1..363306c94a8 100644 --- a/src/Polly/Retry/AsyncRetrySyntax.cs +++ b/src/Polly/Retry/AsyncRetrySyntax.cs @@ -63,7 +63,8 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, Func /// onRetry public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, onRetryAsync: async (outcome, i, _) => onRetry(outcome, i)); @@ -82,7 +83,8 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int /// onRetryAsync public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.RetryAsync(retryCount, onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -121,7 +123,8 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, Func /// onRetry public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, @@ -141,8 +144,10 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int /// onRetryAsync public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func onRetryAsync) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -172,7 +177,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -190,7 +196,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -208,7 +215,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.RetryForeverAsync(onRetryAsync: (Exception outcome, Context _) => onRetryAsync(outcome)); } @@ -223,7 +231,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.RetryForeverAsync(onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -238,7 +247,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -256,7 +266,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -274,7 +285,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -291,7 +303,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilde /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -333,7 +346,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -362,7 +376,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -392,7 +407,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -422,7 +438,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( retryCount, @@ -449,7 +466,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -479,9 +497,12 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func onRetryAsync) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); IEnumerable sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -513,7 +534,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -543,7 +565,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( retryCount, @@ -570,7 +593,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -600,7 +624,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryAsync( retryCount, (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -627,9 +652,12 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func onRetryAsync) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -670,7 +698,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -696,7 +725,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -720,7 +750,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -746,7 +777,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -770,7 +802,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -796,8 +829,10 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func onRetryAsync) { - if (sleepDurations == null) throw new ArgumentNullException(nameof(sleepDurations)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurations == null) + throw new ArgumentNullException(nameof(sleepDurations)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -816,7 +851,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilde /// sleepDurationProvider public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action doNothing = (_, _) => { }; @@ -834,7 +870,8 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// sleepDurationProvider public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action doNothing = (_, _, _) => { }; @@ -855,8 +892,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -877,8 +916,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -899,8 +940,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -921,8 +964,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -943,8 +988,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -967,8 +1014,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -991,7 +1040,9 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), onRetryAsync); @@ -1011,7 +1062,8 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -1032,8 +1084,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -1055,8 +1109,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder polic /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, diff --git a/src/Polly/Retry/AsyncRetryTResultSyntax.cs b/src/Polly/Retry/AsyncRetryTResultSyntax.cs index 19c18ff4064..195e4ea3626 100644 --- a/src/Polly/Retry/AsyncRetryTResultSyntax.cs +++ b/src/Polly/Retry/AsyncRetryTResultSyntax.cs @@ -62,7 +62,8 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilderonRetry public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action, int> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, @@ -82,7 +83,8 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilderonRetryAsync public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func, int, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.RetryAsync(retryCount, onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -121,7 +123,8 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilderonRetry public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action, int, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryAsync(retryCount, @@ -141,8 +144,10 @@ public static AsyncRetryPolicy RetryAsync(this PolicyBuilderonRetryAsync public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func, int, Context, Task> onRetryAsync) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -172,7 +177,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -190,7 +196,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action, int> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -208,7 +215,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.RetryForeverAsync(onRetryAsync: (DelegateResult outcome, Context _) => onRetryAsync(outcome)); } @@ -223,7 +231,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, int, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.RetryForeverAsync(onRetryAsync: (outcome, i, _) => onRetryAsync(outcome, i)); } @@ -238,7 +247,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -256,7 +266,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetry public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action, int, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.RetryForeverAsync( @@ -274,7 +285,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, Context, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -291,7 +303,8 @@ public static AsyncRetryPolicy RetryForeverAsync(this PolicyBu /// onRetryAsync public static AsyncRetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Func, int, Context, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -334,7 +347,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -363,7 +377,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -393,7 +408,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -422,7 +438,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( retryCount, @@ -449,7 +466,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -479,9 +497,12 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, int, Context, Task> onRetryAsync) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); IEnumerable sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -513,7 +534,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -542,7 +564,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( retryCount, @@ -569,7 +592,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -599,7 +623,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Func, TimeSpan, int, Context, Task> onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryAsync( retryCount, @@ -627,9 +652,12 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func, Context, TimeSpan> sleepDurationProvider, Func, TimeSpan, int, Context, Task> onRetryAsync) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -670,7 +698,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -696,7 +725,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func, TimeSpan, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -720,7 +750,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -746,7 +777,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func, TimeSpan, Context, Task> onRetryAsync) { - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryAsync( sleepDurations, @@ -770,7 +802,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, int, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryAsync( @@ -796,8 +829,10 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Func, TimeSpan, int, Context, Task> onRetryAsync) { - if (sleepDurations == null) throw new ArgumentNullException(nameof(sleepDurations)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurations == null) + throw new ArgumentNullException(nameof(sleepDurations)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -816,7 +851,8 @@ public static AsyncRetryPolicy WaitAndRetryAsync(this PolicyBu /// sleepDurationProvider public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action, TimeSpan> doNothing = (_, _) => { }; @@ -834,7 +870,8 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// sleepDurationProvider public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action, TimeSpan, Context> doNothing = (_, _, _) => { }; @@ -855,8 +892,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -877,8 +916,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -899,8 +940,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, TimeSpan, Task> onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -921,8 +964,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, int, TimeSpan, Task> onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return policyBuilder.WaitAndRetryForeverAsync( (retryCount, _) => sleepDurationProvider(retryCount), @@ -943,7 +988,8 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -966,7 +1012,8 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetry public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); #pragma warning disable 1998 // async method has no awaits, will run synchronously return policyBuilder.WaitAndRetryForeverAsync( @@ -989,7 +1036,8 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -1010,7 +1058,8 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Func, int, TimeSpan, Context, Task> onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryForeverAsync( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -1031,8 +1080,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Func, TimeSpan, Context, Task> onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, @@ -1054,8 +1105,10 @@ public static AsyncRetryPolicy WaitAndRetryForeverAsync(this P /// onRetryAsync public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Func, int, TimeSpan, Context, Task> onRetryAsync) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetryAsync == null) throw new ArgumentNullException(nameof(onRetryAsync)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetryAsync == null) + throw new ArgumentNullException(nameof(onRetryAsync)); return new AsyncRetryPolicy( policyBuilder, diff --git a/src/Polly/Retry/RetryEngine.cs b/src/Polly/Retry/RetryEngine.cs index aff987d1a4a..6a8000b5e53 100644 --- a/src/Polly/Retry/RetryEngine.cs +++ b/src/Polly/Retry/RetryEngine.cs @@ -63,7 +63,10 @@ internal static TResult Implementation( outcome = new DelegateResult(handledException); } - if (tryCount < int.MaxValue) { tryCount++; } + if (tryCount < int.MaxValue) + { + tryCount++; + } TimeSpan waitDuration = sleepDurationsEnumerator?.Current ?? (sleepDurationProvider?.Invoke(tryCount, outcome, context) ?? TimeSpan.Zero); diff --git a/src/Polly/Retry/RetrySyntax.cs b/src/Polly/Retry/RetrySyntax.cs index 5293b0933ae..73fe25cec0a 100644 --- a/src/Polly/Retry/RetrySyntax.cs +++ b/src/Polly/Retry/RetrySyntax.cs @@ -49,8 +49,10 @@ public static RetryPolicy Retry(this PolicyBuilder policyBuilder, ActiononRetry public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.Retry(retryCount, (outcome, i, _) => onRetry(outcome, i)); } @@ -78,8 +80,10 @@ public static RetryPolicy Retry(this PolicyBuilder policyBuilder, ActiononRetry public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -109,7 +113,8 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder) /// onRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.RetryForever((Exception outcome, Context _) => onRetry(outcome)); } @@ -124,7 +129,8 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action< /// onRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.RetryForever((outcome, i, _) => onRetry(outcome, i)); } @@ -139,11 +145,12 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action< /// onRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); - return new RetryPolicy( - policyBuilder, - (outcome, _, _, ctx) => onRetry(outcome, ctx)); + return new RetryPolicy( + policyBuilder, + (outcome, _, _, ctx) => onRetry(outcome, ctx)); } /// @@ -156,7 +163,8 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action< /// onRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -172,7 +180,7 @@ public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action< /// The retry count. /// The function that provides the duration to wait for for a particular retry attempt. /// The policy instance. - public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider) + public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider) { Action doNothing = (_, _, _, _) => { }; @@ -196,9 +204,10 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int re /// or /// onRetry /// - public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) + public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry( retryCount, @@ -225,7 +234,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int re /// public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry( retryCount, @@ -252,9 +262,12 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret /// public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); var sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -301,7 +314,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret /// public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry( retryCount, @@ -328,7 +342,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret /// public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetry( retryCount, (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -354,15 +369,18 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret /// public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); - - return new RetryPolicy( - policyBuilder, - onRetry, - retryCount, - sleepDurationProvider: sleepDurationProvider); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); + + return new RetryPolicy( + policyBuilder, + onRetry, + retryCount, + sleepDurationProvider: sleepDurationProvider); } /// @@ -372,7 +390,7 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int ret /// The policy builder. /// The sleep durations to wait for on each retry. /// The policy instance. - public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations) + public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations) { Action doNothing = (_, _) => { }; @@ -393,9 +411,10 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnume /// or /// onRetry /// - public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) + public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, _) => onRetry(outcome, span)); } @@ -416,7 +435,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnume /// public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, ctx) => onRetry(outcome, span, ctx)); } @@ -437,8 +457,10 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumer /// public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action onRetry) { - if (sleepDurations == null) throw new ArgumentNullException(nameof(sleepDurations)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurations == null) + throw new ArgumentNullException(nameof(sleepDurations)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -457,7 +479,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumer /// sleepDurationProvider public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action doNothing = (_, _) => { }; @@ -475,7 +498,8 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, /// sleepDurationProvider public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action doNothing = (_, _, _) => { }; @@ -496,8 +520,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForever( (retryCount, _) => sleepDurationProvider(retryCount), @@ -518,8 +544,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForever( (retryCount, _, _) => sleepDurationProvider(retryCount), @@ -540,7 +568,8 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -561,7 +590,8 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -582,8 +612,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -605,8 +637,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, diff --git a/src/Polly/Retry/RetryTResultSyntax.cs b/src/Polly/Retry/RetryTResultSyntax.cs index e8f3c648263..95a7d8905ea 100644 --- a/src/Polly/Retry/RetryTResultSyntax.cs +++ b/src/Polly/Retry/RetryTResultSyntax.cs @@ -49,8 +49,10 @@ public static RetryPolicy Retry(this PolicyBuilder po /// onRetry public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action, int> onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.Retry(retryCount, (outcome, i, _) => onRetry(outcome, i)); } @@ -78,8 +80,10 @@ public static RetryPolicy Retry(this PolicyBuilder po /// onRetry public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action, int, Context> onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -109,7 +113,8 @@ public static RetryPolicy RetryForever(this PolicyBuilderonRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.RetryForever((DelegateResult outcome, Context _) => onRetry(outcome)); } @@ -124,7 +129,8 @@ public static RetryPolicy RetryForever(this PolicyBuilderonRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action, int> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.RetryForever((outcome, i, _) => onRetry(outcome, i)); } @@ -139,7 +145,8 @@ public static RetryPolicy RetryForever(this PolicyBuilderonRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -156,7 +163,8 @@ public static RetryPolicy RetryForever(this PolicyBuilderonRetry public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action, int, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -198,7 +206,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry( retryCount, @@ -225,7 +234,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry( retryCount, @@ -252,9 +262,12 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); var sleepDurations = Enumerable.Range(1, retryCount) .Select(sleepDurationProvider); @@ -301,7 +314,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry( retryCount, @@ -367,7 +381,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func, Context, TimeSpan> sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry( retryCount, @@ -394,9 +409,12 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func, Context, TimeSpan> sleepDurationProvider, Action, TimeSpan, int, Context> onRetry) { - if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (retryCount < 0) + throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero."); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -435,7 +453,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, _) => onRetry(outcome, span)); } @@ -456,7 +475,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, Context> onRetry) { - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, ctx) => onRetry(outcome, span, ctx)); } @@ -477,8 +497,10 @@ public static RetryPolicy WaitAndRetry(this PolicyBuilder public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable sleepDurations, Action, TimeSpan, int, Context> onRetry) { - if (sleepDurations == null) throw new ArgumentNullException(nameof(sleepDurations)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurations == null) + throw new ArgumentNullException(nameof(sleepDurations)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -497,7 +519,8 @@ public static RetryPolicy WaitAndRetry(this PolicyBuildersleepDurationProvider public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action, TimeSpan> doNothing = (_, _) => { }; @@ -515,7 +538,8 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild /// sleepDurationProvider public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); Action, TimeSpan, Context> doNothing = (_, _, _) => { }; @@ -536,8 +560,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForever( (retryCount, _) => sleepDurationProvider(retryCount), @@ -558,8 +584,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return policyBuilder.WaitAndRetryForever( (retryCount, _, _) => sleepDurationProvider(retryCount), @@ -580,7 +608,8 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -601,7 +630,8 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func sleepDurationProvider, Action, int, TimeSpan, Context> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); return policyBuilder.WaitAndRetryForever( (i, _, ctx) => sleepDurationProvider(i, ctx), @@ -622,8 +652,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Action, TimeSpan, Context> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, @@ -645,8 +677,10 @@ public static RetryPolicy WaitAndRetryForever(this PolicyBuild /// onRetry public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func, Context, TimeSpan> sleepDurationProvider, Action, int, TimeSpan, Context> onRetry) { - if (sleepDurationProvider == null) throw new ArgumentNullException(nameof(sleepDurationProvider)); - if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); + if (sleepDurationProvider == null) + throw new ArgumentNullException(nameof(sleepDurationProvider)); + if (onRetry == null) + throw new ArgumentNullException(nameof(onRetry)); return new RetryPolicy( policyBuilder, diff --git a/src/Polly/Timeout/AsyncTimeoutSyntax.cs b/src/Polly/Timeout/AsyncTimeoutSyntax.cs index 89ec2e51b43..5c53c30ba9c 100644 --- a/src/Polly/Timeout/AsyncTimeoutSyntax.cs +++ b/src/Polly/Timeout/AsyncTimeoutSyntax.cs @@ -44,7 +44,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Func onTimeoutAsync) { TimeoutValidator.ValidateSecondsTimeout(seconds); - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -60,8 +61,10 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, FunconTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Func onTimeoutAsync) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -95,7 +98,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeo /// seconds;Value must be greater than zero. public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeoutAsync); } @@ -203,7 +207,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy /// The policy instance. public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Func doNothingAsync = (_, _, _, _) => TaskHelper.EmptyTask; return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothingAsync); @@ -218,7 +223,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider) /// timeoutProvider public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Func doNothingAsync = (_, _, _, _) => TaskHelper.EmptyTask; return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, doNothingAsync); @@ -235,7 +241,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Ti /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -251,7 +258,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Fu /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -268,7 +276,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Fu /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -285,7 +294,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Ti /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -353,7 +363,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutPro /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return TimeoutAsync(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeoutAsync(ctx, timeout, task)); } @@ -373,8 +384,10 @@ public static AsyncTimeoutPolicy TimeoutAsync( TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return new AsyncTimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs b/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs index 0d5598e60d1..a4a911b0771 100644 --- a/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs +++ b/src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs @@ -58,7 +58,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Fun /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Func onTimeoutAsync) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -92,7 +93,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(int seconds, Tim /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(int seconds, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); return TimeoutAsync(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeoutAsync); } @@ -138,7 +140,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, Func onTimeoutAsync) { TimeoutValidator.ValidateTimeSpanTimeout(timeout); - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return TimeoutAsync(_ => timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -154,8 +157,10 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, Func onTimeoutAsync) { - if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeout)); - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (timeout <= TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(timeout)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return TimeoutAsync(_ => timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -189,7 +194,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeout)); + if (timeout <= TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(timeout)); return TimeoutAsync(_ => timeout, timeoutStrategy, onTimeoutAsync); } @@ -202,7 +208,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout /// The policy instance. public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Func doNothingAsync = (_, _, _, _) => Task.FromResult(default(TResult)); return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothingAsync); @@ -217,7 +224,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t /// The policy instance. public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Func doNothingAsync = (_, _, _, _) => Task.FromResult(default(TResult)); return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, doNothingAsync); @@ -234,7 +242,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -250,7 +259,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeoutAsync); } @@ -267,7 +277,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -284,7 +295,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(Func t /// onTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return TimeoutAsync(_ => timeoutProvider(), timeoutStrategy, onTimeoutAsync); } @@ -350,7 +362,8 @@ public static AsyncTimeoutPolicy TimeoutAsync(FunconTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return TimeoutAsync(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeoutAsync(ctx, timeout, task)); } @@ -367,8 +380,10 @@ public static AsyncTimeoutPolicy TimeoutAsync(FunconTimeoutAsync public static AsyncTimeoutPolicy TimeoutAsync(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Func onTimeoutAsync) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); - if (onTimeoutAsync == null) throw new ArgumentNullException(nameof(onTimeoutAsync)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); + if (onTimeoutAsync == null) + throw new ArgumentNullException(nameof(onTimeoutAsync)); return new AsyncTimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/TimeoutSyntax.cs b/src/Polly/Timeout/TimeoutSyntax.cs index 505a4a40552..2db2fd33588 100644 --- a/src/Polly/Timeout/TimeoutSyntax.cs +++ b/src/Polly/Timeout/TimeoutSyntax.cs @@ -58,7 +58,8 @@ public static TimeoutPolicy Timeout(int seconds, Action /// onTimeout public static TimeoutPolicy Timeout(int seconds, Action onTimeout) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); return Timeout(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeout); } @@ -92,7 +93,8 @@ public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy /// onTimeout public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); return Timeout(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeout); } @@ -200,7 +202,8 @@ public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutStrategy timeoutStr /// The policy instance. public static TimeoutPolicy Timeout(Func timeoutProvider) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothing); @@ -215,7 +218,8 @@ public static TimeoutPolicy Timeout(Func timeoutProvider) /// timeoutProvider public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), timeoutStrategy, doNothing); @@ -232,7 +236,8 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrat /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -248,7 +253,8 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, ActiononTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -265,7 +271,8 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, ActiononTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -282,7 +289,8 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrat /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -348,7 +356,8 @@ public static TimeoutPolicy Timeout(Func timeoutProvider, Act /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (onTimeout == null) throw new ArgumentNullException(nameof(onTimeout)); + if (onTimeout == null) + throw new ArgumentNullException(nameof(onTimeout)); return Timeout(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeout(ctx, timeout, task)); } @@ -368,8 +377,10 @@ public static TimeoutPolicy Timeout( TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); - if (onTimeout == null) throw new ArgumentNullException(nameof(onTimeout)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); + if (onTimeout == null) + throw new ArgumentNullException(nameof(onTimeout)); return new TimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/TimeoutTResultSyntax.cs b/src/Polly/Timeout/TimeoutTResultSyntax.cs index 4451e0ebc36..1fbdcab8d48 100644 --- a/src/Polly/Timeout/TimeoutTResultSyntax.cs +++ b/src/Polly/Timeout/TimeoutTResultSyntax.cs @@ -59,7 +59,9 @@ public static TimeoutPolicy Timeout(int seconds, ActiononTimeout public static TimeoutPolicy Timeout(int seconds, Action onTimeout) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); + return Timeout(_ => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeout); } @@ -94,7 +96,8 @@ public static TimeoutPolicy Timeout(int seconds, TimeoutStrate /// onTimeout public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); return Timeout(_ => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeout); } @@ -201,7 +204,8 @@ public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutS /// The policy instance. public static TimeoutPolicy Timeout(Func timeoutProvider) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, doNothing); @@ -217,7 +221,8 @@ public static TimeoutPolicy Timeout(Func timeoutProv /// timeoutProvider public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); Action doNothing = (_, _, _, _) => { }; return Timeout(_ => timeoutProvider(), timeoutStrategy, doNothing); @@ -234,7 +239,8 @@ public static TimeoutPolicy Timeout(Func timeoutProv /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -250,7 +256,8 @@ public static TimeoutPolicy Timeout(Func timeoutProv /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout); } @@ -268,7 +275,8 @@ public static TimeoutPolicy Timeout(Func timeoutProv /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -286,7 +294,8 @@ public static TimeoutPolicy Timeout(Func timeoutProv /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); return Timeout(_ => timeoutProvider(), timeoutStrategy, onTimeout); } @@ -354,7 +363,8 @@ public static TimeoutPolicy Timeout(Func ti /// onTimeout public static TimeoutPolicy Timeout(Func timeoutProvider, TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (onTimeout == null) throw new ArgumentNullException(nameof(onTimeout)); + if (onTimeout == null) + throw new ArgumentNullException(nameof(onTimeout)); return Timeout(timeoutProvider, timeoutStrategy, (ctx, timeout, task, _) => onTimeout(ctx, timeout, task)); } @@ -375,8 +385,10 @@ public static TimeoutPolicy Timeout( TimeoutStrategy timeoutStrategy, Action onTimeout) { - if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider)); - if (onTimeout == null) throw new ArgumentNullException(nameof(onTimeout)); + if (timeoutProvider == null) + throw new ArgumentNullException(nameof(timeoutProvider)); + if (onTimeout == null) + throw new ArgumentNullException(nameof(onTimeout)); return new TimeoutPolicy( timeoutProvider, diff --git a/src/Polly/Timeout/TimeoutValidator.cs b/src/Polly/Timeout/TimeoutValidator.cs index b1bfe40d7a5..d240e983e46 100644 --- a/src/Polly/Timeout/TimeoutValidator.cs +++ b/src/Polly/Timeout/TimeoutValidator.cs @@ -4,7 +4,8 @@ internal static class TimeoutValidator { internal static void ValidateSecondsTimeout(int seconds) { - if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds)); + if (seconds <= 0) + throw new ArgumentOutOfRangeException(nameof(seconds)); } internal static void ValidateTimeSpanTimeout(TimeSpan timeout) diff --git a/src/Polly/Utilities/SystemClock.cs b/src/Polly/Utilities/SystemClock.cs index bfa4636c8f9..3b51db4cd96 100644 --- a/src/Polly/Utilities/SystemClock.cs +++ b/src/Polly/Utilities/SystemClock.cs @@ -12,7 +12,8 @@ public static class SystemClock /// public static Action Sleep = (timeSpan, cancellationToken) => { - if (cancellationToken.WaitHandle.WaitOne(timeSpan)) cancellationToken.ThrowIfCancellationRequested(); + if (cancellationToken.WaitHandle.WaitOne(timeSpan)) + cancellationToken.ThrowIfCancellationRequested(); }; /// @@ -47,7 +48,8 @@ public static void Reset() { Sleep = (timeSpan, cancellationToken) => { - if (cancellationToken.WaitHandle.WaitOne(timeSpan)) cancellationToken.ThrowIfCancellationRequested(); + if (cancellationToken.WaitHandle.WaitOne(timeSpan)) + cancellationToken.ThrowIfCancellationRequested(); }; SleepAsync = Task.Delay; diff --git a/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs b/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs index 4dc57e16620..c9d9f8a92f5 100644 --- a/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs +++ b/src/Polly/Wrap/AsyncPolicyWrap.ContextAndKeys.cs @@ -13,7 +13,8 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyWrapKey = executionContext.PolicyWrapKey; priorPolicyKey = executionContext.PolicyKey; - if (executionContext.PolicyWrapKey == null) executionContext.PolicyWrapKey = PolicyKey; + if (executionContext.PolicyWrapKey == null) + executionContext.PolicyWrapKey = PolicyKey; base.SetPolicyContext(executionContext, out _, out _); } @@ -32,7 +33,8 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyWrapKey = executionContext.PolicyWrapKey; priorPolicyKey = executionContext.PolicyKey; - if (executionContext.PolicyWrapKey == null) executionContext.PolicyWrapKey = PolicyKey; + if (executionContext.PolicyWrapKey == null) + executionContext.PolicyWrapKey = PolicyKey; base.SetPolicyContext(executionContext, out _, out _); } diff --git a/src/Polly/Wrap/AsyncPolicyWrap.cs b/src/Polly/Wrap/AsyncPolicyWrap.cs index 7815897b330..e1cb77f7f54 100644 --- a/src/Polly/Wrap/AsyncPolicyWrap.cs +++ b/src/Polly/Wrap/AsyncPolicyWrap.cs @@ -68,12 +68,12 @@ public partial class AsyncPolicyWrap : AsyncPolicy, IPolicyWra /// /// Returns the outer in this /// - public IsPolicy Outer => (IsPolicy) _outerGeneric ?? _outerNonGeneric; + public IsPolicy Outer => (IsPolicy)_outerGeneric ?? _outerNonGeneric; /// /// Returns the next inner in this /// - public IsPolicy Inner => (IsPolicy) _innerGeneric ?? _innerNonGeneric; + public IsPolicy Inner => (IsPolicy)_innerGeneric ?? _innerNonGeneric; internal AsyncPolicyWrap(AsyncPolicy outer, IAsyncPolicy inner) : base(outer.ExceptionPredicates, ResultPredicates.None) diff --git a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs index d78f1522be7..0bb2cc19367 100644 --- a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs +++ b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs @@ -13,7 +13,8 @@ public static class IAsyncPolicyPolicyWrapExtensions /// A instance representing the combined wrap. public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } @@ -25,7 +26,8 @@ public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPol /// A instance representing the combined wrap. public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } @@ -37,7 +39,8 @@ public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy oute /// A instance representing the combined wrap. public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } @@ -49,7 +52,8 @@ public static AsyncPolicyWrap WrapAsync(this IAsyncPolicyA instance representing the combined wrap. public static AsyncPolicyWrap WrapAsync(this IAsyncPolicy outerPolicy, IAsyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((AsyncPolicy)outerPolicy).WrapAsync(innerPolicy); } } @@ -63,7 +67,8 @@ public partial class AsyncPolicy /// PolicyWrap.PolicyWrap. public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new AsyncPolicyWrap( this, @@ -78,7 +83,8 @@ public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) /// PolicyWrap.PolicyWrap. public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new AsyncPolicyWrap( this, @@ -95,7 +101,8 @@ public partial class AsyncPolicy /// PolicyWrap.PolicyWrap. public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new AsyncPolicyWrap( this, @@ -109,7 +116,8 @@ public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) /// PolicyWrap.PolicyWrap. public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new AsyncPolicyWrap( this, diff --git a/src/Polly/Wrap/IPolicyWrapExtension.cs b/src/Polly/Wrap/IPolicyWrapExtension.cs index cc5dba8f054..cfe7d6f2100 100644 --- a/src/Polly/Wrap/IPolicyWrapExtension.cs +++ b/src/Polly/Wrap/IPolicyWrapExtension.cs @@ -47,7 +47,9 @@ public static IEnumerable GetPolicies(this IPolicyWrap policyW /// An of all the policies of the given type, matching the filter. public static IEnumerable GetPolicies(this IPolicyWrap policyWrap, Func filter) { - if (filter == null) throw new ArgumentNullException(nameof(filter)); + if (filter == null) + throw new ArgumentNullException(nameof(filter)); + return policyWrap.GetPolicies().OfType().Where(filter); } @@ -71,7 +73,9 @@ public static TPolicy GetPolicy(this IPolicyWrap policyWrap) => /// InvalidOperationException, if more than one policy of the type is found in the wrap. public static TPolicy GetPolicy(this IPolicyWrap policyWrap, Func filter) { - if (filter == null) throw new ArgumentNullException(nameof(filter)); + if (filter == null) + throw new ArgumentNullException(nameof(filter)); + return policyWrap.GetPolicies().OfType().SingleOrDefault(filter); } } diff --git a/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs b/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs index 6ed12582402..90b9468346f 100644 --- a/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs +++ b/src/Polly/Wrap/PolicyWrap.ContextAndKeys.cs @@ -13,7 +13,8 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyWrapKey = executionContext.PolicyWrapKey; priorPolicyKey = executionContext.PolicyKey; - if (executionContext.PolicyWrapKey == null) executionContext.PolicyWrapKey = PolicyKey; + if (executionContext.PolicyWrapKey == null) + executionContext.PolicyWrapKey = PolicyKey; base.SetPolicyContext(executionContext, out _, out _); } @@ -32,7 +33,8 @@ internal override void SetPolicyContext(Context executionContext, out string pri priorPolicyWrapKey = executionContext.PolicyWrapKey; priorPolicyKey = executionContext.PolicyKey; - if (executionContext.PolicyWrapKey == null) executionContext.PolicyWrapKey = PolicyKey; + if (executionContext.PolicyWrapKey == null) + executionContext.PolicyWrapKey = PolicyKey; base.SetPolicyContext(executionContext, out _, out _); } diff --git a/src/Polly/Wrap/PolicyWrap.cs b/src/Polly/Wrap/PolicyWrap.cs index 9e47721192b..bce43769c03 100644 --- a/src/Polly/Wrap/PolicyWrap.cs +++ b/src/Polly/Wrap/PolicyWrap.cs @@ -61,12 +61,12 @@ public partial class PolicyWrap : Policy, IPolicyWrap /// /// Returns the outer in this /// - public IsPolicy Outer => (IsPolicy) _outerGeneric ?? _outerNonGeneric; + public IsPolicy Outer => (IsPolicy)_outerGeneric ?? _outerNonGeneric; /// /// Returns the next inner in this /// - public IsPolicy Inner => (IsPolicy) _innerGeneric ?? _innerNonGeneric; + public IsPolicy Inner => (IsPolicy)_innerGeneric ?? _innerNonGeneric; internal PolicyWrap(Policy outer, ISyncPolicy inner) : base(outer.ExceptionPredicates, ResultPredicates.None) diff --git a/src/Polly/Wrap/PolicyWrapSyntax.cs b/src/Polly/Wrap/PolicyWrapSyntax.cs index a163b8081d7..a55c9d6c6d5 100644 --- a/src/Polly/Wrap/PolicyWrapSyntax.cs +++ b/src/Polly/Wrap/PolicyWrapSyntax.cs @@ -13,7 +13,8 @@ public static class ISyncPolicyPolicyWrapExtensions /// A instance representing the combined wrap. public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((Policy)outerPolicy).Wrap(innerPolicy); } @@ -25,7 +26,8 @@ public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPol /// A instance representing the combined wrap. public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((Policy)outerPolicy).Wrap(innerPolicy); } @@ -37,7 +39,8 @@ public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, IS /// A instance representing the combined wrap. public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((Policy)outerPolicy).Wrap(innerPolicy); } @@ -49,7 +52,8 @@ public static PolicyWrap Wrap(this ISyncPolicy outerP /// A instance representing the combined wrap. public static PolicyWrap Wrap(this ISyncPolicy outerPolicy, ISyncPolicy innerPolicy) { - if (outerPolicy == null) throw new ArgumentNullException(nameof(outerPolicy)); + if (outerPolicy == null) + throw new ArgumentNullException(nameof(outerPolicy)); return ((Policy)outerPolicy).Wrap(innerPolicy); } } @@ -63,7 +67,8 @@ public partial class Policy /// PolicyWrap.PolicyWrap. public PolicyWrap Wrap(ISyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new PolicyWrap( this, @@ -78,7 +83,8 @@ public PolicyWrap Wrap(ISyncPolicy innerPolicy) /// PolicyWrap.PolicyWrap. public PolicyWrap Wrap(ISyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new PolicyWrap( this, @@ -95,7 +101,8 @@ public partial class Policy /// PolicyWrap.PolicyWrap. public PolicyWrap Wrap(ISyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new PolicyWrap( this, @@ -109,7 +116,8 @@ public PolicyWrap Wrap(ISyncPolicy innerPolicy) /// PolicyWrap.PolicyWrap. public PolicyWrap Wrap(ISyncPolicy innerPolicy) { - if (innerPolicy == null) throw new ArgumentNullException(nameof(innerPolicy)); + if (innerPolicy == null) + throw new ArgumentNullException(nameof(innerPolicy)); return new PolicyWrap( this, diff --git a/test/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs b/test/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs index 67682fb64c7..12bbd58788c 100644 --- a/test/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs +++ b/test/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs @@ -6,7 +6,17 @@ public class BulkheadAsyncSpecs(ITestOutputHelper testOutputHelper) : BulkheadSp #region Configuration [Fact] - public void Should_throw_when_maxparallelization_less_or_equal_to_zero() + public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions() + { + Action policy = () => Policy + .BulkheadAsync(0); + + policy.Should().Throw().And + .ParamName.Should().Be("maxParallelization"); + } + + [Fact] + public void Should_throw_when_maxParallelization_less_or_equal_to_zero() { Action policy = () => Policy .BulkheadAsync(0, 1); diff --git a/test/Polly.Specs/Bulkhead/BulkheadSpecs.cs b/test/Polly.Specs/Bulkhead/BulkheadSpecs.cs index 44b3912c828..cfbf37094bd 100644 --- a/test/Polly.Specs/Bulkhead/BulkheadSpecs.cs +++ b/test/Polly.Specs/Bulkhead/BulkheadSpecs.cs @@ -11,7 +11,17 @@ public BulkheadSpecs(ITestOutputHelper testOutputHelper) #region Configuration [Fact] - public void Should_throw_when_maxparallelization_less_or_equal_to_zero() + public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions() + { + Action policy = () => Policy + .Bulkhead(0); + + policy.Should().Throw().And + .ParamName.Should().Be("maxParallelization"); + } + + [Fact] + public void Should_throw_when_maxParallelization_less_or_equal_to_zero() { Action policy = () => Policy .Bulkhead(0, 1); @@ -21,7 +31,7 @@ public void Should_throw_when_maxparallelization_less_or_equal_to_zero() } [Fact] - public void Should_throw_when_maxqueuedactions_less_than_zero() + public void Should_throw_when_maxQueuedActions_less_than_zero() { Action policy = () => Policy .Bulkhead(1, -1); diff --git a/test/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs b/test/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs index 6ab0eda9f39..fe1fc1aa5f4 100644 --- a/test/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs @@ -11,7 +11,17 @@ public BulkheadTResultAsyncSpecs(ITestOutputHelper testOutputHelper) #region Configuration [Fact] - public void Should_throw_when_maxparallelization_less_or_equal_to_zero() + public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions() + { + Action policy = () => Policy + .BulkheadAsync(0); + + policy.Should().Throw().And + .ParamName.Should().Be("maxParallelization"); + } + + [Fact] + public void Should_throw_when_maxParallelization_less_or_equal_to_zero() { Action policy = () => Policy .BulkheadAsync(0, 1); diff --git a/test/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs b/test/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs index 129e2981628..e31972184d3 100644 --- a/test/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs +++ b/test/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs @@ -11,7 +11,17 @@ public BulkheadTResultSpecs(ITestOutputHelper testOutputHelper) #region Configuration [Fact] - public void Should_throw_when_maxparallelization_less_or_equal_to_zero() + public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions() + { + Action policy = () => Policy + .Bulkhead(0); + + policy.Should().Throw().And + .ParamName.Should().Be("maxParallelization"); + } + + [Fact] + public void Should_throw_when_maxParallelization_less_or_equal_to_zero() { Action policy = () => Policy .Bulkhead(0, 1); diff --git a/test/Polly.Specs/Caching/ContextualTtlSpecs.cs b/test/Polly.Specs/Caching/ContextualTtlSpecs.cs index 7056a4e4ed9..411d79fbae7 100644 --- a/test/Polly.Specs/Caching/ContextualTtlSpecs.cs +++ b/test/Polly.Specs/Caching/ContextualTtlSpecs.cs @@ -47,4 +47,64 @@ public void Should_return_negative_value_set_on_context() gotTtl.Timespan.Should().Be(ttl); gotTtl.SlidingExpiration.Should().BeFalse(); } + + [Fact] + public void Should_return_zero_if_non_timespan_value_set_on_context() + { + var contextData = new Dictionary + { + [ContextualTtl.TimeSpanKey] = "non-timespan value" + }; + + Context context = new Context(string.Empty, contextData); + new ContextualTtl().GetTtl(context, null).Timespan.Should().Be(TimeSpan.Zero); + } + + [Fact] + public void Should_return_sliding_expiration_if_set_on_context() + { + var ttl = TimeSpan.FromSeconds(30); + var contextData = new Dictionary + { + [ContextualTtl.TimeSpanKey] = ttl, + [ContextualTtl.SlidingExpirationKey] = true + }; + + var context = new Context(string.Empty, contextData); + var gotTtl = new ContextualTtl().GetTtl(context, null); + gotTtl.Timespan.Should().Be(ttl); + gotTtl.SlidingExpiration.Should().BeTrue(); + } + + [Fact] + public void Should_return_no_sliding_expiration_if_set_to_false_on_context() + { + var ttl = TimeSpan.FromSeconds(30); + var contextData = new Dictionary + { + [ContextualTtl.TimeSpanKey] = ttl, + [ContextualTtl.SlidingExpirationKey] = false + }; + + var context = new Context(string.Empty, contextData); + var gotTtl = new ContextualTtl().GetTtl(context, null); + gotTtl.Timespan.Should().Be(ttl); + gotTtl.SlidingExpiration.Should().BeFalse(); + } + + [Fact] + public void Should_return_no_sliding_expiration_if_non_boolean_value_set_on_context() + { + var ttl = TimeSpan.FromSeconds(30); + var contextData = new Dictionary + { + [ContextualTtl.TimeSpanKey] = ttl, + [ContextualTtl.SlidingExpirationKey] = "non-boolean value" + }; + + var context = new Context(string.Empty, contextData); + var gotTtl = new ContextualTtl().GetTtl(context, null); + gotTtl.Timespan.Should().Be(ttl); + gotTtl.SlidingExpiration.Should().BeFalse(); + } } diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs index 5748a3bc6a8..eabd3a0b2b3 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs @@ -2484,6 +2484,160 @@ public void Should_open_circuit_with_timespan_maxvalue_if_manual_override_open() passedBreakTimespan.Should().Be(TimeSpan.MaxValue); } + [Fact] + public void Should_throw_when_failureThreshold_is_less_or_equals_than_zero() + { + Action onBreak = (_, timespan, _) => { _ = timespan; }; + Action onReset = _ => { }; + + var time = 1.January(2000); + SystemClock.UtcNow = () => time; + + Action action = () => Policy + .Handle() + .AdvancedCircuitBreakerAsync( + failureThreshold: 0, + samplingDuration: TimeSpan.FromSeconds(10), + minimumThroughput: 4, + durationOfBreak: TimeSpan.FromMinutes(1), + onBreak: onBreak, + onReset: onReset); + + action.Should().Throw().And.ParamName.Should().Be("failureThreshold"); + } + + [Fact] + public void Should_throw_when_failureThreshold_is_more_than_one() + { + Action onBreak = (_, timespan, _) => { _ = timespan; }; + Action onReset = _ => { }; + + var time = 1.January(2000); + SystemClock.UtcNow = () => time; + + Action action = () => Policy + .Handle() + .AdvancedCircuitBreakerAsync( + failureThreshold: 1.5, + samplingDuration: TimeSpan.FromSeconds(10), + minimumThroughput: 4, + durationOfBreak: TimeSpan.FromMinutes(1), + onBreak: onBreak, + onReset: onReset); + + action.Should().Throw().And.ParamName.Should().Be("failureThreshold"); + } + + [Fact] + public void Should_throw_when_samplingDuration_is_less_than_resolutionOfCircuit() + { + Action onBreak = (_, timespan, _) => { _ = timespan; }; + Action onReset = _ => { }; + + var time = 1.January(2000); + SystemClock.UtcNow = () => time; + + Action action = () => Policy + .Handle() + .AdvancedCircuitBreakerAsync( + failureThreshold: 0.5, + samplingDuration: TimeSpan.FromMilliseconds(10), + minimumThroughput: 4, + durationOfBreak: TimeSpan.FromMinutes(1), + onBreak: onBreak, + onReset: onReset); + + action.Should().Throw().And.ParamName.Should().Be("samplingDuration"); + } + + [Fact] + public void Should_throw_when_minimumThroughput_is_less_or_equals_than_one() + { + Action onBreak = (_, timespan, _) => { _ = timespan; }; + Action onReset = _ => { }; + + var time = 1.January(2000); + SystemClock.UtcNow = () => time; + + Action action = () => Policy + .Handle() + .AdvancedCircuitBreakerAsync( + failureThreshold: 0.5, + samplingDuration: TimeSpan.FromSeconds(10), + minimumThroughput: 0, + durationOfBreak: TimeSpan.FromMinutes(1), + onBreak: onBreak, + onReset: onReset); + + action.Should().Throw().And.ParamName.Should().Be("minimumThroughput"); + } + + [Fact] + public void Should_throw_when_durationOfBreak_is_negative_timespan() + { + Action onBreak = (_, timespan, _) => { _ = timespan; }; + Action onReset = _ => { }; + + var time = 1.January(2000); + SystemClock.UtcNow = () => time; + + Action action = () => Policy + .Handle() + .AdvancedCircuitBreakerAsync( + failureThreshold: 0.5, + samplingDuration: TimeSpan.FromSeconds(10), + minimumThroughput: 4, + durationOfBreak: TimeSpan.FromMinutes(-1), + onBreak: onBreak, + onReset: onReset); + + action.Should().Throw().And.ParamName.Should().Be("durationOfBreak"); + } + + [Fact] + public void Should_throw_when_onReset_is_null() + { + Action onBreak = (_, timespan, _) => { _ = timespan; }; + + var time = 1.January(2000); + SystemClock.UtcNow = () => time; + + Action action = () => Policy + .Handle() + .AdvancedCircuitBreakerAsync( + failureThreshold: 0.5, + samplingDuration: TimeSpan.FromSeconds(10), + minimumThroughput: 4, + durationOfBreak: TimeSpan.FromMinutes(1), + onBreak: onBreak, + onReset: null); + + action.Should().Throw().And.ParamName.Should().Be("onReset"); + } + + [Fact] + public void Should_throw_when_onHalfOpen_is_null() + { + Action onBreak = (_, timespan, _) => { _ = timespan; }; + Action onReset = _ => { }; + + var time = 1.January(2000); + SystemClock.UtcNow = () => time; + + Action action = () => Policy + .Handle() + .AdvancedCircuitBreakerAsync( + failureThreshold: 0.5, + samplingDuration: TimeSpan.FromSeconds(10), + minimumThroughput: 4, + durationOfBreak: TimeSpan.FromMinutes(1), + onBreak: onBreak, + onReset: onReset, + onHalfOpen: null); + + action.Should().Throw().And.ParamName.Should().Be("onHalfOpen"); + } + #endregion #region Tests that supplied context is passed to stage-change delegates diff --git a/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs b/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs index 7e10400b443..7948d057fdd 100644 --- a/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs +++ b/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs @@ -43,6 +43,18 @@ public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_ configure.Should().Throw().And.ParamName.Should().Be("policyKey"); } + [Fact] + public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once_via_interface() + { + IAsyncPolicy policyAsInterface = Policy.Handle().RetryAsync(); + + Action configure = () => policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); + + configure.Should().NotThrow(); + + configure.Should().Throw().And.ParamName.Should().Be("policyKey"); + } + [Fact] public void PolicyKey_property_should_be_non_null_or_empty_if_not_explicitly_configured() { @@ -227,6 +239,17 @@ public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_ configure.Should().Throw().And.ParamName.Should().Be("policyKey"); } + [Fact] + public void Should_not_be_able_to_configure_the_policy_key_explicitly_more_than_once_via_interface() + { + IAsyncPolicy policyAsInterface = Policy.HandleResult(0).RetryAsync(); + Action configure = () => policyAsInterface.WithPolicyKey(Guid.NewGuid().ToString()); + + configure.Should().NotThrow(); + + configure.Should().Throw().And.ParamName.Should().Be("policyKey"); + } + [Fact] public void PolicyKey_property_should_be_non_null_or_empty_if_not_explicitly_configured() { diff --git a/test/Polly.Specs/Retry/RetryAsyncSpecs.cs b/test/Polly.Specs/Retry/RetryAsyncSpecs.cs index 589f3f7bd6f..486c6a23700 100644 --- a/test/Polly.Specs/Retry/RetryAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/RetryAsyncSpecs.cs @@ -155,6 +155,26 @@ await policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrowAsync(); } + [Fact] + public void Should_throw_when_onRetry_is_null() + { + Action action = () => Policy + .Handle() + .RetryAsync(3, (Action)null!); + + action.Should().Throw().And.ParamName.Should().Be("onRetry"); + } + + [Fact] + public void Should_throw_when_onRetryAsync_is_null() + { + Action action = () => Policy + .Handle() + .RetryAsync(3, (Func)null!); + + action.Should().Throw().And.ParamName.Should().Be("onRetryAsync"); + } + [Fact] public async Task Should_call_onretry_on_each_retry_with_the_current_retry_count() { diff --git a/test/Polly.Specs/Retry/WaitAndRetryTResultAsyncSpecs.cs b/test/Polly.Specs/Retry/WaitAndRetryTResultAsyncSpecs.cs index eadcb2fb4b9..e0d09d6e821 100644 --- a/test/Polly.Specs/Retry/WaitAndRetryTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetryTResultAsyncSpecs.cs @@ -42,6 +42,48 @@ await policy.ExecuteAsync(async () => actualRetryWaits.Should().ContainInOrder(expectedRetryWaits.Values); } + [Fact] + public void Should_throw_when_retry_count_is_less_than_zero() + { + var expectedRetryWaits = new Dictionary + { + {ResultPrimitive.Fault, 2.Seconds()}, + {ResultPrimitive.FaultAgain, 4.Seconds()}, + }; + + var actualRetryWaits = new List(); + + Action configure = () => Policy + .HandleResult(ResultPrimitive.Fault) + .WaitAndRetryAsync(-1, + (_, outcome, _) => expectedRetryWaits[outcome.Result], + (_, timeSpan, _, _) => + { + actualRetryWaits.Add(timeSpan); + return TaskHelper.EmptyTask; + }); + + configure.Should().Throw().And.ParamName.Should().Be("retryCount"); + } + + [Fact] + public void Should_throw_when_onRetryAsync_is_null() + { + var expectedRetryWaits = new Dictionary + { + {ResultPrimitive.Fault, 2.Seconds()}, + {ResultPrimitive.FaultAgain, 4.Seconds()}, + }; + + Action configure = () => Policy + .HandleResult(ResultPrimitive.Fault) + .WaitAndRetryAsync(2, + (_, outcome, _) => expectedRetryWaits[outcome.Result], + null); + + configure.Should().Throw().And.ParamName.Should().Be("onRetryAsync"); + } + public void Dispose() => SystemClock.Reset(); } diff --git a/test/Polly.Specs/Utilities/SystemClockSpecs.cs b/test/Polly.Specs/Utilities/SystemClockSpecs.cs new file mode 100644 index 00000000000..c6911dfe6f4 --- /dev/null +++ b/test/Polly.Specs/Utilities/SystemClockSpecs.cs @@ -0,0 +1,38 @@ +namespace Polly.Specs.Utilities; + +public class SystemClockSpecs +{ + [Fact] + public void Sleep_ShouldNotThrow_WhenCancellationNotRequested() => + SystemClock.Sleep.Invoking(s => + { + using var cts = new CancellationTokenSource(); + s(TimeSpan.FromMilliseconds(1), cts.Token); + }).Should().NotThrow(); + + [Fact] + public void Sleep_ShouldThrow_WhenCancellationRequested() => + SystemClock.Sleep.Invoking(s => + { + using var cts = new CancellationTokenSource(); + cts.Cancel(); + s(TimeSpan.FromMilliseconds(1), cts.Token); + }).Should().Throw(); + + [Fact] + public async Task SleepAsync_ShouldNotThrow_WhenCancellationNotRequested() => + await SystemClock.SleepAsync.Invoking(async s => + { + using var cts = new CancellationTokenSource(); + await s(TimeSpan.FromMilliseconds(1), cts.Token); + }).Should().NotThrowAsync(); + + [Fact] + public async Task SleepAsync_ShouldThrow_WhenCancellationRequested() => + await SystemClock.SleepAsync.Invoking(async s => + { + using var cts = new CancellationTokenSource(); + cts.Cancel(); + await s(TimeSpan.FromMilliseconds(1), cts.Token); + }).Should().ThrowAsync(); +}