public async Task Redis_Multiple_PubSub_ClearRegion()
        {
            // arrange
            var item        = new CacheItem <object>(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "something");
            var channelName = Guid.NewGuid().ToString();

            // act/assert
            await RedisTests.RunMultipleCaches(
                async (cacheA, cacheB) =>
            {
                cacheA.Add(item);
                cacheB.Get(item.Key, item.Region).Should().Be(item.Value);
                cacheB.ClearRegion(item.Region);
                await Task.Delay(0);
            },
                async (cache) =>
            {
                cache.Get(item.Key, item.Region).Should().BeNull();
                await Task.Delay(0);
            },
                2,
                TestManagers.CreateRedisCache(5),
                TestManagers.CreateRedisCache(5),
                TestManagers.CreateRedisCache(5),
                TestManagers.CreateRedisCache(5));
        }
Example #2
0
        public void Redis_Multiple_PubSub_Remove()
        {
            // arrange
            var item = new CacheItem <object>(Guid.NewGuid().ToString(), "something");

            // act/assert
            RedisTests.RunMultipleCaches(
                (cacheA, cacheB) =>
            {
                cacheA.Add(item);
                cacheB.Get(item.Key).Should().Be(item.Value);
                cacheB.Remove(item.Key);
            },
                (cache) =>
            {
                Thread.Sleep(10);
                var value = cache.GetCacheItem(item.Key);
                value.Should().BeNull();
            },
                2,
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(6),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(6),
                TestManagers.CreateRedisCache(6),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(6));
        }
Example #3
0
        public void Redis_ValueConverter_Poco_Update()
        {
            var cache = TestManagers.CreateRedisCache(17, false, Serializer.Json);

            // act/assert
            using (cache)
            {
                var key    = Guid.NewGuid().ToString();
                var region = Guid.NewGuid().ToString();
                var value  = new Poco()
                {
                    Id = 23, Something = "§asdad"
                };
                cache.Add(key, value, region);

                var newValue = new Poco()
                {
                    Id = 24, Something = "%!else$&"
                };
                object      resultValue = null;
                Func <bool> act         = () => cache.TryUpdate(key, region, (o) => newValue, out resultValue);

                act().Should().BeTrue();
                newValue.ShouldBeEquivalentTo(resultValue);
            }
        }
Example #4
0
        public void Redis_Multiple_PubSub_Change()
        {
            // arrange
            var item = new CacheItem <object>(Guid.NewGuid().ToString(), "something");

            // act/assert
            RedisTests.RunMultipleCaches(
                (cacheA, cacheB) =>
            {
                cacheA.Put(item);
                Thread.Sleep(1);
                var value = cacheB.Get(item.Key);
                value.Should().Be(item.Value);
                cacheB.Put(item.Key, "new value");
            },
                (cache) =>
            {
                Thread.Sleep(1);
                var value = cache.Get(item.Key);
                value.Should().Be("new value");
            },
                3,
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(3),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(3),
                TestManagers.CreateRedisCache(3),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(3));
        }
Example #5
0
        public void Redis_Absolute_DoesExpire_MultiClients()
        {
            // arrange
            var cacheA = TestManagers.CreateRedisCache(2);
            var cacheB = TestManagers.CreateRedisCache(2);

            // act/assert
            using (cacheA)
                using (cacheB)
                {
                    // act
                    var item = new CacheItem <object>(Guid.NewGuid().ToString(), "something", ExpirationMode.Absolute, TimeSpan.FromMilliseconds(100));

                    var result = cacheA.Add(item);

                    var itemB = cacheB.GetCacheItem(item.Key);

                    // assert
                    result.Should().BeTrue();
                    item.Value.Should().Be(itemB.Value);

                    Thread.Sleep(30);
                    cacheA.GetCacheItem(item.Key).Should().NotBeNull();
                    cacheB.GetCacheItem(item.Key).Should().NotBeNull();

                    // after 130ms both it should be expired
                    Thread.Sleep(100);
                    cacheA.GetCacheItem(item.Key).Should().BeNull();
                    cacheB.GetCacheItem(item.Key).Should().BeNull();
                }
        }
            public async Task Redis_Absolute_DoesExpire_MultiClients()
            {
                // arrange
                var timeout = 50;
                var cacheA  = TestManagers.CreateRedisCache(2);
                var cacheB  = TestManagers.CreateRedisCache(2);

                // act/assert
                using (cacheA)
                    using (cacheB)
                    {
                        await TestAbsoluteExpiration(
                            timeout,
                            (key) => cacheA.Add(new CacheItem <object>(key, "something", ExpirationMode.Absolute, TimeSpan.FromMilliseconds(timeout))),
                            (key) =>
                        {
                            var a = cacheA.GetCacheItem(key);
                            var b = cacheB.GetCacheItem(key);
                            if (a == null || b == null)
                            {
                                return(null);
                            }

                            return(a);
                        });
                    }
            }
