public async Task Should_call_onError_delegate_if_cache_get_errors() { Exception ex = new Exception(); IAsyncCacheProvider stubCacheProvider = new StubErroringCacheProvider(getException: ex, putException: null); Exception exceptionFromCacheProvider = null; const string valueToReturnFromCache = "valueToReturnFromCache"; const string valueToReturnFromExecution = "valueToReturnFromExecution"; const string executionKey = "SomeExecutionKey"; Action <Context, string, Exception> onError = (ctx, key, exc) => { exceptionFromCacheProvider = exc; }; CachePolicy cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue, onError); await stubCacheProvider.PutAsync(executionKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue), CancellationToken.None, false).ConfigureAwait(false); bool delegateExecuted = false; // Even though value is in cache, get will error; so value is returned from execution. (await cache.ExecuteAsync(async() => { delegateExecuted = true; await TaskHelper.EmptyTask.ConfigureAwait(false); return(valueToReturnFromExecution); }, new Context(executionKey)) .ConfigureAwait(false)) .Should().Be(valueToReturnFromExecution); delegateExecuted.Should().BeTrue(); // And error should be captured by onError delegate. exceptionFromCacheProvider.Should().Be(ex); }
public async Task Should_call_onError_delegate_if_cache_put_errors() { Exception ex = new Exception(); IAsyncCacheProvider stubCacheProvider = new StubErroringCacheProvider(getException: null, putException: ex); Exception exceptionFromCacheProvider = null; const string valueToReturn = "valueToReturn"; const string operationKey = "SomeOperationKey"; Action <Context, string, Exception> onError = (_, _, exc) => { exceptionFromCacheProvider = exc; }; var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue, onError); (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); // 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); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); }
public void Should_call_onError_delegate_if_cache_get_errors() { Exception ex = new Exception(); ISyncCacheProvider stubCacheProvider = new StubErroringCacheProvider(getException: ex, putException: null); Exception exceptionFromCacheProvider = null; const string valueToReturnFromCache = "valueToReturnFromCache"; const string valueToReturnFromExecution = "valueToReturnFromExecution"; const string executionKey = "SomeExecutionKey"; Action <Context, string, Exception> onError = (ctx, key, exc) => { exceptionFromCacheProvider = exc; }; CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue, onError); stubCacheProvider.Put(executionKey, valueToReturnFromCache, new Ttl(TimeSpan.MaxValue)); bool delegateExecuted = false; // Even though value is in cache, get will error; so value is returned from execution. cache.Execute(() => { delegateExecuted = true; return(valueToReturnFromExecution); }, new Context(executionKey)) .Should().Be(valueToReturnFromExecution); delegateExecuted.Should().BeTrue(); // And error should be captured by onError delegate. exceptionFromCacheProvider.Should().Be(ex); }
public void Should_call_onError_delegate_if_cache_put_errors() { Exception ex = new Exception(); ISyncCacheProvider stubCacheProvider = new StubErroringCacheProvider(getException: null, putException: ex); Exception exceptionFromCacheProvider = null; const string valueToReturn = "valueToReturn"; const string operationKey = "SomeOperationKey"; Action <Context, string, Exception> onError = (ctx, key, exc) => { exceptionFromCacheProvider = exc; }; CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue, onError); (bool cacheHit1, object fromCache1) = stubCacheProvider.TryGet(operationKey); cacheHit1.Should().BeFalse(); fromCache1.Should().BeNull(); cache.Execute(ctx => { 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) = stubCacheProvider.TryGet(operationKey); cacheHit2.Should().BeFalse(); fromCache2.Should().BeNull(); }
public async Task Should_call_onError_delegate_if_cache_put_errors() { Exception ex = new Exception(); IAsyncCacheProvider stubCacheProvider = new StubErroringCacheProvider(getException: null, putException: ex); Exception exceptionFromCacheProvider = null; const string valueToReturn = "valueToReturn"; const string executionKey = "SomeExecutionKey"; Action <Context, string, Exception> onError = (ctx, key, exc) => { exceptionFromCacheProvider = exc; }; CachePolicy cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue, onError); ((string)await stubCacheProvider.GetAsync(executionKey, CancellationToken.None, false).ConfigureAwait(false)).Should().BeNull(); (await cache.ExecuteAsync(async() => { await TaskHelper.EmptyTask.ConfigureAwait(false); return(valueToReturn); }, new Context(executionKey)).ConfigureAwait(false)).Should().Be(valueToReturn); // error should be captured by onError delegate. exceptionFromCacheProvider.Should().Be(ex); // failed to put it in the cache ((string)await stubCacheProvider.GetAsync(executionKey, CancellationToken.None, false).ConfigureAwait(false)).Should().BeNull(); }