Exemplo n.º 1
0
        public async Task Redis_Sliding_DoesExpire_MultiClients()
        {
            // arrange
            var item        = new CacheItem <object>(Guid.NewGuid().ToString(), "something", ExpirationMode.Sliding, TimeSpan.FromMilliseconds(100));
            var channelName = Guid.NewGuid().ToString();
            var cacheA      = TestManagers.CreateRedisAndDicCacheWithBackplane(10, false, channelName);
            var cacheB      = TestManagers.CreateRedisAndDicCacheWithBackplane(10, false, channelName);

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

                    var valueB = cacheB.Get(item.Key);

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

                    for (int s = 0; s < 3; s++)
                    {
                        await Task.Delay(50);

                        cacheA.GetCacheItem(item.Key).Should().NotBeNull();
                        cacheB.GetCacheItem(item.Key).Should().NotBeNull();
                    }

                    await Task.Delay(250);

                    cacheA.GetCacheItem(item.Key).Should().BeNull();
                    cacheB.GetCacheItem(item.Key).Should().BeNull();
                }
        }
            public async Task Redis_Sliding_DoesExpire_MultiClients()
            {
                // arrange
                var timeout     = 50;
                var channelName = Guid.NewGuid().ToString();
                var cacheA      = TestManagers.CreateRedisAndDicCacheWithBackplane(10, false, channelName);
                var cacheB      = TestManagers.CreateRedisAndDicCacheWithBackplane(10, false, channelName);

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

                            return(a);
                        });
                    }
            }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public void Redis_Sliding_DoesExpire_MultiClients()
        {
            // arrange
            var item        = new CacheItem <object>(Guid.NewGuid().ToString(), "something", ExpirationMode.Sliding, TimeSpan.FromMilliseconds(100));
            var channelName = Guid.NewGuid().ToString();
            var cacheA      = TestManagers.CreateRedisAndDicCacheWithBackplane(10, true, channelName);
            var cacheB      = TestManagers.CreateRedisAndDicCacheWithBackplane(10, true, channelName);

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

                    var valueB = cacheB.Get(item.Key);

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

                    // 450ms added so absolute would be expired on the 2nd go
                    for (int s = 0; s < 3; s++)
                    {
                        Thread.Sleep(80);
                        cacheA.GetCacheItem(item.Key).Should().NotBeNull();
                        cacheB.GetCacheItem(item.Key).Should().NotBeNull();
                    }

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

            // act/assert
            await RedisTests.RunMultipleCaches(
                async (cacheA, cacheB) =>
            {
                cacheA.Add(item);
                cacheB.Get(item.Key).Should().Be(item.Value);
                cacheB.Clear();
                await Task.Delay(0);
            },
                async (cache) =>
            {
                cache.Get(item.Key).Should().BeNull();
                await Task.Delay(0);
            },
                2,
                TestManagers.CreateRedisAndDicCacheWithBackplane(444, true, channelName),
                TestManagers.CreateRedisAndDicCacheWithBackplane(444, true, channelName),
                TestManagers.CreateRedisCache(444),
                TestManagers.CreateRedisAndDicCacheWithBackplane(444, true, channelName));
        }
Exemplo n.º 6
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));
        }
        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));
        }
Exemplo n.º 8
0
        private static void TestBackplaneEvent <TEventArgs>(
            CacheEvent cacheEvent,
            Action <ICacheManager <object> > arrange,
            Action <ICacheManager <object>, TEventArgs> assertLocal,
            Action <ICacheManager <object>, TEventArgs> assertRemote)
            where TEventArgs : EventArgs
        {
            var       channelName          = Guid.NewGuid().ToString();
            var       cacheA               = TestManagers.CreateRedisAndDicCacheWithBackplane(1, false, channelName);
            var       cacheB               = TestManagers.CreateRedisAndDicCacheWithBackplane(1, false, channelName);
            var       eventTriggeredLocal  = 0;
            var       eventTriggeredRemote = 0;
            Exception lastError            = null;

            Action <EventArgs> testLocal = (args) =>
            {
                try
                {
                    assertLocal(cacheA, (TEventArgs)args);

                    Interlocked.Increment(ref eventTriggeredLocal);
                }
                catch (Exception ex)
                {
                    lastError = ex;
                    throw;
                }
            };

            Action <EventArgs> testRemote = (args) =>
            {
                try
                {
                    assertRemote(cacheB, (TEventArgs)args);

                    Interlocked.Increment(ref eventTriggeredRemote);
                }
                catch (Exception ex)
                {
                    lastError = ex;
                    throw;
                }
            };

            switch (cacheEvent)
            {
            case CacheEvent.OnAdd:
                cacheA.OnAdd += (ev, args) =>
                {
                    testLocal(args);
                };

                cacheB.OnAdd += (ev, args) =>
                {
                    testRemote(args);
                };
                break;

            case CacheEvent.OnClear:
                cacheA.OnClear += (ev, args) =>
                {
                    testLocal(args);
                };

                cacheB.OnClear += (ev, args) =>
                {
                    testRemote(args);
                };
                break;

            case CacheEvent.OnClearRegion:
                cacheA.OnClearRegion += (ev, args) =>
                {
                    testLocal(args);
                };

                cacheB.OnClearRegion += (ev, args) =>
                {
                    testRemote(args);
                };
                break;

            case CacheEvent.OnPut:
                cacheA.OnPut += (ev, args) =>
                {
                    testLocal(args);
                };

                cacheB.OnPut += (ev, args) =>
                {
                    testRemote(args);
                };
                break;

            case CacheEvent.OnRemove:
                cacheA.OnRemove += (ev, args) =>
                {
                    testLocal(args);
                };

                cacheB.OnRemove += (ev, args) =>
                {
                    testRemote(args);
                };
                break;

            case CacheEvent.OnUpdate:
                cacheA.OnUpdate += (ev, args) =>
                {
                    testLocal(args);
                };

                cacheB.OnUpdate += (ev, args) =>
                {
                    testRemote(args);
                };
                break;
            }

            arrange(cacheA);

            Func <int, Func <bool>, bool> waitForIt = (tries, act) =>
            {
                var i      = 0;
                var result = false;
                while (!result && i < tries)
                {
                    i++;
                    result = act();
                    if (result)
                    {
                        return(true);
                    }

                    Thread.Sleep(100);
                }

                return(false);
            };

            Func <Exception, string> formatError = (err) =>
            {
                var xunitError = err as XunitException;
                if (xunitError != null)
                {
                    return(xunitError.Message);
                }

                return(err?.ToString());
            };

            var triggerResult = waitForIt(100, () => eventTriggeredRemote == 1);

            lastError.Should().BeNull(formatError(lastError));
            triggerResult.Should().BeTrue("Event should get triggered through the backplane.");
            eventTriggeredLocal.Should().Be(1, "Local cache event should be triggered one time");
        }