Example #7
0
        public void Redis_Multiple_PubSub_Remove()
        {
            // arrange
            var item        = new CacheItem <object>(Guid.NewGuid().ToString(), "something");
            var channelName = Guid.NewGuid().ToString();

            // act/assert
            RedisTests.RunMultipleCaches(
                (cacheA, cacheB) =>
            {
                cacheA.Add(item);
                cacheB.Get(item.Key).Should().Be(item.Value);
                cacheB.Remove(item.Key);
                Thread.Sleep(10);
            },
                (cache) =>
            {
                int tries    = 0;
                object value = null;
                do
                {
                    tries++;
                    Thread.Sleep(100);
                    value = cache.GetCacheItem(item.Key);
                }while (value != null && tries < 50);

                value.Should().BeNull();
            },
                1,
                TestManagers.CreateRedisAndDicCacheWithBackplane(6, true, channelName),
                TestManagers.CreateRedisAndDicCacheWithBackplane(6, true, channelName),
                TestManagers.CreateRedisCache(6),
                TestManagers.CreateRedisAndDicCacheWithBackplane(6, true, channelName));
        }
Example #8
0
        public void Redis_Sliding_DoesExpire_WithRegion()
        {
            // arrange
            var item  = new CacheItem <object>(Guid.NewGuid().ToString(), "something", "region", ExpirationMode.Sliding, TimeSpan.FromMilliseconds(50));
            var cache = TestManagers.CreateRedisCache(11);

            // act/assert
            using (cache)
            {
                // act
                var result = cache.Add(item);

                // assert
                result.Should().BeTrue();

                // 450ms added so absolute would be expired on the 2nd go
                for (int s = 0; s < 3; s++)
                {
                    Thread.Sleep(30);
                    var value = cache.GetCacheItem(item.Key, item.Region);
                    value.Should().NotBeNull();
                }

                Thread.Sleep(60);
                var valueExpired = cache.GetCacheItem(item.Key, item.Region);
                valueExpired.Should().BeNull();
            }
        }
Example #9
0
        public void Redis_ValueConverter_ObjectCacheTypeConversion_Bytes()
        {
            var cache = TestManagers.CreateRedisCache(13);

            // act/assert
            using (cache)
            {
                var value = new byte[] { 0, 1, 2, 3 };
                var key   = Guid.NewGuid().ToString();
                cache.Add(key, value);
                var result = cache.Get(key) as byte[];
                value.Should().BeEquivalentTo(result);
            }
        }
Example #10
0
        public void Redis_ValueConverter_ObjectCacheTypeConversion_Double()
        {
            var cache = TestManagers.CreateRedisCache(14);

            // act/assert
            using (cache)
            {
                var value = 0231.2d;
                var key   = Guid.NewGuid().ToString();
                cache.Add(key, value);
                var result = (double)cache.Get(key);
                value.Should().Be(result);
            }
        }
Example #11
0
        public void Redis_ValueConverter_ObjectCacheTypeConversion_Long()
        {
            var cache = TestManagers.CreateRedisCache(16);

            // act/assert
            using (cache)
            {
                var key   = Guid.NewGuid().ToString();
                var value = 123456L;
                cache.Add(key, value);
                var result = (long)cache.Get(key);
                value.Should().Be(result);
            }
        }
