public async Task AppliesDistributedCacheSoftTimeoutAsync() { var simulatedDelayMs = 5_000; var distributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); var chaosDistributedCache = new ChaosDistributedCache(distributedCache); chaosDistributedCache.SetAlwaysDelayExactly(TimeSpan.FromMilliseconds(simulatedDelayMs)); using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { using (var fusionCache = new FusionCache(new FusionCacheOptions(), memoryCache)) { fusionCache.SetupDistributedCache(chaosDistributedCache, new FusionCacheNewtonsoftJsonSerializer()); await fusionCache.SetAsync <int>("foo", 42, new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true)); await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); var sw = Stopwatch.StartNew(); var res = await fusionCache.GetOrSetAsync <int>("foo", async ct => throw new Exception("Sloths are cool"), new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true).SetDistributedCacheTimeouts(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1_000))); sw.Stop(); Assert.Equal(42, res); Assert.True(sw.ElapsedMilliseconds >= 100, "Distributed cache soft timeout not applied"); Assert.True(sw.ElapsedMilliseconds < simulatedDelayMs, "Distributed cache soft timeout not applied"); } } }
public void AppliesDistributedCacheHardTimeout() { var simulatedDelayMs = 5_000; var distributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); var chaosDistributedCache = new ChaosDistributedCache(distributedCache); chaosDistributedCache.SetAlwaysDelayExactly(TimeSpan.FromMilliseconds(simulatedDelayMs)); using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { using (var fusionCache = new FusionCache(new FusionCacheOptions(), memoryCache)) { fusionCache.SetupDistributedCache(chaosDistributedCache, new FusionCacheNewtonsoftJsonSerializer()); fusionCache.Set <int>("foo", 42, new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true)); Thread.Sleep(TimeSpan.FromSeconds(1)); memoryCache.Remove("foo"); Assert.Throws <Exception>(() => { _ = fusionCache.GetOrSet <int>("foo", ct => throw new Exception("Sloths are cool"), new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true).SetDistributedCacheTimeouts(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1_000))); }); } } }
public async Task AppliesDistributedCacheHardTimeoutAsync() { var simulatedDelayMs = 5_000; var distributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); var chaosDistributedCache = new ChaosDistributedCache(distributedCache); chaosDistributedCache.SetAlwaysDelayExactly(TimeSpan.FromMilliseconds(simulatedDelayMs)); using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { using (var fusionCache = new FusionCache(new FusionCacheOptions(), memoryCache)) { fusionCache.SetupDistributedCache(chaosDistributedCache, new FusionCacheNewtonsoftJsonSerializer()); await fusionCache.SetAsync <int>("foo", 42, new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true)); await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); memoryCache.Remove("foo"); await Assert.ThrowsAsync <Exception>(async() => { var res = await fusionCache.GetOrSetAsync <int>("foo", async ct => throw new Exception("Sloths are cool"), new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true).SetDistributedCacheTimeouts(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1_000))); }); } } }
public void AppliesDistributedCacheSoftTimeout(SerializerType serializerType) { var simulatedDelayMs = 5_000; var distributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); var chaosDistributedCache = new ChaosDistributedCache(distributedCache); chaosDistributedCache.SetAlwaysDelayExactly(TimeSpan.FromMilliseconds(simulatedDelayMs)); using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { using (var fusionCache = new FusionCache(new FusionCacheOptions(), memoryCache)) { fusionCache.SetupDistributedCache(chaosDistributedCache, GetSerializer(serializerType)); fusionCache.Set <int>("foo", 42, new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true)); Thread.Sleep(TimeSpan.FromSeconds(1)); var sw = Stopwatch.StartNew(); var res = fusionCache.GetOrSet <int>("foo", ct => throw new Exception("Sloths are cool"), new FusionCacheEntryOptions().SetDurationSec(1).SetFailSafe(true).SetDistributedCacheTimeouts(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1_000))); sw.Stop(); Assert.Equal(42, res); Assert.True(sw.ElapsedMilliseconds >= 100, "Distributed cache soft timeout not applied"); Assert.True(sw.ElapsedMilliseconds < simulatedDelayMs, "Distributed cache soft timeout not applied"); } } }