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_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_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 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_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_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();
            cache.Set("MultiThreadedValue", 12345);
            var values = new HashSet<long>();

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

            Assert.Equal(100, values.Count);
            var sorted = new long[100];
            values.CopyTo(sorted);
            Array.Sort(sorted);
            for (var i = 0; i < sorted.Length; i++)
                Assert.Equal(i + 1 + 12345, sorted[i]);
        }
        public void DistributedCacheEmulator_Increment_WorksMultiThreaded()
        {
            var cache = new DistributedCacheEmulator();

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

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

            Assert.Equal(100, values.Count);
            var sorted = new long[100];

            values.CopyTo(sorted);
            Array.Sort(sorted);
            for (var i = 0; i < sorted.Length; i++)
            {
                Assert.Equal(i + 1 + 12345, sorted[i]);
            }
        }