Example #12
0
        public void Redis_Multiple_PubSub_Change()
        {
            // arrange
            string fileName    = BaseCacheManagerTest.GetCfgFileName(@"/Configuration/configuration.valid.allFeatures.config");
            var    channelName = Guid.NewGuid().ToString();

            // redis config name must be same for all cache handles, configured via file and via code
            // otherwise the pub sub channel name is different
            string cacheName = "redisConfigFromConfig";

            RedisConfigurations.LoadConfiguration(fileName, RedisConfigurationSection.DefaultSectionName);

            var cfg = (CacheManagerConfiguration)ConfigurationBuilder.LoadConfigurationFile(fileName, cacheName);

            cfg.BackplaneChannelName = channelName;

            var cfgCache = CacheFactory.FromConfiguration <object>(cfg);

            var item = new CacheItem <object>(Guid.NewGuid().ToString(), "something");

            // act/assert
            RedisTests.RunMultipleCaches(
                (cacheA, cacheB) =>
            {
                cacheA.Put(item);
                cacheA.Get(item.Key).Should().Be("something");
                Thread.Sleep(10);
                var value = cacheB.Get(item.Key);
                value.Should().Be(item.Value, cacheB.ToString());
                cacheB.Put(item.Key, "new value");
            },
                (cache) =>
            {
                int tries    = 0;
                object value = null;
                do
                {
                    tries++;
                    Thread.Sleep(100);
                    value = cache.Get(item.Key);
                }while (value.ToString() != "new value" && tries < 10);

                value.Should().Be("new value", cache.ToString());
            },
                1,
                TestManagers.CreateRedisAndDicCacheWithBackplane(113, true, channelName, Serializer.Json),
                cfgCache,
                TestManagers.CreateRedisCache(113, false, Serializer.Json),
                TestManagers.CreateRedisAndDicCacheWithBackplane(113, true, channelName, Serializer.Json));
        }
Example #13
0
        public void Redis_ValueConverter_ObjectCacheTypeConversion_String()
        {
            var cache = TestManagers.CreateRedisCache(18);

            // act/assert
            using (cache)
            {
                var key   = Guid.NewGuid().ToString();
                var value = "some string";
                cache.Add(key, value);
                var result = cache.Get(key) as string;
                value.Should().Be(result);
            }
        }
            public async Task Redis_Sliding_DoesExpire()
            {
                // arrange
                var timeout = 50;
                var cache   = TestManagers.CreateRedisCache(9);

                // act/assert
                using (cache)
                {
                    await TestSlidingExpiration(
                        timeout,
                        (key) => cache.Add(new CacheItem <object>(key, "something", ExpirationMode.Sliding, TimeSpan.FromMilliseconds(timeout))),
                        (key) => cache.GetCacheItem(key));
                }
            }
        public void Redis_ExpirationTimeoutLimit()
        {
            // arrange
            var timeout = TimeSpan.FromTicks(100);
            var cache   = TestManagers.CreateRedisCache(1);
            var key     = Guid.NewGuid().ToString();

            // act/assert
            using (cache)
            {
                Action act = () => cache.Add(new CacheItem <object>(key, key, ExpirationMode.Absolute, timeout));

                act.Should().Throw <ArgumentException>().WithMessage("*not supported*");
            }
        }
Example #16
0
        public void Redis_ValueConverter_CacheTypeConversion_Poco()
        {
            var cache = TestManagers.CreateRedisCache <Poco>(17, false, Serializer.Json);

            // act/assert
            using (cache)
            {
                var key   = Guid.NewGuid().ToString();
                var value = new Poco()
                {
                    Id = 23, Something = "§asdad"
                };
                cache.Add(key, value);
                var result = (Poco)cache.Get(key);
                value.ShouldBeEquivalentTo(result);
            }
        }
Example #17
0
        public void ProtoBufSerializer_FullAddGet()
        {
            using (var cache = TestManagers.CreateRedisCache(serializer: Serializer.Proto))
            {
                // arrange
                var key   = Guid.NewGuid().ToString();
                var pocco = SerializerPoccoSerializable.Create();

                // act
                Action actSet = () =>
                {
                    cache.Add(key, pocco);
                };

                // assert
                actSet.ShouldNotThrow();
                cache.Get <SerializerPoccoSerializable>(key).ShouldBeEquivalentTo(pocco);
            }
        }
