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