Example #1
0
        public async Task DataInMemoryCacheButNotFresh()
        {
            CachedItemWrapper <string> memoryCacheItem = new CachedItemWrapper <string>("fromMemDistCache", new DateTimeOffset(2020, 05, 17, 19, 59, 59, 00, new TimeSpan(0, 0, 0)));

            _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((true, memoryCacheItem));

            _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0)));

            MemDistCacheFactory <string> memDistCacheFactory = new MemDistCacheFactory <string>(_pollySyncCacheProvider.Object, _distributedCacheWrapper.Object, _mockableDateTime.Object, _logger.Object);
            IMemDistCache <string>       memDistCache        = memDistCacheFactory.GetCache(_defaultCacheDuration, Cache.ResetTimeFactory.OnHour);

            string result = await memDistCache.GetCachedDataAsync(_dataGetterDelegate1, _key, RefreshBehaviour.DontWaitForFreshData, CancellationToken.None);

            Assert.AreEqual("fromMemDistCache", result);

            _pollySyncCacheProvider.Verify(x => x.TryGet(It.Is <string>(y => y == _key)), Times.Once);

            _distributedCacheWrapper.Verify(x => x.TryGetAsync <string>(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Never);

            await Task.Delay(_waitForBackgroundThreadToCompleteMs); // wait for background thread

            Assert.AreEqual(1, _numberOfTimesDataGetterDelegate1Called);

            DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0));

            _pollySyncCacheProvider.Verify(x => x.Put(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once);

            _distributedCacheWrapper.Verify(x => x.PutAsync(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Once);
        }
Example #2
0
        public async Task RefreshData()
        {
            _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0)));

            MemDistCacheFactory <string> memDistCacheFactory = new MemDistCacheFactory <string>(_pollySyncCacheProvider.Object, _distributedCacheWrapper.Object, _mockableDateTime.Object, _logger.Object);
            IMemDistCache <string>       memDistCache        = memDistCacheFactory.GetCache(_defaultCacheDuration, Cache.ResetTimeFactory.OnHour);

            string result = await memDistCache.RefreshDataAsync(_dataGetterDelegate1, _key, CancellationToken.None);

            Assert.AreEqual("dataFromBackendGet", result);

            Assert.AreEqual(1, _numberOfTimesDataGetterDelegate1Called);

            DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0));

            _pollySyncCacheProvider.Verify(x => x.Put(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once);

            _distributedCacheWrapper.Verify(x => x.PutAsync(It.Is <string>(y => y == _key), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Once);
        }
Example #3
0
        public async Task DataInMemoryCacheAndFresh()
        {
            CachedItemWrapper <string> memoryCacheItem = new CachedItemWrapper <string>("fromMemDistCache", new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0)));

            _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((true, memoryCacheItem));

            _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0)));

            MemDistCacheFactory <string> memDistCacheFactory = new MemDistCacheFactory <string>(_pollySyncCacheProvider.Object, _distributedCacheWrapper.Object, _mockableDateTime.Object, _logger.Object);
            IMemDistCache <string>       memDistCache        = memDistCacheFactory.GetCache(_defaultCacheDuration, Cache.ResetTimeFactory.OnHour);

            string result = await memDistCache.GetCachedDataAsync(_dataGetterDelegate1, _key, RefreshBehaviour.DontWaitForFreshData, CancellationToken.None);


            Assert.AreEqual("fromMemDistCache", result);
            Assert.AreEqual(0, _numberOfTimesDataGetterDelegate1Called);

            _pollySyncCacheProvider.Verify(x => x.TryGet(It.Is <string>(y => y == _key)), Times.Once);
            _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.IsAny <CachedItemWrapper <string> >(), It.IsAny <Ttl>()), Times.Never);

            _distributedCacheWrapper.Verify(x => x.TryGetAsync <string>(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Never);
            _distributedCacheWrapper.Verify(x => x.PutAsync(It.IsAny <string>(), It.IsAny <CachedItemWrapper <string> >(), It.IsAny <Ttl>(), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Never);
        }
Example #4
0
        public async Task MultipleConcurrentRequests()
        {
            int numberOfTimesDataGetterDelegate1Called = 0;
            Func <CancellationToken, Task <string> > dataGetterDelegate1 = async(token) =>
            {
                Interlocked.Increment(ref numberOfTimesDataGetterDelegate1Called);
                await Task.Delay(1000);

                return(await Task.FromResult("dataFromBackendGet1"));
            };

            int numberOfTimesDataGetterDelegate2Called = 0;
            Func <CancellationToken, Task <string> > dataGetterDelegate2 = async(token) =>
            {
                Interlocked.Increment(ref numberOfTimesDataGetterDelegate2Called);
                await Task.Delay(1000);

                return(await Task.FromResult("dataFromBackendGet2"));
            };


            _pollySyncCacheProvider.Setup(x => x.TryGet(It.IsAny <string>())).Returns((false, null));

            _mockableDateTime.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 05, 17, 20, 00, 00, 00, new TimeSpan(0, 0, 0)));

            MemDistCacheFactory <string> memDistCacheFactory = new MemDistCacheFactory <string>(_pollySyncCacheProvider.Object, _distributedCacheWrapper.Object, _mockableDateTime.Object, _logger.Object);
            IMemDistCache <string>       memDistCache        = memDistCacheFactory.GetCache(_defaultCacheDuration, Cache.ResetTimeFactory.OnHour);

            ConcurrentBag <Task <string> > results1 = new ConcurrentBag <Task <string> >();
            ConcurrentBag <Task <string> > results2 = new ConcurrentBag <Task <string> >();

            Stopwatch stopwatch = Stopwatch.StartNew();

            Task task1 = Task.Factory.StartNew(() =>
            {
                Parallel.For(0, 50, i =>
                {
                    Task <string> result = memDistCache.GetCachedDataAsync(dataGetterDelegate1, "key1", RefreshBehaviour.WaitForFreshData, CancellationToken.None);
                    results1.Add(result);
                });
            });


            Task task2 = Task.Factory.StartNew(() =>
            {
                Parallel.For(0, 50, i =>
                {
                    Task <string> result = memDistCache.GetCachedDataAsync(dataGetterDelegate2, "key2", RefreshBehaviour.WaitForFreshData, CancellationToken.None);
                    results2.Add(result);
                });
            });


            Task.WaitAll(task1, task2);

            await Task.WhenAll(results1);

            await Task.WhenAll(results2);

            stopwatch.Stop();

            Assert.Less(stopwatch.ElapsedMilliseconds, 2000); // shows the calls were processed concurrently

            foreach (Task <string> result in results1)
            {
                Assert.AreEqual("dataFromBackendGet1", await result);
            }
            foreach (Task <string> result in results2)
            {
                Assert.AreEqual("dataFromBackendGet2", await result);
            }

            Assert.AreEqual(1, numberOfTimesDataGetterDelegate1Called);
            Assert.AreEqual(1, numberOfTimesDataGetterDelegate2Called);

            await Task.Delay(_waitForBackgroundThreadToCompleteMs); // wait for background thread



            DateTimeOffset whenDataWillNotBeFresh = new DateTimeOffset(2020, 05, 17, 21, 00, 00, 00, new TimeSpan(0, 0, 0));

            _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet1" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once);

            _distributedCacheWrapper.Verify(x => x.PutAsync(It.IsAny <string>(), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet1" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Once);

            _pollySyncCacheProvider.Verify(x => x.Put(It.IsAny <string>(), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet2" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration)), Times.Once);

            _distributedCacheWrapper.Verify(x => x.PutAsync(It.IsAny <string>(), It.Is <CachedItemWrapper <string> >(y => y.Content == "dataFromBackendGet2" && y.IsFreshUntil == whenDataWillNotBeFresh), It.Is <Ttl>(y => y.Timespan == _defaultCacheDuration), It.IsAny <CancellationToken>(), It.IsAny <bool>()), Times.Once);
        }