Ejemplo n.º 1
0
        public void Set_String_ShouldHaveValueInCache()
        {
            _distributedCacheProvider.Set(Key, Value, TimeSpan.FromHours(1));

            var cached = JsonConvert.DeserializeObject <string>(_memoryDistributedCache.GetString(Key));

            cached.ShouldBe(Value);
        }
Ejemplo n.º 2
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"));
                }
            }
        }
Ejemplo n.º 3
0
    public void Post_StoresNewGameInCache()
    {
        var result = _controller.Post();
        var gameId = result.Value.Should().BeAssignableTo <GameState>().Subject.GameId;

        _memoryCache.GetString(gameId).Should().NotBeNullOrEmpty();
    }
        public void Remove_FunctionWithoutParameters_ShouldBeRemoved()
        {
            var key = _distributedCacheService.GetKey(() => _testFunctions.FunctionWithoutParameters());

            _memoryDistributedCache.SetString(key, JsonConvert.SerializeObject(Value));
            var beforeRemove = _memoryDistributedCache.GetString(key);

            _distributedCacheService.Remove(() => _testFunctions.FunctionWithoutParameters());
            var afterRemove = _memoryDistributedCache.GetString(key);

            beforeRemove.Should().NotBeNull();
            afterRemove.Should().BeNull();
        }
        public async Task ScheduleTest_TestMethod_EqualTestMethod()
        {
            //Arrange
            var options             = Options.Create(new MemoryDistributedCacheOptions());
            var cacheRepository     = new MemoryDistributedCache(options);
            var delayedPRController = new DelayedPRController(cacheRepository);

            //Act
            var key = await delayedPRController.Shedule(() => _testClassObject.TestMethod("test", 1), DateTime.UtcNow.AddSeconds(5));

            var data = cacheRepository.GetString(key);

            var cachedObject = JsonConvert.DeserializeObject <CachedJob>(data);

            //Assert
            Assert.IsFalse(string.IsNullOrEmpty(key));
            Assert.AreEqual(typeof(TestClass).Assembly.GetName().FullName, cachedObject.AssemblyName);
            Assert.AreEqual(_testClassObject.GetType().FullName, cachedObject.ClassName);
            Assert.AreEqual("TestMethod", cachedObject.MethodName);
            Assert.AreEqual(typeof(string).FullName, cachedObject.MethodParameters[0].TypeName);
            Assert.AreEqual("test", cachedObject.MethodParameters[0].Value);
            Assert.AreEqual(typeof(int).FullName, cachedObject.MethodParameters[1].TypeName);
            Assert.AreEqual(1, int.Parse(cachedObject.MethodParameters[1].Value));
        }