public async Task ErrorHandling_Async_UsePreviousValue() { bool isInvalid = false; Func <CachedValue <double>, CacheValidationResult> validate = val => isInvalid ? CacheValidationResult.Invalid : CacheValidationResult.Unknown; CacheStrategyAsync <int> cacheStrategy = CreateCache().Method(c => c.RandomValueThatThrowsExceptionOnSecondTryAsync()) .IfRetrievalFailsUsePreviousValue(); int previousValue = await cacheStrategy.GetValueAsync(); isInvalid = true; //force a second retrieval by invalidating the previous value int newValue = await cacheStrategy.GetValueAsync(); Assert.AreEqual(previousValue, newValue, "The retrieval error handler should have kicked in"); }
public async Task ErrorHandling_Async_NoErrorHandling() { CacheStrategyAsync <int> cacheStrategy = CreateCache().Method(c => c.ThrowsExceptionImmediatelyAsync()) .IfRetrievalFailsUsePreviousValue(); await cacheStrategy.GetValueAsync(); }
public async Task Method_Async_GetValue_NotDefault(Cache <Example> cache) { CacheStrategyAsync <double> strategy = cache.Method(c => c.CalculateSomeWorkAsync(3)); double value = await strategy.GetValueAsync(); Assert.AreNotEqual(0d, value); }
public async Task CacheMethod_Method_GetValueAsync() { var cache = CreateCache(); CacheStrategyAsync <string> strat = cache.Method(t => t.DownloadTextAsync(t.TestProperty)); string result = await strat.GetValueAsync(); Assert.AreNotEqual(null, result); }
public async Task CacheMethod_Method_GetValueAsync_LocalArgument() { var cache = CreateCache(); var arg = new MethodTestsArguments().Property; CacheStrategyAsync <string> strat = cache.Method(t => t.MethodAsync(arg)); string result = await strat.GetValueAsync(); Assert.AreNotEqual(null, result); }
public async Task ErrorHandling_Async_UsePreviousValueOrDefault() { int defaultValue = -1111; CacheStrategyAsync <int> cacheStrategy = CreateCache().Method(c => c.ThrowsExceptionImmediatelyAsync()) .IfRetrievalFailsUsePreviousValueOrDefault(defaultValue); int value = await cacheStrategy.GetValueAsync(); Assert.AreEqual(defaultValue, value, "The retrieval error handler should have kicked in and returned the default value"); }
public async Task CacheMethod_Method_GetValue_IgnoreParameter() { string url = "http://www.google.com"; string url2 = "http://www.cnn.com"; var cache = CreateCache(); CacheStrategyAsync <string> strat1 = cache.Method(t => t.DownloadTextAsync(Parameter.DoNotCache(url))); CacheStrategyAsync <string> strat2 = cache.Method(t => t.DownloadTextAsync(Parameter.DoNotCache(url2))); Assert.AreEqual(strat1.Key, strat2.Key, "Both strategies should have the same key because the parameter should be ignored"); string result = await strat1.GetValueAsync(); strat1.ClearValue(); string result2 = await strat2.GetValueAsync(); Assert.AreNotEqual(result, result2, "These should have returned different results because the parameter values were different"); }
public async Task CacheMethod_Method_GetValue_IgnoreParameter() { string arg1 = "hello"; string arg2 = "world"; var cache = CreateCache(); CacheStrategyAsync <string> strat1 = cache.Method(t => t.MethodAsync(Parameter.DoNotCache(arg1))); CacheStrategyAsync <string> strat2 = cache.Method(t => t.MethodAsync(Parameter.DoNotCache(arg2))); Assert.AreEqual(strat1.Key, strat2.Key, "Both strategies should have the same key because the parameter should be ignored"); string result = await strat1.GetValueAsync(); strat1.ClearValue(); string result2 = await strat2.GetValueAsync(); Assert.AreNotEqual(result, result2, "These should have returned different results because the parameter values were different"); }