diff --git a/eng/analyzers/Stylecop.json b/eng/analyzers/Stylecop.json index 9cad70f365d..d1034436502 100644 --- a/eng/analyzers/Stylecop.json +++ b/eng/analyzers/Stylecop.json @@ -19,9 +19,8 @@ "maintainabilityRules": { }, "namingRules": { - "tupleElementNameCasing": "camelCase" }, "readabilityRules": { } } -} \ No newline at end of file +} diff --git a/src/Polly.Core/Registry/RegistryPipelineComponentBuilder.cs b/src/Polly.Core/Registry/RegistryPipelineComponentBuilder.cs index 10b7f937f8a..fb826066e7d 100644 --- a/src/Polly.Core/Registry/RegistryPipelineComponentBuilder.cs +++ b/src/Polly.Core/Registry/RegistryPipelineComponentBuilder.cs @@ -30,7 +30,7 @@ public RegistryPipelineComponentBuilder( _configure = configure; } - internal (ResilienceContextPool? contextPool, PipelineComponent component) CreateComponent() + internal (ResilienceContextPool? ContextPool, PipelineComponent Component) CreateComponent() { var builder = CreateBuilder(); var component = builder.ComponentFactory(); diff --git a/src/Polly/RateLimit/IRateLimiter.cs b/src/Polly/RateLimit/IRateLimiter.cs index 67ab7e1dda0..a6c702e68f9 100644 --- a/src/Polly/RateLimit/IRateLimiter.cs +++ b/src/Polly/RateLimit/IRateLimiter.cs @@ -11,5 +11,5 @@ internal interface IRateLimiter /// Returns whether the execution is permitted; if not, returns what should be waited before retrying. /// Calling this method consumes an execution permit if one is available: a caller receiving a return value true should make an execution. /// - (bool permitExecution, TimeSpan retryAfter) PermitExecution(); + (bool PermitExecution, TimeSpan RetryAfter) PermitExecution(); } diff --git a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs index 5333dd90b1e..7a5ef950006 100644 --- a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs +++ b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs @@ -40,7 +40,7 @@ public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity) /// /// Returns whether the execution is permitted; if not, returns what should be waited before retrying. /// - public (bool permitExecution, TimeSpan retryAfter) PermitExecution() + public (bool PermitExecution, TimeSpan RetryAfter) PermitExecution() { while (true) { diff --git a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs index 0436500b9bc..c8d352084ec 100644 --- a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs @@ -38,13 +38,13 @@ public void Should_throw_when_cache_key_strategy_is_null() [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -52,9 +52,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -62,34 +62,34 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return ValueToReturn; }, new Context(OperationKey))).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); } [Fact] public async Task Should_execute_delegate_and_put_value_in_cache_but_when_it_expires_execute_delegate_again() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); TimeSpan ttl = TimeSpan.FromMinutes(30); var cache = Policy.CacheAsync(stubCacheProvider, ttl); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); @@ -98,49 +98,49 @@ public async Task Should_execute_delegate_and_put_value_in_cache_but_when_it_exp { delegateInvocations++; await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; DateTimeOffset fixedTime = SystemClock.DateTimeOffsetUtcNow(); SystemClock.DateTimeOffsetUtcNow = () => fixedTime; // First execution should execute delegate and put result in the cache. - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); // Second execution (before cache expires) should get it from the cache - no further delegate execution. // (Manipulate time so just prior cache expiry). SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(-1); - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); // Manipulate time to force cache expiry. SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(1); // Third execution (cache expired) should not get it from the cache - should cause further delegate execution. - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(2); } [Fact] public async Task Should_execute_delegate_but_not_put_value_in_cache_if_cache_does_not_hold_value_but_ttl_indicates_not_worth_caching() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.Zero); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return ValueToReturn; }, new Context(OperationKey))).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); } @@ -148,8 +148,8 @@ public async Task Should_execute_delegate_but_not_put_value_in_cache_if_cache_do [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; var cache = Policy.CacheAsync(new StubCacheProvider(), TimeSpan.MaxValue); @@ -158,16 +158,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_pri { delegateInvocations++; await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); } @@ -225,18 +225,18 @@ public async Task Should_allow_custom_ICacheKeyStrategy() public async Task Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_reference_type() { ResultClass? valueToReturn = null; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(OperationKey))).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -246,11 +246,11 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { ResultClass? valueToReturnFromCache = null; ResultClass valueToReturnFromExecution = new ResultClass(ResultPrimitive.Good); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -259,7 +259,7 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac delegateExecuted = true; await TaskHelper.EmptyTask; return valueToReturnFromExecution; - }, new Context(operationKey))) + }, new Context(OperationKey))) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -269,18 +269,18 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac public async Task Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_value_type() { ResultPrimitive valueToReturn = default; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(OperationKey))).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -291,11 +291,11 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac ResultPrimitive valueToReturnFromCache = default; ResultPrimitive valueToReturnFromExecution = ResultPrimitive.Good; valueToReturnFromExecution.Should().NotBe(valueToReturnFromCache); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -304,7 +304,7 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac delegateExecuted = true; await TaskHelper.EmptyTask; return valueToReturnFromExecution; - }, new Context(operationKey))) + }, new Context(OperationKey))) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -317,16 +317,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_outermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); var noop = Policy.NoOpAsync(); var wrap = Policy.WrapAsync(cache, noop); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -334,9 +334,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -344,16 +344,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_innermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); var noop = Policy.NoOpAsync(); var wrap = Policy.WrapAsync(noop, cache); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -361,9 +361,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -371,16 +371,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_mid_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); var noop = Policy.NoOpAsync(); var wrap = Policy.WrapAsync(noop, cache, noop); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -388,9 +388,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -445,8 +445,8 @@ public void Should_always_execute_delegate_if_execution_is_void_returning() [Fact] public async Task Should_honour_cancellation_even_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; var cache = Policy.CacheAsync(new StubCacheProvider(), TimeSpan.MaxValue); @@ -458,15 +458,15 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached( // delegate does not observe cancellation token; test is whether CacheEngine does. delegateInvocations++; await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; - (await cache.ExecuteAsync(func, new Context(operationKey), tokenSource.Token)).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); tokenSource.Cancel(); - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(operationKey), tokenSource.Token)) + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) .Should().ThrowAsync(); delegateInvocations.Should().Be(1); } @@ -474,8 +474,8 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached( [Fact] public async Task Should_honour_cancellation_during_delegate_execution_and_not_put_to_cache() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); @@ -487,13 +487,13 @@ public async Task Should_honour_cancellation_during_delegate_execution_and_not_p tokenSource.Cancel(); // simulate cancellation raised during delegate execution ct.ThrowIfCancellationRequested(); await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(operationKey), tokenSource.Token)) + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) .Should().ThrowAsync(); - (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); } @@ -510,15 +510,15 @@ public async Task Should_call_onError_delegate_if_cache_get_errors() Exception? exceptionFromCacheProvider = null; - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; Action onError = (_, _, exc) => { exceptionFromCacheProvider = exc; }; var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue, onError); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -527,10 +527,10 @@ public async Task Should_call_onError_delegate_if_cache_get_errors() { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; + return ValueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromExecution); + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromExecution); delegateExecuted.Should().BeTrue(); // And error should be captured by onError delegate. @@ -545,24 +545,24 @@ public async Task Should_call_onError_delegate_if_cache_put_errors() Exception? exceptionFromCacheProvider = null; - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; Action onError = (_, _, exc) => { exceptionFromCacheProvider = exc; }; var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue, onError); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return ValueToReturn; }, new Context(OperationKey))).Should().Be(ValueToReturn); // error should be captured by onError delegate. exceptionFromCacheProvider.Should().Be(ex); // failed to put it in the cache - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); } @@ -570,13 +570,13 @@ public async Task Should_call_onError_delegate_if_cache_put_errors() [Fact] public async Task Should_execute_oncacheget_after_got_from_cache() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; string? keyPassedToDelegate = null; - Context contextToExecute = new Context(operationKey); + Context contextToExecute = new Context(OperationKey); Context? contextPassedToDelegate = null; Action noErrorHandling = (_, _, _) => { }; @@ -585,32 +585,32 @@ public async Task Should_execute_oncacheget_after_got_from_cache() IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, new RelativeTtl(TimeSpan.MaxValue), DefaultCacheKeyStrategy.Instance, onCacheAction, emptyDelegate, emptyDelegate, noErrorHandling, noErrorHandling); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; (await cache.ExecuteAsync(async _ => { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; + return ValueToReturnFromExecution; }, contextToExecute)) - .Should().Be(valueToReturnFromCache); + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); contextPassedToDelegate.Should().BeSameAs(contextToExecute); - keyPassedToDelegate.Should().Be(operationKey); + keyPassedToDelegate.Should().Be(OperationKey); } [Fact] public async Task Should_execute_oncachemiss_and_oncacheput_if_cache_does_not_hold_value_and_put() { - const string valueToReturn = "valueToReturn"; + const string ValueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; string? keyPassedToOnCacheMiss = null; string? keyPassedToOnCachePut = null; - Context contextToExecute = new Context(operationKey); + Context contextToExecute = new Context(OperationKey); Context? contextPassedToOnCacheMiss = null; Context? contextPassedToOnCachePut = null; @@ -622,18 +622,18 @@ public async Task Should_execute_oncachemiss_and_oncacheput_if_cache_does_not_ho IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, new RelativeTtl(TimeSpan.MaxValue), DefaultCacheKeyStrategy.Instance, emptyDelegate, onCacheMiss, onCachePut, noErrorHandling, noErrorHandling); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, contextToExecute)).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return ValueToReturn; }, contextToExecute)).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); contextPassedToOnCachePut.Should().BeSameAs(contextToExecute); - keyPassedToOnCachePut.Should().Be(operationKey); + keyPassedToOnCachePut.Should().Be(OperationKey); contextPassedToOnCacheMiss.Should().NotBeNull(); keyPassedToOnCacheMiss.Should().Be("SomeOperationKey"); } @@ -641,13 +641,13 @@ public async Task Should_execute_oncachemiss_and_oncacheput_if_cache_does_not_ho [Fact] public async Task Should_execute_oncachemiss_but_not_oncacheput_if_cache_does_not_hold_value_and_returned_value_not_worth_caching() { - const string valueToReturn = "valueToReturn"; + const string ValueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; string? keyPassedToOnCacheMiss = null; string? keyPassedToOnCachePut = null; - Context contextToExecute = new Context(operationKey); + Context contextToExecute = new Context(OperationKey); Context? contextPassedToOnCacheMiss = null; Context? contextPassedToOnCachePut = null; @@ -659,11 +659,11 @@ public async Task Should_execute_oncachemiss_but_not_oncacheput_if_cache_does_no IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, new RelativeTtl(TimeSpan.Zero), DefaultCacheKeyStrategy.Instance, emptyDelegate, onCacheMiss, onCachePut, noErrorHandling, noErrorHandling); - (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, contextToExecute)).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return ValueToReturn; }, contextToExecute)).Should().Be(ValueToReturn); contextPassedToOnCachePut.Should().BeNull(); keyPassedToOnCachePut.Should().BeNull(); diff --git a/test/Polly.Specs/Caching/CacheSpecs.cs b/test/Polly.Specs/Caching/CacheSpecs.cs index be5b7ecc911..cae6c4e5d9b 100644 --- a/test/Polly.Specs/Caching/CacheSpecs.cs +++ b/test/Polly.Specs/Caching/CacheSpecs.cs @@ -38,22 +38,22 @@ public void Should_throw_when_cache_key_strategy_is_null() [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; cache.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -61,34 +61,34 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, new Context(OperationKey)).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); } [Fact] public void Should_execute_delegate_and_put_value_in_cache_but_when_it_expires_execute_delegate_again() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); TimeSpan ttl = TimeSpan.FromMinutes(30); CachePolicy cache = Policy.Cache(stubCacheProvider, ttl); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); @@ -96,50 +96,50 @@ public void Should_execute_delegate_and_put_value_in_cache_but_when_it_expires_e Func func = _ => { delegateInvocations++; - return valueToReturn; + return ValueToReturn; }; DateTimeOffset fixedTime = SystemClock.DateTimeOffsetUtcNow(); SystemClock.DateTimeOffsetUtcNow = () => fixedTime; // First execution should execute delegate and put result in the cache. - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); // Second execution (before cache expires) should get it from the cache - no further delegate execution. // (Manipulate time so just prior cache expiry). SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(-1); - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); // Manipulate time to force cache expiry. SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(1); // Third execution (cache expired) should not get it from the cache - should cause further delegate execution. - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(2); } [Fact] public void Should_execute_delegate_but_not_put_value_in_cache_if_cache_does_not_hold_value_but_ttl_indicates_not_worth_caching() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.Zero); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, new Context(OperationKey)).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); } @@ -147,8 +147,8 @@ public void Should_execute_delegate_but_not_put_value_in_cache_if_cache_does_not [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; CachePolicy cache = Policy.Cache(new StubCacheProvider(), TimeSpan.MaxValue); @@ -156,16 +156,16 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_prior_exe Func func = _ => { delegateInvocations++; - return valueToReturn; + return ValueToReturn; }; - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); } @@ -223,18 +223,18 @@ public void Should_allow_custom_ICacheKeyStrategy() public void Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_reference_type() { ResultClass? valueToReturn = null; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => valueToReturn, new Context(OperationKey)).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -244,11 +244,11 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol { ResultClass? valueToReturnFromCache = null; ResultClass valueToReturnFromExecution = new ResultClass(ResultPrimitive.Good); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; @@ -256,7 +256,7 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol { delegateExecuted = true; return valueToReturnFromExecution; - }, new Context(operationKey)) + }, new Context(OperationKey)) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -266,18 +266,18 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol public void Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_value_type() { ResultPrimitive valueToReturn = default; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => valueToReturn, new Context(OperationKey)).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -288,11 +288,11 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol ResultPrimitive valueToReturnFromCache = default; ResultPrimitive valueToReturnFromExecution = ResultPrimitive.Good; valueToReturnFromExecution.Should().NotBe(valueToReturnFromCache); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; @@ -300,7 +300,7 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol { delegateExecuted = true; return valueToReturnFromExecution; - }, new Context(operationKey)) + }, new Context(OperationKey)) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -313,25 +313,25 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_outermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); Policy noop = Policy.NoOp(); PolicyWrap wrap = Policy.Wrap(cache, noop); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; wrap.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -339,25 +339,25 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_innermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); Policy noop = Policy.NoOp(); PolicyWrap wrap = Policy.Wrap(noop, cache); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; wrap.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -365,25 +365,25 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_mid_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); Policy noop = Policy.NoOp(); PolicyWrap wrap = Policy.Wrap(noop, cache, noop); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; wrap.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -437,8 +437,8 @@ public void Should_always_execute_delegate_if_execution_is_void_returning() [Fact] public void Should_honour_cancellation_even_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; CachePolicy cache = Policy.Cache(new StubCacheProvider(), TimeSpan.MaxValue); @@ -449,15 +449,15 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached() { // delegate does not observe cancellation token; test is whether CacheEngine does. delegateInvocations++; - return valueToReturn; + return ValueToReturn; }; - cache.Execute(func, new Context(operationKey), tokenSource.Token).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); tokenSource.Cancel(); - cache.Invoking(policy => policy.Execute(func, new Context(operationKey), tokenSource.Token)) + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) .Should().Throw(); delegateInvocations.Should().Be(1); } @@ -465,8 +465,8 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached() [Fact] public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_cache() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); @@ -477,13 +477,13 @@ public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_ { tokenSource.Cancel(); // simulate cancellation raised during delegate execution ct.ThrowIfCancellationRequested(); - return valueToReturn; + return ValueToReturn; }; - cache.Invoking(policy => policy.Execute(func, new Context(operationKey), tokenSource.Token)) + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) .Should().Throw(); - (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); } @@ -500,15 +500,15 @@ public void Should_call_onError_delegate_if_cache_get_errors() Exception? exceptionFromCacheProvider = null; - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; Action onError = (_, _, exc) => { exceptionFromCacheProvider = exc; }; CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue, onError); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; @@ -516,9 +516,9 @@ public void Should_call_onError_delegate_if_cache_get_errors() cache.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromExecution); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromExecution); delegateExecuted.Should().BeTrue(); // And error should be captured by onError delegate. @@ -533,24 +533,24 @@ public void Should_call_onError_delegate_if_cache_put_errors() Exception? exceptionFromCacheProvider = null; - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; Action onError = (_, _, exc) => { exceptionFromCacheProvider = exc; }; CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue, onError); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, new Context(OperationKey)).Should().Be(ValueToReturn); // error should be captured by onError delegate. exceptionFromCacheProvider.Should().Be(ex); // failed to put it in the cache - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); @@ -559,13 +559,13 @@ public void Should_call_onError_delegate_if_cache_put_errors() [Fact] public void Should_execute_oncacheget_after_got_from_cache() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; string? keyPassedToDelegate = null; - Context contextToExecute = new Context(operationKey); + Context contextToExecute = new Context(OperationKey); Context? contextPassedToDelegate = null; Action noErrorHandling = (_, _, _) => { }; @@ -574,31 +574,31 @@ public void Should_execute_oncacheget_after_got_from_cache() ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, new RelativeTtl(TimeSpan.MaxValue), DefaultCacheKeyStrategy.Instance, onCacheAction, emptyDelegate, emptyDelegate, noErrorHandling, noErrorHandling); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; cache.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; + return ValueToReturnFromExecution; }, contextToExecute) - .Should().Be(valueToReturnFromCache); + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); contextPassedToDelegate.Should().BeSameAs(contextToExecute); - keyPassedToDelegate.Should().Be(operationKey); + keyPassedToDelegate.Should().Be(OperationKey); } [Fact] public void Should_execute_oncachemiss_and_oncacheput_if_cache_does_not_hold_value_and_put() { - const string valueToReturn = "valueToReturn"; + const string ValueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; string? keyPassedToOnCacheMiss = null; string? keyPassedToOnCachePut = null; - Context contextToExecute = new Context(operationKey); + Context contextToExecute = new Context(OperationKey); Context? contextPassedToOnCacheMiss = null; Context? contextPassedToOnCachePut = null; @@ -610,33 +610,33 @@ public void Should_execute_oncachemiss_and_oncacheput_if_cache_does_not_hold_val ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, new RelativeTtl(TimeSpan.MaxValue), DefaultCacheKeyStrategy.Instance, emptyDelegate, onCacheMiss, onCachePut, noErrorHandling, noErrorHandling); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, contextToExecute).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, contextToExecute).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); contextPassedToOnCacheMiss.Should().BeSameAs(contextToExecute); - keyPassedToOnCacheMiss.Should().Be(operationKey); + keyPassedToOnCacheMiss.Should().Be(OperationKey); contextPassedToOnCachePut.Should().BeSameAs(contextToExecute); - keyPassedToOnCachePut.Should().Be(operationKey); + keyPassedToOnCachePut.Should().Be(OperationKey); } [Fact] public void Should_execute_oncachemiss_but_not_oncacheput_if_cache_does_not_hold_value_and_returned_value_not_worth_caching() { - const string valueToReturn = "valueToReturn"; + const string ValueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; string? keyPassedToOnCacheMiss = null; string? keyPassedToOnCachePut = null; - Context contextToExecute = new Context(operationKey); + Context contextToExecute = new Context(OperationKey); Context? contextPassedToOnCacheMiss = null; Context? contextPassedToOnCachePut = null; @@ -648,14 +648,14 @@ public void Should_execute_oncachemiss_but_not_oncacheput_if_cache_does_not_hold ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, new RelativeTtl(TimeSpan.Zero), DefaultCacheKeyStrategy.Instance, emptyDelegate, onCacheMiss, onCachePut, noErrorHandling, noErrorHandling); - (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); - cache.Execute(_ => valueToReturn, contextToExecute).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, contextToExecute).Should().Be(ValueToReturn); contextPassedToOnCacheMiss.Should().BeSameAs(contextToExecute); - keyPassedToOnCacheMiss.Should().Be(operationKey); + keyPassedToOnCacheMiss.Should().Be(OperationKey); contextPassedToOnCachePut.Should().BeNull(); keyPassedToOnCachePut.Should().BeNull(); diff --git a/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs b/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs index 51d0b3514c6..ff4d038bc2b 100644 --- a/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs @@ -37,13 +37,13 @@ public void Should_throw_when_cache_key_strategy_is_null() [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -51,9 +51,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -61,34 +61,34 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return ValueToReturn; }, new Context(OperationKey))).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); } [Fact] public async Task Should_execute_delegate_and_put_value_in_cache_but_when_it_expires_execute_delegate_again() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); TimeSpan ttl = TimeSpan.FromMinutes(30); var cache = Policy.CacheAsync(stubCacheProvider, ttl); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); @@ -97,50 +97,50 @@ public async Task Should_execute_delegate_and_put_value_in_cache_but_when_it_exp { delegateInvocations++; await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; DateTimeOffset fixedTime = SystemClock.DateTimeOffsetUtcNow(); SystemClock.DateTimeOffsetUtcNow = () => fixedTime; // First execution should execute delegate and put result in the cache. - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); // Second execution (before cache expires) should get it from the cache - no further delegate execution. // (Manipulate time so just prior cache expiry). SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(-1); - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); // Manipulate time to force cache expiry. SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(1); // Third execution (cache expired) should not get it from the cache - should cause further delegate execution. - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(2); } [Fact] public async Task Should_execute_delegate_but_not_put_value_in_cache_if_cache_does_not_hold_value_but_ttl_indicates_not_worth_caching() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.Zero); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return ValueToReturn; }, new Context(OperationKey))).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); } @@ -148,8 +148,8 @@ public async Task Should_execute_delegate_but_not_put_value_in_cache_if_cache_do [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; var cache = Policy.CacheAsync(new StubCacheProvider(), TimeSpan.MaxValue); @@ -158,16 +158,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_pri { delegateInvocations++; await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (await cache.ExecuteAsync(func, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey))).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); } @@ -226,18 +226,18 @@ public async Task Should_allow_custom_ICacheKeyStrategy() public async Task Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_reference_type() { ResultClass? valueToReturn = null; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(OperationKey))).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -247,11 +247,11 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { ResultClass? valueToReturnFromCache = null; ResultClass valueToReturnFromExecution = new ResultClass(ResultPrimitive.Good); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -260,7 +260,7 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac delegateExecuted = true; await TaskHelper.EmptyTask; return valueToReturnFromExecution; - }, new Context(operationKey))) + }, new Context(OperationKey))) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -270,18 +270,18 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac public async Task Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_value_type() { ResultPrimitive valueToReturn = default; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(operationKey))).Should().Be(valueToReturn); + (await cache.ExecuteAsync(async _ => { await TaskHelper.EmptyTask; return valueToReturn; }, new Context(OperationKey))).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -292,11 +292,11 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac ResultPrimitive valueToReturnFromCache = default; ResultPrimitive valueToReturnFromExecution = ResultPrimitive.Good; valueToReturnFromExecution.Should().NotBe(valueToReturnFromCache); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -305,7 +305,7 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac delegateExecuted = true; await TaskHelper.EmptyTask; return valueToReturnFromExecution; - }, new Context(operationKey))) + }, new Context(OperationKey))) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -318,16 +318,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_outermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); var noop = Policy.NoOpAsync(); var wrap = cache.WrapAsync(noop); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -335,9 +335,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -345,16 +345,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_innermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); var noop = Policy.NoOpAsync(); var wrap = noop.WrapAsync(cache); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -362,9 +362,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -372,16 +372,16 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac [Fact] public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_mid_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); var noop = Policy.NoOpAsync(); var wrap = Policy.WrapAsync(noop, cache, noop); - await stubCacheProvider.PutAsync(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); + await stubCacheProvider.PutAsync(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false); bool delegateExecuted = false; @@ -389,9 +389,9 @@ public async Task Should_return_value_from_cache_and_not_execute_delegate_if_cac { delegateExecuted = true; await TaskHelper.EmptyTask; - return valueToReturnFromExecution; - }, new Context(operationKey))) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey))) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -429,8 +429,8 @@ public async Task Should_always_execute_delegate_if_execution_key_not_set() [Fact] public async Task Should_honour_cancellation_even_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; var cache = Policy.CacheAsync(new StubCacheProvider(), TimeSpan.MaxValue); @@ -442,15 +442,15 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached( // delegate does not observe cancellation token; test is whether CacheEngine does. delegateInvocations++; await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; - (await cache.ExecuteAsync(func, new Context(operationKey), tokenSource.Token)).Should().Be(valueToReturn); + (await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); tokenSource.Cancel(); - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(operationKey), tokenSource.Token)) + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) .Should().ThrowAsync(); delegateInvocations.Should().Be(1); } @@ -458,8 +458,8 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached( [Fact] public async Task Should_honour_cancellation_during_delegate_execution_and_not_put_to_cache() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); @@ -471,13 +471,13 @@ public async Task Should_honour_cancellation_during_delegate_execution_and_not_p tokenSource.Cancel(); // simulate cancellation raised during delegate execution ct.ThrowIfCancellationRequested(); await TaskHelper.EmptyTask; - return valueToReturn; + return ValueToReturn; }; - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(operationKey), tokenSource.Token)) + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) .Should().ThrowAsync(); - (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); } diff --git a/test/Polly.Specs/Caching/CacheTResultSpecs.cs b/test/Polly.Specs/Caching/CacheTResultSpecs.cs index 966446a92cb..9115491e1f3 100644 --- a/test/Polly.Specs/Caching/CacheTResultSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultSpecs.cs @@ -38,22 +38,22 @@ public void Should_throw_when_cache_key_strategy_is_null() [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; cache.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -61,34 +61,34 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, new Context(OperationKey)).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); } [Fact] public void Should_execute_delegate_and_put_value_in_cache_but_when_it_expires_execute_delegate_again() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); TimeSpan ttl = TimeSpan.FromMinutes(30); CachePolicy cache = Policy.Cache(stubCacheProvider, ttl); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); @@ -96,50 +96,50 @@ public void Should_execute_delegate_and_put_value_in_cache_but_when_it_expires_e Func func = _ => { delegateInvocations++; - return valueToReturn; + return ValueToReturn; }; DateTimeOffset fixedTime = SystemClock.DateTimeOffsetUtcNow(); SystemClock.DateTimeOffsetUtcNow = () => fixedTime; // First execution should execute delegate and put result in the cache. - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); // Second execution (before cache expires) should get it from the cache - no further delegate execution. // (Manipulate time so just prior cache expiry). SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(-1); - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); // Manipulate time to force cache expiry. SystemClock.DateTimeOffsetUtcNow = () => fixedTime.Add(ttl).AddSeconds(1); // Third execution (cache expired) should not get it from the cache - should cause further delegate execution. - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(2); } [Fact] public void Should_execute_delegate_but_not_put_value_in_cache_if_cache_does_not_hold_value_but_ttl_indicates_not_worth_caching() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.Zero); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, new Context(OperationKey)).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); } @@ -147,8 +147,8 @@ public void Should_execute_delegate_but_not_put_value_in_cache_if_cache_does_not [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; CachePolicy cache = Policy.Cache(new StubCacheProvider(), TimeSpan.MaxValue); @@ -156,16 +156,16 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_prior_exe Func func = _ => { delegateInvocations++; - return valueToReturn; + return ValueToReturn; }; - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); - cache.Execute(func, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey)).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); } @@ -223,18 +223,18 @@ public void Should_allow_custom_ICacheKeyStrategy() public void Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_reference_type() { ResultClass? valueToReturn = null; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => valueToReturn, new Context(OperationKey)).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -244,11 +244,11 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol { ResultClass? valueToReturnFromCache = null; ResultClass valueToReturnFromExecution = new ResultClass(ResultPrimitive.Good); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; @@ -256,7 +256,7 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol { delegateExecuted = true; return valueToReturnFromExecution; - }, new Context(operationKey)) + }, new Context(OperationKey)) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -266,18 +266,18 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol public void Should_execute_delegate_and_put_value_in_cache_if_cache_does_not_hold_value__default_for_value_type() { ResultPrimitive valueToReturn = default; - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => valueToReturn, new Context(OperationKey)).Should().Be(valueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); fromCache2.Should().Be(valueToReturn); } @@ -288,11 +288,11 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol ResultPrimitive valueToReturnFromCache = default; ResultPrimitive valueToReturnFromExecution = ResultPrimitive.Good; valueToReturnFromExecution.Should().NotBe(valueToReturnFromCache); - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; @@ -300,7 +300,7 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol { delegateExecuted = true; return valueToReturnFromExecution; - }, new Context(operationKey)) + }, new Context(OperationKey)) .Should().Be(valueToReturnFromCache); delegateExecuted.Should().BeFalse(); @@ -313,25 +313,25 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_outermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); Policy noop = Policy.NoOp(); PolicyWrap wrap = cache.Wrap(noop); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; wrap.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -339,25 +339,25 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_innermost_in_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); Policy noop = Policy.NoOp(); PolicyWrap wrap = noop.Wrap(cache); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; wrap.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -365,25 +365,25 @@ public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_hol [Fact] public void Should_return_value_from_cache_and_not_execute_delegate_if_cache_holds_value_when_mid_policywrap() { - const string valueToReturnFromCache = "valueToReturnFromCache"; - const string valueToReturnFromExecution = "valueToReturnFromExecution"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturnFromCache = "valueToReturnFromCache"; + const string ValueToReturnFromExecution = "valueToReturnFromExecution"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); Policy noop = Policy.NoOp(); PolicyWrap wrap = Policy.Wrap(noop, cache, noop); - stubCacheProvider.Put(operationKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); + stubCacheProvider.Put(OperationKey, ValueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; wrap.Execute(_ => { delegateExecuted = true; - return valueToReturnFromExecution; - }, new Context(operationKey)) - .Should().Be(valueToReturnFromCache); + return ValueToReturnFromExecution; + }, new Context(OperationKey)) + .Should().Be(ValueToReturnFromCache); delegateExecuted.Should().BeFalse(); } @@ -420,8 +420,8 @@ public void Should_always_execute_delegate_if_execution_key_not_set() [Fact] public void Should_honour_cancellation_even_if_prior_execution_has_cached() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; CachePolicy cache = Policy.Cache(new StubCacheProvider(), TimeSpan.MaxValue); @@ -432,15 +432,15 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached() { // delegate does not observe cancellation token; test is whether CacheEngine does. delegateInvocations++; - return valueToReturn; + return ValueToReturn; }; - cache.Execute(func, new Context(operationKey), tokenSource.Token).Should().Be(valueToReturn); + cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn); delegateInvocations.Should().Be(1); tokenSource.Cancel(); - cache.Invoking(policy => policy.Execute(func, new Context(operationKey), tokenSource.Token)) + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) .Should().Throw(); delegateInvocations.Should().Be(1); } @@ -448,8 +448,8 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached() [Fact] public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_cache() { - const string valueToReturn = "valueToReturn"; - const string operationKey = "SomeOperationKey"; + const string ValueToReturn = "valueToReturn"; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); @@ -460,13 +460,13 @@ public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_ { tokenSource.Cancel(); // simulate cancellation raised during delegate execution ct.ThrowIfCancellationRequested(); - return valueToReturn; + return ValueToReturn; }; - cache.Invoking(policy => policy.Execute(func, new Context(operationKey), tokenSource.Token)) + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) .Should().Throw(); - (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); } diff --git a/test/Polly.Specs/Caching/GenericCacheProviderAsyncSpecs.cs b/test/Polly.Specs/Caching/GenericCacheProviderAsyncSpecs.cs index df5871f95c2..3eb93b50402 100644 --- a/test/Polly.Specs/Caching/GenericCacheProviderAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/GenericCacheProviderAsyncSpecs.cs @@ -6,7 +6,7 @@ public class GenericCacheProviderAsyncSpecs : IDisposable [Fact] public async Task Should_not_error_for_executions_on_non_nullable_types_if_cache_does_not_hold_value() { - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; bool onErrorCalled = false; Action onError = (_, _, _) => { onErrorCalled = true; }; @@ -14,7 +14,7 @@ public async Task Should_not_error_for_executions_on_non_nullable_types_if_cache IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue, onError); - (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); @@ -22,7 +22,7 @@ public async Task Should_not_error_for_executions_on_non_nullable_types_if_cache { await TaskHelper.EmptyTask; return ResultPrimitive.Substitute; - }, new Context(operationKey)); + }, new Context(OperationKey)); onErrorCalled.Should().BeFalse(); } @@ -30,13 +30,13 @@ public async Task Should_not_error_for_executions_on_non_nullable_types_if_cache [Fact] public async Task Should_execute_delegate_and_put_value_in_cache_for_non_nullable_types_if_cache_does_not_hold_value() { - const ResultPrimitive valueToReturn = ResultPrimitive.Substitute; - const string operationKey = "SomeOperationKey"; + const ResultPrimitive ValueToReturn = ResultPrimitive.Substitute; + const string OperationKey = "SomeOperationKey"; IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit1, object? fromCache1) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); @@ -44,11 +44,11 @@ public async Task Should_execute_delegate_and_put_value_in_cache_for_non_nullabl { await TaskHelper.EmptyTask; return ResultPrimitive.Substitute; - }, new Context(operationKey))).Should().Be(valueToReturn); + }, new Context(OperationKey))).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(operationKey, CancellationToken.None, false); + (bool cacheHit2, object? fromCache2) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); } public void Dispose() => diff --git a/test/Polly.Specs/Caching/GenericCacheProviderSpecs.cs b/test/Polly.Specs/Caching/GenericCacheProviderSpecs.cs index 8efe5f802f1..8c0d2cbe92e 100644 --- a/test/Polly.Specs/Caching/GenericCacheProviderSpecs.cs +++ b/test/Polly.Specs/Caching/GenericCacheProviderSpecs.cs @@ -6,7 +6,7 @@ public class GenericCacheProviderSpecs : IDisposable [Fact] public void Should_not_error_for_executions_on_non_nullable_types_if_cache_does_not_hold_value() { - const string operationKey = "SomeOperationKey"; + const string OperationKey = "SomeOperationKey"; bool onErrorCalled = false; Action onError = (_, _, _) => { onErrorCalled = true; }; @@ -14,11 +14,11 @@ public void Should_not_error_for_executions_on_non_nullable_types_if_cache_does_ ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue, onError); - (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey); cacheHit.Should().BeFalse(); fromCache.Should().BeNull(); - ResultPrimitive result = cache.Execute(_ => ResultPrimitive.Substitute, new Context(operationKey)); + ResultPrimitive result = cache.Execute(_ => ResultPrimitive.Substitute, new Context(OperationKey)); onErrorCalled.Should().BeFalse(); } @@ -26,23 +26,23 @@ public void Should_not_error_for_executions_on_non_nullable_types_if_cache_does_ [Fact] public void Should_execute_delegate_and_put_value_in_cache_for_non_nullable_types_if_cache_does_not_hold_value() { - const ResultPrimitive valueToReturn = ResultPrimitive.Substitute; - const string operationKey = "SomeOperationKey"; + const ResultPrimitive ValueToReturn = ResultPrimitive.Substitute; + const string OperationKey = "SomeOperationKey"; ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit1, object? fromCache1) = stubCacheProvider.TryGet(OperationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); - cache.Execute(_ => valueToReturn, new Context(operationKey)).Should().Be(valueToReturn); + cache.Execute(_ => ValueToReturn, new Context(OperationKey)).Should().Be(ValueToReturn); - (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(operationKey); + (bool cacheHit2, object? fromCache2) = stubCacheProvider.TryGet(OperationKey); cacheHit2.Should().BeTrue(); - fromCache2.Should().Be(valueToReturn); + fromCache2.Should().Be(ValueToReturn); } public void Dispose() => diff --git a/test/Polly.Specs/Caching/ResultTtlSpecs.cs b/test/Polly.Specs/Caching/ResultTtlSpecs.cs index d89befc02eb..5dd0f5909fb 100644 --- a/test/Polly.Specs/Caching/ResultTtlSpecs.cs +++ b/test/Polly.Specs/Caching/ResultTtlSpecs.cs @@ -50,14 +50,14 @@ public void Should_return_func_result() [Fact] public void Should_return_func_result_using_context() { - const string specialKey = "specialKey"; + const string SpecialKey = "specialKey"; TimeSpan ttl = TimeSpan.FromMinutes(1); - Func func = (context, result) => context.OperationKey == specialKey ? new Ttl(TimeSpan.Zero) : new Ttl(result!.Ttl); + Func func = (context, result) => context.OperationKey == SpecialKey ? new Ttl(TimeSpan.Zero) : new Ttl(result!.Ttl); ResultTtl ttlStrategy = new ResultTtl(func); ttlStrategy.GetTtl(new Context("someOperationKey"), new { Ttl = ttl }).Timespan.Should().Be(ttl); - ttlStrategy.GetTtl(new Context(specialKey), new { Ttl = ttl }).Timespan.Should().Be(TimeSpan.Zero); + ttlStrategy.GetTtl(new Context(SpecialKey), new { Ttl = ttl }).Timespan.Should().Be(TimeSpan.Zero); } } diff --git a/test/Polly.Specs/Helpers/Bulkhead/AnnotatedOutputHelper.cs b/test/Polly.Specs/Helpers/Bulkhead/AnnotatedOutputHelper.cs index 48358718d4c..c6b47575084 100644 --- a/test/Polly.Specs/Helpers/Bulkhead/AnnotatedOutputHelper.cs +++ b/test/Polly.Specs/Helpers/Bulkhead/AnnotatedOutputHelper.cs @@ -4,12 +4,12 @@ public class AnnotatedOutputHelper : ITestOutputHelper { private class Item { - private static int monotonicSequence; + private static int MonotonicSequence; public Item(string format, object[] args) { TimeStamp = DateTimeOffset.UtcNow; - Position = Interlocked.Increment(ref monotonicSequence); + Position = Interlocked.Increment(ref MonotonicSequence); Format = format; Args = args; @@ -21,30 +21,30 @@ public Item(string format, object[] args) public object[] Args { get; } } - private readonly ConcurrentDictionary items = new(); + private readonly ConcurrentDictionary _items = new(); - private readonly object[] noArgs = Array.Empty(); + private readonly object[] _noArgs = []; - private readonly ITestOutputHelper innerOutputHelper; + private readonly ITestOutputHelper _innerOutputHelper; public AnnotatedOutputHelper(ITestOutputHelper innerOutputHelper) => - this.innerOutputHelper = innerOutputHelper ?? throw new ArgumentNullException(nameof(innerOutputHelper)); + _innerOutputHelper = innerOutputHelper ?? throw new ArgumentNullException(nameof(innerOutputHelper)); public void Flush() { // Some IDEs limit the number of lines of output displayed in a test result. Display the lines in reverse order so that we always see the most recent. - var toOutput = items.Select(kvp => kvp.Value).OrderBy(i => i.Position).Reverse(); + var toOutput = _items.Select(kvp => kvp.Value).OrderBy(i => i.Position).Reverse(); foreach (var item in toOutput) { - innerOutputHelper.WriteLine(item.TimeStamp.ToString("o") + ": " + item.Format, item.Args); + _innerOutputHelper.WriteLine(item.TimeStamp.ToString("o") + ": " + item.Format, item.Args); } - items.Clear(); + _items.Clear(); } public void WriteLine(string message) => - items.TryAdd(Guid.NewGuid(), new Item(message ?? string.Empty, noArgs)); + _items.TryAdd(Guid.NewGuid(), new Item(message ?? string.Empty, _noArgs)); public void WriteLine(string format, params object[] args) => - items.TryAdd(Guid.NewGuid(), new Item(format ?? string.Empty, args == null || args.Length == 0 ? noArgs : args)); + _items.TryAdd(Guid.NewGuid(), new Item(format ?? string.Empty, args == null || args.Length == 0 ? _noArgs : args)); } diff --git a/test/Polly.Specs/Helpers/Bulkhead/TraceableAction.cs b/test/Polly.Specs/Helpers/Bulkhead/TraceableAction.cs index 7db6027db15..4300bd4a6df 100644 --- a/test/Polly.Specs/Helpers/Bulkhead/TraceableAction.cs +++ b/test/Polly.Specs/Helpers/Bulkhead/TraceableAction.cs @@ -10,7 +10,7 @@ public class TraceableAction : IDisposable private readonly ITestOutputHelper _testOutputHelper; private readonly TaskCompletionSource _tcsProxyForRealWork = new(); - private readonly CancellationTokenSource CancellationSource = new(); + private readonly CancellationTokenSource _cancellationSource = new(); private readonly AutoResetEvent _statusChanged; private TraceableActionStatus _status; @@ -41,7 +41,7 @@ public void SignalStateChange() public Task ExecuteOnBulkhead(BulkheadPolicy bulkhead) => ExecuteThroughSyncBulkheadOuter( - () => bulkhead.Execute(_ => ExecuteThroughSyncBulkheadInner(), CancellationSource.Token)); + () => bulkhead.Execute(_ => ExecuteThroughSyncBulkheadInner(), _cancellationSource.Token)); public Task ExecuteOnBulkhead(BulkheadPolicy bulkhead) => ExecuteThroughSyncBulkheadOuter( @@ -49,7 +49,7 @@ public Task ExecuteOnBulkhead(BulkheadPolicy bulkhead) => { ExecuteThroughSyncBulkheadInner(); return default; - }, CancellationSource.Token)); + }, _cancellationSource.Token)); // Note re TaskCreationOptions.LongRunning: Testing the parallelization of the bulkhead policy efficiently requires the ability to start large numbers of parallel tasks in a short space of time. The ThreadPool's algorithm of only injecting extra threads (when necessary) at a rate of two-per-second however makes high-volume tests using the ThreadPool both slow and flaky. For PCL tests further, ThreadPool.SetMinThreads(...) is not available, to mitigate this. Using TaskCreationOptions.LongRunning allows us to force tasks to be started near-instantly on non-ThreadPool threads. private Task ExecuteThroughSyncBulkheadOuter(Action executeThroughBulkheadInner) @@ -123,7 +123,7 @@ private void ExecuteThroughSyncBulkheadInner() public Task ExecuteOnBulkheadAsync(AsyncBulkheadPolicy bulkhead) => ExecuteThroughAsyncBulkheadOuter( - () => bulkhead.ExecuteAsync(async _ => await ExecuteThroughAsyncBulkheadInner(), CancellationSource.Token)); + () => bulkhead.ExecuteAsync(async _ => await ExecuteThroughAsyncBulkheadInner(), _cancellationSource.Token)); public Task ExecuteOnBulkheadAsync(AsyncBulkheadPolicy bulkhead) => ExecuteThroughAsyncBulkheadOuter( @@ -131,7 +131,7 @@ public Task ExecuteOnBulkheadAsync(AsyncBulkheadPolicy bulkhe { await ExecuteThroughAsyncBulkheadInner(); return default; - }, CancellationSource.Token)); + }, _cancellationSource.Token)); public Task ExecuteThroughAsyncBulkheadOuter(Func executeThroughBulkheadInner) { @@ -195,7 +195,7 @@ private async Task ExecuteThroughAsyncBulkheadInner() _testOutputHelper.WriteLine(_id + "Cancelling execution."); Status = TraceableActionStatus.Canceled; - throw new OperationCanceledException(CancellationSource.Token); // Exception rethrown for the purpose of testing exceptions thrown through the BulkheadEngine. + throw new OperationCanceledException(_cancellationSource.Token); // Exception rethrown for the purpose of testing exceptions thrown through the BulkheadEngine. } else if (t.IsFaulted) { @@ -220,16 +220,16 @@ public void AllowCompletion() => public void Cancel() { - if (CancellationSource.IsCancellationRequested) + if (_cancellationSource.IsCancellationRequested) { throw new InvalidOperationException(_id + "Action has already been cancelled."); } - CancellationSource.Cancel(); + _cancellationSource.Cancel(); _tcsProxyForRealWork.SetCanceled(); } public void Dispose() => - CancellationSource.Dispose(); + _cancellationSource.Dispose(); } diff --git a/test/Polly.Specs/Helpers/Caching/StubCacheKeyStrategy.cs b/test/Polly.Specs/Helpers/Caching/StubCacheKeyStrategy.cs index 85aee76fc54..1329e5aa6fb 100644 --- a/test/Polly.Specs/Helpers/Caching/StubCacheKeyStrategy.cs +++ b/test/Polly.Specs/Helpers/Caching/StubCacheKeyStrategy.cs @@ -5,11 +5,11 @@ /// internal class StubCacheKeyStrategy : ICacheKeyStrategy { - private readonly Func strategy; + private readonly Func _strategy; public StubCacheKeyStrategy(Func strategy) => - this.strategy = strategy; + _strategy = strategy; public string GetCacheKey(Context context) => - strategy(context); + _strategy(context); } diff --git a/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs b/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs index dce2456be56..3b693028ebc 100644 --- a/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs +++ b/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs @@ -17,19 +17,19 @@ public CacheItem(object? value, Ttl ttl) public readonly object? Value; } - private readonly Dictionary cachedValues = []; + private readonly Dictionary _cachedValues = []; public (bool, object?) TryGet(string key) { - if (cachedValues.ContainsKey(key)) + if (_cachedValues.ContainsKey(key)) { - if (SystemClock.DateTimeOffsetUtcNow() < cachedValues[key].Expiry) + if (SystemClock.DateTimeOffsetUtcNow() < _cachedValues[key].Expiry) { - return (true, cachedValues[key].Value); + return (true, _cachedValues[key].Value); } else { - cachedValues.Remove(key); + _cachedValues.Remove(key); } } @@ -37,7 +37,7 @@ public CacheItem(object? value, Ttl ttl) } public void Put(string key, object? value, Ttl ttl) => - cachedValues[key] = new CacheItem(value, ttl); + _cachedValues[key] = new CacheItem(value, ttl); #region Naive async-over-sync implementation diff --git a/test/Polly.Specs/Helpers/Caching/StubErroringCacheProvider.cs b/test/Polly.Specs/Helpers/Caching/StubErroringCacheProvider.cs index b78a72398d4..ad253c82fbd 100644 --- a/test/Polly.Specs/Helpers/Caching/StubErroringCacheProvider.cs +++ b/test/Polly.Specs/Helpers/Caching/StubErroringCacheProvider.cs @@ -5,7 +5,7 @@ internal class StubErroringCacheProvider : ISyncCacheProvider, IAsyncCacheProvid private readonly Exception? _getException; private readonly Exception? _putException; - private readonly StubCacheProvider innerProvider = new(); + private readonly StubCacheProvider _innerProvider = new(); public StubErroringCacheProvider(Exception? getException, Exception? putException) { @@ -17,14 +17,14 @@ public StubErroringCacheProvider(Exception? getException, Exception? putExceptio { if (_getException != null) throw _getException; - return innerProvider.TryGet(key); + return _innerProvider.TryGet(key); } public void Put(string key, object? value, Ttl ttl) { if (_putException != null) throw _putException; - innerProvider.Put(key, value, ttl); + _innerProvider.Put(key, value, ttl); } #region Naive async-over-sync implementation diff --git a/test/Polly.Specs/Helpers/RateLimit/IRateLimiterExtensions.cs b/test/Polly.Specs/Helpers/RateLimit/IRateLimiterExtensions.cs index b01d5ee3c41..cae57bb82a7 100644 --- a/test/Polly.Specs/Helpers/RateLimit/IRateLimiterExtensions.cs +++ b/test/Polly.Specs/Helpers/RateLimit/IRateLimiterExtensions.cs @@ -4,10 +4,10 @@ internal static class IRateLimiterExtensions { public static void ShouldPermitAnExecution(this IRateLimiter rateLimiter) { - (bool permitExecution, TimeSpan retryAfter) canExecute = rateLimiter.PermitExecution(); + (bool PermitExecution, TimeSpan RetryAfter) canExecute = rateLimiter.PermitExecution(); - canExecute.permitExecution.Should().BeTrue(); - canExecute.retryAfter.Should().Be(TimeSpan.Zero); + canExecute.PermitExecution.Should().BeTrue(); + canExecute.RetryAfter.Should().Be(TimeSpan.Zero); } public static void ShouldPermitNExecutions(this IRateLimiter rateLimiter, long numberOfExecutions) @@ -20,16 +20,16 @@ public static void ShouldPermitNExecutions(this IRateLimiter rateLimiter, long n public static void ShouldNotPermitAnExecution(this IRateLimiter rateLimiter, TimeSpan? retryAfter = null) { - (bool permitExecution, TimeSpan retryAfter) canExecute = rateLimiter.PermitExecution(); + (bool PermitExecution, TimeSpan RetryAfter) canExecute = rateLimiter.PermitExecution(); - canExecute.permitExecution.Should().BeFalse(); + canExecute.PermitExecution.Should().BeFalse(); if (retryAfter == null) { - canExecute.retryAfter.Should().BeGreaterThan(TimeSpan.Zero); + canExecute.RetryAfter.Should().BeGreaterThan(TimeSpan.Zero); } else { - canExecute.retryAfter.Should().Be(retryAfter.Value); + canExecute.RetryAfter.Should().Be(retryAfter.Value); } } } diff --git a/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs b/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs index 46c407a4518..7e10400b443 100644 --- a/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs +++ b/test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs @@ -24,11 +24,11 @@ public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() [Fact] public void PolicyKey_property_should_be_the_fluently_configured_policy_key() { - const string key = "SomePolicyKey"; + const string Key = "SomePolicyKey"; - var policy = Policy.Handle().RetryAsync().WithPolicyKey(key); + var policy = Policy.Handle().RetryAsync().WithPolicyKey(Key); - policy.PolicyKey.Should().Be(key); + policy.PolicyKey.Should().Be(Key); } [Fact] @@ -208,11 +208,11 @@ public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() [Fact] public void PolicyKey_property_should_be_the_fluently_configured_policy_key() { - const string key = "SomePolicyKey"; + const string Key = "SomePolicyKey"; - var policy = Policy.HandleResult(0).RetryAsync().WithPolicyKey(key); + var policy = Policy.HandleResult(0).RetryAsync().WithPolicyKey(Key); - policy.PolicyKey.Should().Be(key); + policy.PolicyKey.Should().Be(Key); } [Fact] diff --git a/test/Polly.Specs/PolicyContextAndKeySpecs.cs b/test/Polly.Specs/PolicyContextAndKeySpecs.cs index 45ed28d2026..4c17f00c718 100644 --- a/test/Polly.Specs/PolicyContextAndKeySpecs.cs +++ b/test/Polly.Specs/PolicyContextAndKeySpecs.cs @@ -24,11 +24,11 @@ public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() [Fact] public void PolicyKey_property_should_be_the_fluently_configured_policy_key() { - const string key = "SomePolicyKey"; + const string Key = "SomePolicyKey"; - var policy = Policy.Handle().Retry().WithPolicyKey(key); + var policy = Policy.Handle().Retry().WithPolicyKey(Key); - policy.PolicyKey.Should().Be(key); + policy.PolicyKey.Should().Be(Key); } [Fact] @@ -205,11 +205,11 @@ public void Should_be_able_fluently_to_configure_the_policy_key_via_interface() [Fact] public void PolicyKey_property_should_be_the_fluently_configured_policy_key() { - const string key = "SomePolicyKey"; + const string Key = "SomePolicyKey"; - var policy = Policy.HandleResult(0).Retry().WithPolicyKey(key); + var policy = Policy.HandleResult(0).Retry().WithPolicyKey(Key); - policy.PolicyKey.Should().Be(key); + policy.PolicyKey.Should().Be(Key); } [Fact] diff --git a/test/Polly.Specs/Polly.Specs.csproj b/test/Polly.Specs/Polly.Specs.csproj index 43cf95ff43e..c69d15f0651 100644 --- a/test/Polly.Specs/Polly.Specs.csproj +++ b/test/Polly.Specs/Polly.Specs.csproj @@ -8,8 +8,8 @@ 75,60,70 [Polly]* true - $(NoWarn);S103;S104;CA2000;IDE0011;SA1600;SA1204;SA1602;S6608;IDE1006;CA2008;CA1806;CA2201; - $(NoWarn);SA1414;CA1508;S3878;CA1030;S4144;S3717;SA1129;SA1407;S1402;SA1649;SA1402;S4056;CA1031 + $(NoWarn);S103;S104;CA2000;IDE0011;SA1600;SA1204;SA1602;CA2008;CA1806;CA2201; + $(NoWarn);S3878;CA1030;S4144;S3717;SA1129;SA1407;S1402;SA1649;SA1402;S4056;CA1031 $(NoWarn);S2184; diff --git a/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs b/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs index abeeaaa11f8..e215e497441 100644 --- a/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs +++ b/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs @@ -11,7 +11,7 @@ protected abstract IRateLimitPolicy GetPolicyViaSyntax( TimeSpan perTimeSpan, int maxBurst); - protected abstract (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy policy); + protected abstract (bool PermitExecution, TimeSpan RetryAfter) TryExecuteThroughPolicy(IRateLimitPolicy policy); protected void ShouldPermitAnExecution(IRateLimitPolicy policy) { @@ -31,16 +31,16 @@ protected void ShouldPermitNExecutions(IRateLimitPolicy policy, long numberOfExe protected void ShouldNotPermitAnExecution(IRateLimitPolicy policy, TimeSpan? retryAfter = null) { - (bool permitExecution, TimeSpan retryAfter) canExecute = TryExecuteThroughPolicy(policy); + (bool PermitExecution, TimeSpan RetryAfter) canExecute = TryExecuteThroughPolicy(policy); - canExecute.permitExecution.Should().BeFalse(); + canExecute.PermitExecution.Should().BeFalse(); if (retryAfter == null) { - canExecute.retryAfter.Should().BeGreaterThan(TimeSpan.Zero); + canExecute.RetryAfter.Should().BeGreaterThan(TimeSpan.Zero); } else { - canExecute.retryAfter.Should().Be(retryAfter.Value); + canExecute.RetryAfter.Should().Be(retryAfter.Value); } } @@ -282,7 +282,7 @@ public void Given_immediate_parallel_contention_ratelimiter_still_only_permits_o // Arrange - parallel tasks all waiting on a manual reset event. ManualResetEventSlim gate = new(); - Task<(bool permitExecution, TimeSpan retryAfter)>[] tasks = new Task<(bool, TimeSpan)>[parallelContention]; + Task<(bool PermitExecution, TimeSpan RetryAfter)>[] tasks = new Task<(bool, TimeSpan)>[parallelContention]; for (int i = 0; i < parallelContention; i++) { tasks[i] = Task.Run(() => @@ -300,7 +300,7 @@ public void Given_immediate_parallel_contention_ratelimiter_still_only_permits_o // Assert - one should have permitted execution, n-1 not. var results = tasks.Select(t => t.Result).ToList(); - results.Count(r => r.permitExecution).Should().Be(1); - results.Count(r => !r.permitExecution).Should().Be(parallelContention - 1); + results.Count(r => r.PermitExecution).Should().Be(1); + results.Count(r => !r.PermitExecution).Should().Be(parallelContention - 1); } } diff --git a/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs b/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs index 74d9afffc4b..cdf067bd52b 100644 --- a/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs +++ b/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs @@ -181,7 +181,7 @@ public void Given_immediate_parallel_contention_ratelimiter_still_only_permits_o // Arrange - parallel tasks all waiting on a manual reset event. ManualResetEventSlim gate = new ManualResetEventSlim(); - Task<(bool permitExecution, TimeSpan retryAfter)>[] tasks = new Task<(bool, TimeSpan)>[parallelContention]; + Task<(bool PermitExecution, TimeSpan RetryAfter)>[] tasks = new Task<(bool, TimeSpan)>[parallelContention]; for (int i = 0; i < parallelContention; i++) { tasks[i] = Task.Run(() => @@ -199,7 +199,7 @@ public void Given_immediate_parallel_contention_ratelimiter_still_only_permits_o // Assert - one should have permitted execution, n-1 not. var results = tasks.Select(t => t.Result).ToList(); - results.Count(r => r.permitExecution).Should().Be(1); - results.Count(r => !r.permitExecution).Should().Be(parallelContention - 1); + results.Count(r => r.PermitExecution).Should().Be(1); + results.Count(r => !r.PermitExecution).Should().Be(parallelContention - 1); } } diff --git a/test/Polly.Specs/Registry/ConcurrentPolicyRegistrySpecs.cs b/test/Polly.Specs/Registry/ConcurrentPolicyRegistrySpecs.cs index fb36bfc9548..51b0694d6c2 100644 --- a/test/Polly.Specs/Registry/ConcurrentPolicyRegistrySpecs.cs +++ b/test/Polly.Specs/Registry/ConcurrentPolicyRegistrySpecs.cs @@ -215,18 +215,18 @@ public void Should_update_with_AddOrUpdate_with_updatefactory_ignoring_addvalue_ string key = Guid.NewGuid().ToString(); _registry.Add(key, existingPolicy); - const string policyKeyToDecorate = "SomePolicyKey"; + const string PolicyKeyToDecorate = "SomePolicyKey"; Policy otherPolicyNotExpectingToAdd = Policy.Handle().Retry(); var returnedPolicy = _registry.AddOrUpdate( key, otherPolicyNotExpectingToAdd, - (_, _) => existingPolicy.WithPolicyKey(policyKeyToDecorate)); + (_, _) => existingPolicy.WithPolicyKey(PolicyKeyToDecorate)); returnedPolicy.Should().NotBeSameAs(otherPolicyNotExpectingToAdd); returnedPolicy.Should().BeSameAs(existingPolicy); - returnedPolicy.PolicyKey.Should().Be(policyKeyToDecorate); + returnedPolicy.PolicyKey.Should().Be(PolicyKeyToDecorate); } [Fact] @@ -236,17 +236,17 @@ public void Should_update_with_AddOrUpdate_with_updatefactory_ignoring_addfactor string key = Guid.NewGuid().ToString(); _registry.Add(key, existingPolicy); - const string policyKeyToDecorate = "SomePolicyKey"; + const string PolicyKeyToDecorate = "SomePolicyKey"; Policy otherPolicyNotExpectingToAdd = Policy.Handle().Retry(); var returnedPolicy = _registry.AddOrUpdate( key, _ => otherPolicyNotExpectingToAdd, - (_, _) => existingPolicy.WithPolicyKey(policyKeyToDecorate)); + (_, _) => existingPolicy.WithPolicyKey(PolicyKeyToDecorate)); returnedPolicy.Should().NotBeSameAs(otherPolicyNotExpectingToAdd); returnedPolicy.Should().BeSameAs(existingPolicy); - returnedPolicy.PolicyKey.Should().Be(policyKeyToDecorate); + returnedPolicy.PolicyKey.Should().Be(PolicyKeyToDecorate); } }