コード例 #1
0
        public async Task HandlesDistributedCacheFailuresInTheMiddleOfAnOperationAsync()
        {
            using (var memoryCache = new MemoryCache(new MemoryCacheOptions()))
            {
                var distributedCache      = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
                var chaosDistributedCache = new ChaosDistributedCache(distributedCache);
                using (var fusionCache = new FusionCache(new FusionCacheOptions(), memoryCache).SetupDistributedCache(chaosDistributedCache, new FusionCacheNewtonsoftJsonSerializer()))
                {
                    var task = fusionCache.GetOrSetAsync <int>("foo", async _ => { await Task.Delay(2_000); return(42); }, new FusionCacheEntryOptions(TimeSpan.FromSeconds(10)));
                    await Task.Delay(500);

                    chaosDistributedCache.SetAlwaysThrow();
                    var value = await task;
                    chaosDistributedCache.SetNeverThrow();

                    // END RESULT IS WHAT EXPECTED
                    Assert.Equal(42, value);

                    // MEMORY CACHE HAS BEEN UPDATED
                    Assert.Equal(42, memoryCache.Get <FusionCacheEntry <int> >("foo").Value);

                    // DISTRIBUTED CACHE HAS -NOT- BEEN UPDATED
                    Assert.Null(distributedCache.GetString("foo"));
                }
            }
        }
コード例 #2
0
        public void DistributedCacheCircuitBreakerActuallyWorks()
        {
            var circuitBreakerDuration = TimeSpan.FromSeconds(2);
            var distributedCache       = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
            var chaosDistributedCache  = new ChaosDistributedCache(distributedCache);

            using (var memoryCache = new MemoryCache(new MemoryCacheOptions()))
            {
                using (var fusionCache = new FusionCache(new FusionCacheOptions()
                {
                    DistributedCacheCircuitBreakerDuration = circuitBreakerDuration
                }, memoryCache))
                {
                    fusionCache.DefaultEntryOptions.AllowBackgroundDistributedCacheOperations = false;
                    fusionCache.SetupDistributedCache(chaosDistributedCache, new FusionCacheNewtonsoftJsonSerializer());

                    fusionCache.Set <int>("foo", 1, options => options.SetDurationSec(60).SetFailSafe(true));
                    chaosDistributedCache.SetAlwaysThrow();
                    fusionCache.Set <int>("foo", 2, options => options.SetDurationSec(60).SetFailSafe(true));
                    chaosDistributedCache.SetNeverThrow();
                    fusionCache.Set <int>("foo", 3, options => options.SetDurationSec(60).SetFailSafe(true));
                    Thread.Sleep(circuitBreakerDuration);
                    memoryCache.Remove("foo");
                    var res = fusionCache.GetOrDefault <int>("foo", -1);

                    Assert.Equal(1, res);
                }
            }
        }
コード例 #3
0
        public async Task DistributedCacheCircuitBreakerActuallyWorksAsync(SerializerType serializerType)
        {
            var circuitBreakerDuration = TimeSpan.FromSeconds(2);
            var distributedCache       = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
            var chaosDistributedCache  = new ChaosDistributedCache(distributedCache);

            using (var memoryCache = new MemoryCache(new MemoryCacheOptions()))
            {
                using (var fusionCache = new FusionCache(new FusionCacheOptions()
                {
                    DistributedCacheCircuitBreakerDuration = circuitBreakerDuration
                }, memoryCache))
                {
                    fusionCache.DefaultEntryOptions.AllowBackgroundDistributedCacheOperations = false;
                    fusionCache.SetupDistributedCache(chaosDistributedCache, GetSerializer(serializerType));

                    await fusionCache.SetAsync <int>("foo", 1, options => options.SetDurationSec(60).SetFailSafe(true));

                    chaosDistributedCache.SetAlwaysThrow();
                    await fusionCache.SetAsync <int>("foo", 2, options => options.SetDurationSec(60).SetFailSafe(true));

                    chaosDistributedCache.SetNeverThrow();
                    await fusionCache.SetAsync <int>("foo", 3, options => options.SetDurationSec(60).SetFailSafe(true));

                    await Task.Delay(circuitBreakerDuration).ConfigureAwait(false);

                    memoryCache.Remove("foo");
                    var res = await fusionCache.GetOrDefaultAsync <int>("foo", -1);

                    Assert.Equal(1, res);
                }
            }
        }
コード例 #4
0
        public async Task HandlesDistributedCacheFailuresAsync()
        {
            var distributedCache      = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
            var chaosDistributedCache = new ChaosDistributedCache(distributedCache);

            using (var fusionCache = new FusionCache(new FusionCacheOptions()).SetupDistributedCache(chaosDistributedCache, new FusionCacheNewtonsoftJsonSerializer()))
            {
                var initialValue = await fusionCache.GetOrSetAsync <int>("foo", _ => Task.FromResult(42), new FusionCacheEntryOptions()
                {
                    Duration = TimeSpan.FromSeconds(1), IsFailSafeEnabled = true
                });

                await Task.Delay(1_500);

                chaosDistributedCache.SetAlwaysThrow();
                var newValue = await fusionCache.GetOrSetAsync <int>("foo", async _ => throw new Exception("Generic error"), new FusionCacheEntryOptions(TimeSpan.FromSeconds(1)) { IsFailSafeEnabled = true });;
                Assert.Equal(initialValue, newValue);
            }
        }