Exemplo n.º 1
0
        public object Remove(string key)
        {
            var old = cache.Get <object>(key);

            cache.Set <object>(key, null);
            return(old);
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksProperly()
        {
            using (new MunqContext())
            {
                var registrar = Dependency.Resolve <IDependencyRegistrar>();

                DateTimeProvider.StaticProvider = null;
                DateTime now      = DateTime.Now;
                var      dateTime = A.Fake <IDateTimeProvider>();
                A.CallTo(() => dateTime.Now).ReturnsLazily(() => now);

                registrar.RegisterInstance(dateTime);
                var cache = new DistributedCacheEmulator();

                cache.Set("SomeInt", 13579, TimeSpan.FromSeconds(5));
                var actualInt = cache.Get <int>("SomeInt");
                Assert.Equal(13579, actualInt);
                now = now.AddSeconds(1);

                var notExpiredInt = cache.Get <int>("SomeInt");
                Assert.Equal(13579, notExpiredInt);

                now = now.AddSeconds(6);

                var expiredInt = cache.Get <int>("SomeInt");
                Assert.Equal(0, expiredInt);
            }
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();

            var threads = new int[100];

            for (var i = 0; i < threads.Length; i++)
            {
                threads[i] = i;
            }

            Parallel.ForEach(threads, x =>
            {
                cache.Set("MultiThreadedValue" + x, x * 7, TimeSpan.FromMilliseconds(100));
            });

            Thread.Sleep(1);

            for (var i = 0; i < threads.Length; i++)
            {
                var actual = cache.Get <int>("MultiThreadedValue" + threads[i]);
                Assert.Equal(threads[i] * 7, actual);
            }

            Thread.Sleep(110);

            for (var i = 0; i < threads.Length; i++)
            {
                var actual = cache.Get <int>("MultiThreadedValue" + threads[i]);
                Assert.Equal(0, actual);
            }
        }
        public void DistributedCacheEmulator_Get_ReturnsDefaultIfValueNotInCache()
        {
            var cache = new DistributedCacheEmulator();

            var actualInt = cache.Get<int>("NonExistingInt");
            Assert.Equal(0, actualInt);

            var actualStr = cache.Get<string>("NonExistingString");
            Assert.Equal(null, actualStr);

            var actualBool = cache.Get<bool>("NonExistingBoolean");
            Assert.Equal(false, actualBool);
        }
        public void DistributedCacheEmulator_Get_ReturnsDefaultIfValueNotInCache()
        {
            var cache = new DistributedCacheEmulator();

            var actualInt = cache.Get <int>("NonExistingInt");

            Assert.Equal(0, actualInt);

            var actualStr = cache.Get <string>("NonExistingString");

            Assert.Equal(null, actualStr);

            var actualBool = cache.Get <bool>("NonExistingBoolean");

            Assert.Equal(false, actualBool);
        }
        public void DistributedCacheEmulator_Increment_SetsValueToOneIfKeyNotInCache()
        {
            var cache = new DistributedCacheEmulator();
            var actual = cache.Increment("NotExistingValue", 7);
            Assert.Equal(1, actual);

            var inCache = cache.Get<long>("NotExistingValue");
            Assert.Equal(1, inCache);
        }
        public void DistributedCacheEmulator_Set_WorksProperly()
        {
            var cache = new DistributedCacheEmulator();

            cache.Set("SomeInt", 13579);
            var actualInt = cache.Get <int>("SomeInt");

            Assert.Equal(13579, actualInt);

            cache.Set("SomeStr", "abc");
            var actualStr = cache.Get <string>("SomeStr");

            Assert.Equal("abc", actualStr);

            cache.Set("SomeBool", true);
            var actualBool = cache.Get <bool>("SomeBool");

            Assert.Equal(true, actualBool);
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksMultiThreaded()
        {
            using (new MunqContext())
            {
                DateTimeProvider.StaticProvider = null;
                var registrar = Dependency.Resolve <IDependencyRegistrar>();

                DateTime now      = DateTime.Now;
                var      dateTime = A.Fake <IDateTimeProvider>();
                A.CallTo(() => dateTime.Now).ReturnsLazily(() => now);

                registrar.RegisterInstance(dateTime);
                var cache = new DistributedCacheEmulator();

                var threads = new int[100];
                for (var i = 0; i < threads.Length; i++)
                {
                    threads[i] = i;
                }

                Parallel.ForEach(threads, x =>
                {
                    cache.Set("MultiThreadedValue" + x, x * 7, TimeSpan.FromSeconds(5));
                });

                now = now.AddSeconds(1);

                for (var i = 0; i < threads.Length; i++)
                {
                    var actual = cache.Get <int>("MultiThreadedValue" + threads[i]);
                    Assert.Equal(threads[i] * 7, actual);
                }

                now = now.AddSeconds(6);

                for (var i = 0; i < threads.Length; i++)
                {
                    var actual = cache.Get <int>("MultiThreadedValue" + threads[i]);
                    Assert.Equal(0, actual);
                }
            }
        }
        public void DistributedCacheEmulator_Increment_ProperlyIncrementsValueInCache()
        {
            var cache = new DistributedCacheEmulator();
            cache.Set("ExistingValue", 100);

            var actual = cache.Increment("ExistingValue", 3);
            Assert.Equal(103, actual);

            var inCache = cache.Get<long>("ExistingValue");
            Assert.Equal(103, inCache);
        }
        public void DistributedCacheEmulator_Increment_SetsValueToOneIfKeyNotInCache()
        {
            var cache  = new DistributedCacheEmulator();
            var actual = cache.Increment("NotExistingValue", 7);

            Assert.Equal(1, actual);

            var inCache = cache.Get <long>("NotExistingValue");

            Assert.Equal(1, inCache);
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksProperly()
        {
            var cache = new DistributedCacheEmulator();

            cache.Set("SomeInt", 13579, TimeSpan.FromMilliseconds(100));
            var actualInt = cache.Get <int>("SomeInt");

            Assert.Equal(13579, actualInt);

            Thread.Sleep(1);

            var notExpiredInt = cache.Get <int>("SomeInt");

            Assert.Equal(13579, notExpiredInt);

            Thread.Sleep(110);

            var expiredInt = cache.Get <int>("SomeInt");

            Assert.Equal(0, expiredInt);
        }
        public void DistributedCacheEmulator_Increment_ProperlyIncrementsValueInCache()
        {
            var cache = new DistributedCacheEmulator();

            cache.Set("ExistingValue", 100);

            var actual = cache.Increment("ExistingValue", 3);

            Assert.Equal(103, actual);

            var inCache = cache.Get <long>("ExistingValue");

            Assert.Equal(103, inCache);
        }
        public void DistributedCacheEmulator_Get_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();
            cache.Set("MultiThreadedValue", 12345);
            var values = new HashSet<int>();

            Parallel.ForEach(new int[100], x =>
            {
                var result = cache.Get<int>("MultiThreadedValue");
                lock (values)
                    values.Add(result);
            });

            Assert.Equal(12345, values.First());
        }
        public void DistributedCacheEmulator_Get_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();

            cache.Set("MultiThreadedValue", 12345);
            var values = new HashSet <int>();

            Parallel.ForEach(new int[100], x =>
            {
                var result = cache.Get <int>("MultiThreadedValue");
                lock (values)
                    values.Add(result);
            });

            Assert.Equal(12345, values.First());
        }
        public void DistributedCacheEmulator_Set_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();

            var threads = new int[100];

            for (var i = 0; i < threads.Length; i++)
            {
                threads[i] = i;
            }

            Parallel.ForEach(threads, x =>
            {
                cache.Set("MultiThreadedValue" + x, x * 7);
            });

            for (var i = 0; i < threads.Length; i++)
            {
                var actual = cache.Get <int>("MultiThreadedValue" + threads[i]);
                Assert.Equal(threads[i] * 7, actual);
            }
        }
        public void DistributedCacheEmulator_Reset_ClearsCacheItems()
        {
            var cache = new DistributedCacheEmulator();

            cache.Set("SomeInt", 1);
            cache.Set("SomeStr", "str");
            cache.Set("WithExpiration", true, TimeSpan.FromHours(10));

            Assert.Equal(1, cache.Get <int?>("SomeInt"));
            Assert.Equal("str", cache.Get <string>("SomeStr"));
            Assert.True(cache.Get <bool?>("WithExpiration"));

            cache.Reset();

            Assert.Null(cache.Get <int?>("SomeInt"));
            Assert.Null(cache.Get <string>("SomeStr"));
            Assert.Null(cache.Get <bool?>("WithExpiration"));
        }
        public void DistributedCacheEmulatorTests()
        {
            DependencyContext.Using(c =>
            {
                var dateTimeProvider = new FakeDateTimeProvider();
                var cacheEmulator = new DistributedCacheEmulator();
                var key = "test";
                var value = new object();
                var cacheTimeout = 30;
                dateTimeProvider.Now = new DateTime(2000, 1, 1, 0, 0, 0);

                A.CallTo(() => c.Resolver.TryResolve<IDateTimeProvider>())
                    .ReturnsLazily(() => dateTimeProvider);

                Assert.Null(cacheEmulator.Get<object>(key));

                cacheEmulator.Set(key, value, TimeSpan.FromSeconds(cacheTimeout));
                Assert.Equal(value, cacheEmulator.Get<object>(key));

                dateTimeProvider.Now = dateTimeProvider.Now.AddSeconds(60);
                Assert.Null(cacheEmulator.Get<object>(key));

                cacheEmulator.Set(key, value, TimeSpan.FromSeconds(cacheTimeout));
                Assert.Equal(value, cacheEmulator.Get<object>(key));

                cacheEmulator.Set(key, value);
                dateTimeProvider.Now = dateTimeProvider.Now.AddSeconds(60);
                Assert.Equal(value, cacheEmulator.Get<object>(key));

                cacheEmulator.Reset();
                Assert.Null(cacheEmulator.Get<object>(key));

                Assert.Equal(1, cacheEmulator.Increment(key));
                Assert.Equal(2, cacheEmulator.Increment(key));
                cacheEmulator.Reset();
                Assert.Equal(2, cacheEmulator.Increment(key, 2));
            });
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksProperly()
        {
            var cache = new DistributedCacheEmulator();

            cache.Set("SomeInt", 13579, TimeSpan.FromMilliseconds(100));
            var actualInt = cache.Get<int>("SomeInt");
            Assert.Equal(13579, actualInt);

            Thread.Sleep(1);

            var notExpiredInt = cache.Get<int>("SomeInt");
            Assert.Equal(13579, notExpiredInt);

            Thread.Sleep(110);

            var expiredInt = cache.Get<int>("SomeInt");
            Assert.Equal(0, expiredInt);
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksMultiThreaded()
        {
            using (new MunqContext())
            {
                DateTimeProvider.StaticProvider = null;
                var registrar = Dependency.Resolve<IDependencyRegistrar>();

                DateTime now = DateTime.Now;
                var dateTime = A.Fake<IDateTimeProvider>();
                A.CallTo(() => dateTime.Now).ReturnsLazily(() => now);

                registrar.RegisterInstance(dateTime);
                var cache = new DistributedCacheEmulator();

                var threads = new int[100];
                for (var i = 0; i < threads.Length; i++)
                    threads[i] = i;

                Parallel.ForEach(threads, x =>
                {
                    cache.Set("MultiThreadedValue" + x, x * 7, TimeSpan.FromSeconds(5));
                });

                now = now.AddSeconds(1);

                for (var i = 0; i < threads.Length; i++)
                {
                    var actual = cache.Get<int>("MultiThreadedValue" + threads[i]);
                    Assert.Equal(threads[i] * 7, actual);
                }

                now = now.AddSeconds(6);

                for (var i = 0; i < threads.Length; i++)
                {
                    var actual = cache.Get<int>("MultiThreadedValue" + threads[i]);
                    Assert.Equal(0, actual);
                }
            }
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksProperly()
        {
            using (new MunqContext())
            {
                var registrar = Dependency.Resolve<IDependencyRegistrar>();

                DateTimeProvider.StaticProvider = null;
                DateTime now = DateTime.Now;
                var dateTime = A.Fake<IDateTimeProvider>();
                A.CallTo(() => dateTime.Now).ReturnsLazily(() => now);

                registrar.RegisterInstance(dateTime);
                var cache = new DistributedCacheEmulator();

                cache.Set("SomeInt", 13579, TimeSpan.FromSeconds(5));
                var actualInt = cache.Get<int>("SomeInt");
                Assert.Equal(13579, actualInt);
                now = now.AddSeconds(1);

                var notExpiredInt = cache.Get<int>("SomeInt");
                Assert.Equal(13579, notExpiredInt);

                now = now.AddSeconds(6);

                var expiredInt = cache.Get<int>("SomeInt");
                Assert.Equal(0, expiredInt);
            }
        }
        public void DistributedCacheEmulator_Set_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();

            var threads = new int[100];
            for (var i = 0; i < threads.Length; i++)
                threads[i] = i;

            Parallel.ForEach(threads, x =>
            {
                cache.Set("MultiThreadedValue" + x, x * 7);
            });

            for (var i = 0; i < threads.Length; i++)
            {
                var actual = cache.Get<int>("MultiThreadedValue" + threads[i]);
                Assert.Equal(threads[i] * 7, actual);
            }
        }
        public void DistributedCacheEmulator_Set_WorksProperly()
        {
            var cache = new DistributedCacheEmulator();

            cache.Set("SomeInt", 13579);
            var actualInt = cache.Get<int>("SomeInt");
            Assert.Equal(13579, actualInt);

            cache.Set("SomeStr", "abc");
            var actualStr = cache.Get<string>("SomeStr");
            Assert.Equal("abc", actualStr);

            cache.Set("SomeBool", true);
            var actualBool = cache.Get<bool>("SomeBool");
            Assert.Equal(true, actualBool);
        }
        public void DistributedCacheEmulator_SetWithExpiration_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();

            var threads = new int[100];
            for (var i = 0; i < threads.Length; i++)
                threads[i] = i;

            Parallel.ForEach(threads, x =>
            {
                cache.Set("MultiThreadedValue" + x, x * 7, TimeSpan.FromMilliseconds(100));
            });

            Thread.Sleep(1);

            for (var i = 0; i < threads.Length; i++)
            {
                var actual = cache.Get<int>("MultiThreadedValue" + threads[i]);
                Assert.Equal(threads[i] * 7, actual);
            }

            Thread.Sleep(110);

            for (var i = 0; i < threads.Length; i++)
            {
                var actual = cache.Get<int>("MultiThreadedValue" + threads[i]);
                Assert.Equal(0, actual);
            }
        }