Example #18
0
        ////[Trait("category", "Unreliable")]
        public void Redis_Multiple_PubSub_Change()
        {
            // arrange
            string fileName    = BaseCacheManagerTest.GetCfgFileName(@"/Configuration/configuration.valid.allFeatures.config");
            var    channelName = Guid.NewGuid().ToString();

            // redis config name must be same for all cache handles, configured via file and via code
            // otherwise the pub sub channel name is different
            string cacheName = "redisConfig";

            RedisConfigurations.LoadConfiguration(fileName, RedisConfigurationSection.DefaultSectionName);

            var cfg = ConfigurationBuilder.LoadConfigurationFile(fileName, cacheName);

            cfg.BackPlateChannelName = channelName;

            var cfgCache = CacheFactory.FromConfiguration <object>(cfg);

            var item = new CacheItem <object>(Guid.NewGuid().ToString(), "something");

            // act/assert
            RedisTests.RunMultipleCaches(
                (cacheA, cacheB) =>
            {
                cacheA.Put(item);
                Thread.Sleep(10);
                var value = cacheB.Get(item.Key);
                value.Should().Be(item.Value, cacheB.ToString());
                cacheB.Put(item.Key, "new value");
            },
                (cache) =>
            {
                Thread.Sleep(12);
                var value = cache.Get(item.Key);
                value.Should().Be("new value", cache.ToString());
            },
                2,
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(69, true, channelName),
                cfgCache,
                TestManagers.CreateRedisCache(69),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(69, true, channelName));
        }
Example #19
0
        public void Redis_Multiple_PubSub_ClearRegion()
        {
            // arrange
            var item = new CacheItem <object>(Guid.NewGuid().ToString(), "something", Guid.NewGuid().ToString());

            // act/assert
            RedisTests.RunMultipleCaches(
                (cacheA, cacheB) =>
            {
                cacheA.Add(item);
                cacheB.Get(item.Key, item.Region).Should().Be(item.Value);
                cacheB.ClearRegion(item.Region);
            },
                (cache) =>
            {
                cache.Get(item.Key, item.Region).Should().BeNull();
            }, 10,
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(5),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(5),
                TestManagers.CreateRedisCache(5),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(5));
        }
Example #20
0
        public void Redis_Absolute_DoesExpire()
        {
            // arrange
            var item  = new CacheItem <object>(Guid.NewGuid().ToString(), "something", ExpirationMode.Absolute, TimeSpan.FromMilliseconds(150));
            var cache = TestManagers.CreateRedisCache(1);

            // act/assert
            using (cache)
            {
                // act
                var result = cache.Add(item);

                // assert
                result.Should().BeTrue();
                Thread.Sleep(30);
                var value = cache.GetCacheItem(item.Key);
                value.Should().NotBeNull();

                Thread.Sleep(150);
                var valueExpired = cache.GetCacheItem(item.Key);
                valueExpired.Should().BeNull();
            }
        }
        public async Task Redis_Multiple_PubSub_Change()
        {
            // arrange
            var channelName = Guid.NewGuid().ToString();
            var item        = new CacheItem <object>(Guid.NewGuid().ToString(), "something");

            // act/assert
            await RunMultipleCaches(
                async (cacheA, cacheB) =>
            {
                cacheA.Put(item);
                cacheA.Get(item.Key).Should().Be("something");
                await Task.Delay(10);
                var value = cacheB.Get(item.Key);
                value.Should().Be(item.Value, cacheB.ToString());
                cacheB.Put(item.Key, "new value");
            },
                async (cache) =>
            {
                int tries    = 0;
                object value = null;
                do
                {
                    tries++;
                    await Task.Delay(100);
                    value = cache.Get(item.Key);
                }while (value.ToString() != "new value" && tries < 10);

                value.Should().Be("new value", cache.ToString());
            },
                1,
                TestManagers.CreateRedisAndDicCacheWithBackplane(113, true, channelName, Serializer.Json),
                TestManagers.CreateRedisAndDicCacheWithBackplane(113, true, channelName, Serializer.Json),
                TestManagers.CreateRedisCache(113, false, Serializer.Json),
                TestManagers.CreateRedisAndDicCacheWithBackplane(113, true, channelName, Serializer.Json));
        }
Example #22
0
        public void Redis_Multiple_PubSub_Clear()
        {
            // arrange
            var item        = new CacheItem <object>(Guid.NewGuid().ToString(), "something");
            var channelName = Guid.NewGuid().ToString();

            // act/assert
            RedisTests.RunMultipleCaches(
                (cacheA, cacheB) =>
            {
                cacheA.Add(item);
                cacheB.Get(item.Key).Should().Be(item.Value);
                cacheB.Clear();
            },
                (cache) =>
            {
                cache.Get(item.Key).Should().BeNull();
            },
                2,
                TestManagers.CreateRedisAndDicCacheWithBackplane(444, true, channelName),
                TestManagers.CreateRedisAndDicCacheWithBackplane(444, true, channelName),
                TestManagers.CreateRedisCache(444),
                TestManagers.CreateRedisAndDicCacheWithBackplane(444, true, channelName));
        }