示例#1
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.CreateRedisAndSystemCacheWithBackPlate(444, true, channelName),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(444, true, channelName),
                TestManagers.CreateRedisCache(444),
                TestManagers.CreateRedisAndSystemCacheWithBackPlate(444, true, channelName));
        }
示例#2
0
        ////[Trait("category", "Unreliable")]
        public void Redis_Multiple_PubSub_ClearRegion()
        {
            // arrange
            var item        = new CacheItem <object>(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "something");
            var channelName = 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();
            },
                2,
                TestManagers.CreateRedisCache(5),
                TestManagers.CreateRedisCache(5),
                TestManagers.CreateRedisCache(5),
                TestManagers.CreateRedisCache(5));
        }
        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));
        }
示例#4
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");
        }