コード例 #1
0
        public void WhenActiveCacheItemRemovedThenGetOrCreateTriggersUpdate()
        {
            // arrange
            var expectedResult   = "expectedResult";
            var unexpectedResult = "unexpectedResult";
            var cacheKey         = "key";
            var triggerUpdate    = false;

            using (var cache = new SaintModeCache())
            {
                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration
                };
                cache.GetOrCreate(cacheKey, (key, cancel) => unexpectedResult, policy);
                cache.Remove(cacheKey);
                var ewh = new EventWaitHandle(false, EventResetMode.ManualReset);

                // act
                cache.GetOrCreate(cacheKey, (key, cancel) =>
                {
                    triggerUpdate = true;
                    ewh.Set();
                    return(expectedResult);
                }, policy);

                // assert
                ewh.WaitOne(5000);
                Assert.That(triggerUpdate, Is.True);
            }
        }
コード例 #2
0
        public void WhenCacheIsEmptyAndCacheItemPolicyOverrideOnUpdateWithNullThenUseDefaultPolicy()
        {
            // arrange
            var expectedResult = "expectedResult";
            var cacheKey       = "key";
            var defaultTimeout = 1;
            var defaultPolicy  = new CacheItemPolicy
            {
                AbsoluteExpiration = DateTime.UtcNow.AddSeconds(defaultTimeout)
            };

            using (var cache = new SaintModeCache())
            {
                var startTests = new EventWaitHandle(false, EventResetMode.ManualReset);

                // act
                cache.GetOrCreate(cacheKey, (key, cancel) =>
                {
                    cancel.CacheItemPolicy = null;
                    startTests.Set();
                    return(expectedResult);
                }, defaultPolicy);

                // assert
                startTests.WaitOne();
                Thread.Sleep(defaultTimeout * 1000);
                Assert.That(cache.Expired(cacheKey), Is.True);
            }
        }
コード例 #3
0
        public void WhenStaleCacheItemThenGetOrCreateAfterAnUpdateCompletesReturnsUpdatedItem()
        {
            // arrange
            var expectedResult   = "expectedResult";
            var unexpectedResult = "unexpectedResult";
            var cacheKey         = "key";

            using (var cache = new SaintModeCache())
            {
                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = DateTime.UtcNow.AddMilliseconds(10)
                };
                cache.GetOrCreate(cacheKey, (key, cancel) => unexpectedResult, policy);
                Thread.Sleep(20);
                var startTests = new EventWaitHandle(false, EventResetMode.ManualReset);
                cache.GetOrCreate(cacheKey, (key, cancel) =>
                {
                    startTests.Set();
                    return(expectedResult);
                }, ObjectCache.InfiniteAbsoluteExpiration);
                startTests.WaitOne();
                Thread.Sleep(20);

                // act
                var cacheValue = cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, policy);

                // assert
                Assert.That(cacheValue, Is.EqualTo(expectedResult));
            }
        }
コード例 #4
0
        public void WhenStaleCacheItemThenGetOrCreateReturnsCachedValueAndTriggersUpdate()
        {
            // arrange
            var expectedResult   = "expectedResult";
            var unexpectedResult = "unexpectedResult";
            var cacheKey         = "key";
            var triggerUpdate    = false;

            using (var cache = new SaintModeCache())
            {
                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = DateTime.UtcNow.AddMilliseconds(10)
                };
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, policy);
                Thread.Sleep(20);
                var startTests = new EventWaitHandle(false, EventResetMode.ManualReset);

                // act
                var cacheValue = cache.GetOrCreate(cacheKey, (key, cancel) =>
                {
                    triggerUpdate = true;
                    startTests.Set();
                    return(unexpectedResult);
                }, policy);

                // assert
                Assert.That(cacheValue, Is.EqualTo(expectedResult));
                startTests.WaitOne();
                Assert.That(triggerUpdate, Is.True);
            }
        }
コード例 #5
0
        public void WhenStaleCacheItemExistsAndCancellationTokenUsedThenReturnStaleItem()
        {
            // arrange
            var expectedResult   = "expectedResult";
            var unexpectedResult = "unexpectedResult";
            var cacheKey         = "key";

            using (var cache = new SaintModeCache())
            {
                var startTests = new EventWaitHandle(false, EventResetMode.ManualReset);
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, DateTime.UtcNow.AddMilliseconds(10));
                Thread.Sleep(20);

                // act
                var cacheValue = cache.GetOrCreate(cacheKey, (key, cancel) =>
                {
                    cancel.IsCancellationRequested = true;
                    startTests.Set();
                    return(unexpectedResult);
                });

                // assert
                startTests.WaitOne();
                Thread.Sleep(20);
                Assert.That(cacheValue, Is.EqualTo(expectedResult));
            }
        }
コード例 #6
0
        public void WhenEmptyCacheAndMultipleThreadsUpdateValueThenOnlyOneTheadUpdatesValue()
        {
            // arrange
            var expectedResult = "expectedResult";
            var cacheKey       = "key";

            using (var cache = new SaintModeCache())
            {
                var counter      = 0;
                var startTests   = new EventWaitHandle(false, EventResetMode.ManualReset);
                var waitForTests = new EventWaitHandle(false, EventResetMode.ManualReset);

                // act //assert
                for (var i = 0; i < 1000; i++)
                {
                    Func <object> testCache = () =>
                    {
                        startTests.WaitOne();
                        return(cache.GetOrCreate(cacheKey, (key, cancel) =>
                        {
                            Interlocked.Increment(ref counter);
                            return expectedResult;
                        }));
                    };

                    testCache.BeginInvoke(re =>
                    {
                        var result = testCache.EndInvoke(re);
                        try
                        {
                            Assert.That(result, Is.EqualTo(expectedResult));
                            Assert.That(counter, Is.LessThanOrEqualTo(1));
                        }
                        catch
                        {
                            waitForTests.Set();
                            throw;
                        }

                        if ((int)re.AsyncState == 999)
                        {
                            waitForTests.Set();
                        }
                    }, i);
                }

                startTests.Set();
                waitForTests.WaitOne();

                Assert.That(counter, Is.EqualTo(1));
            }
        }
コード例 #7
0
        public void WhenEmptyCacheThenGetWithoutCreateOrNullReturnsNull()
        {
            // arrange
            var cacheKey = "key";

            using (var cache = new SaintModeCache())
            {
                // act
                var result = cache.GetWithoutCreateOrNull(cacheKey);

                // assert
                Assert.That(result, Is.Null);
            }
        }
コード例 #8
0
        public void WhenEmptyCacheThenGetOrCreateReturnsUpdateItem()
        {
            // arrange
            var expectedResult = "value";
            var cacheKey       = "key";

            using (var cache = new SaintModeCache())
            {
                // act
                var cacheValue = cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult,
                                                   ObjectCache.InfiniteAbsoluteExpiration);

                // assert
                Assert.That(cacheValue, Is.EqualTo(expectedResult));
            }
        }
コード例 #9
0
        public void WhenCustomCacheIsUsedThenCacheItemsAreAddedToCustomCache()
        {
            // arrange
            var expectedResult = "value";
            var cacheKey       = "key";
            var memoryCache    = new MemoryCache("CustomCache", null);

            using (var cache = new SaintModeCache(memoryCache))
            {
                // act
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, ObjectCache.InfiniteAbsoluteExpiration);

                // assert
                Assert.That(memoryCache[cacheKey], Is.EqualTo(expectedResult));
            }
        }
コード例 #10
0
        public void WhenInfiniteAbsoluteExpirationCacheItemThenItemIsNotStale()
        {
            // arrange
            var expectedResult = "expectedResult";
            var cacheKey       = "key";

            using (var cache = new SaintModeCache())
            {
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, ObjectCache.InfiniteAbsoluteExpiration);

                // act
                var result = cache.Stale(cacheKey);

                // assert
                Assert.That(result, Is.False);
            }
        }
コード例 #11
0
        public void WhenActiveCacheItemThenGetWithoutCreateOrNullReturnsItem()
        {
            // arrange
            var expectedResult = "expectedResult";
            var cacheKey       = "key";

            using (var cache = new SaintModeCache())
            {
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, ObjectCache.InfiniteAbsoluteExpiration);

                // act
                var result = cache.GetWithoutCreateOrNull(cacheKey);

                // assert
                Assert.That(result, Is.EqualTo(expectedResult));
            }
        }
コード例 #12
0
        public void WhenActiveCacheItemThenGetWithoutCreateOrNullKeyIsCaseSensitive()
        {
            // arrange
            var expectedResult1 = "expectedResult1";
            var cacheKey1       = "key";
            var cacheKey2       = "KEY";

            using (var cache = new SaintModeCache())
            {
                cache.SetOrUpdateWithoutCreate(cacheKey1, expectedResult1, ObjectCache.InfiniteAbsoluteExpiration);

                // act
                var result = cache.GetWithoutCreateOrNull(cacheKey2);

                // assert
                Assert.That(result, Is.Null);
            }
        }
コード例 #13
0
        public void WhenGetOrCreateWithActiveCacheItemThenNoUpdateToCache()
        {
            // arrange
            var expectedResult   = "expectedResult";
            var unexpectedResult = "unexpectedResult";
            var cacheKey         = "key";

            using (var cache = new SaintModeCache())
            {
                cache.SetOrUpdateWithoutCreate(cacheKey, expectedResult, ObjectCache.InfiniteAbsoluteExpiration);

                // act
                var result = cache.GetOrCreate(cacheKey, (key, cancel) => unexpectedResult,
                                               ObjectCache.InfiniteAbsoluteExpiration);

                // assert
                Assert.That(result, Is.EqualTo(expectedResult));
            }
        }
コード例 #14
0
        public void WhenActiveCacheItemThenIsNotExpired()
        {
            // arrange
            var expectedResult = "expectedResult";
            var cacheKey       = "key";

            using (var cache = new SaintModeCache())
            {
                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration
                };
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, policy);

                // act
                var result = cache.Expired(cacheKey);

                // assert
                Assert.That(result, Is.False);
            }
        }
コード例 #15
0
        public void WhenStaleCacheItemThenItemIsStale()
        {
            // arrange
            var expectedResult = "expectedResult";
            var cacheKey       = "key";

            using (var cache = new SaintModeCache())
            {
                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = DateTime.UtcNow.AddMilliseconds(10)
                };
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, policy);
                Thread.Sleep(20);

                // act
                var result = cache.Stale(cacheKey);

                // assert
                Assert.That(result, Is.True);
            }
        }
コード例 #16
0
        public void WhenSetOrUpdateWithoutCreateThenGetWithoutCreateOrNullKeyIsCaseSensitive()
        {
            // arrange
            var expectedResult1 = "expectedResult1";
            var expectedResult2 = "expectedResult2";
            var cacheKey1       = "key";
            var cacheKey2       = "KEY";

            using (var cache = new SaintModeCache())
            {
                cache.SetOrUpdateWithoutCreate(cacheKey1, expectedResult1);
                cache.SetOrUpdateWithoutCreate(cacheKey2, expectedResult2);

                // act
                var result1 = cache.GetWithoutCreateOrNull(cacheKey1);
                var result2 = cache.GetWithoutCreateOrNull(cacheKey2);

                // assert
                Assert.That(result1, Is.EqualTo(expectedResult1));
                Assert.That(result2, Is.EqualTo(expectedResult2));
            }
        }
コード例 #17
0
        public void WhenInfiniteAbsoluteExpirationCacheItemThenGetOrCreateUpdatesCachePolicy()
        {
            // arrange
            var expectedResult   = "expectedResult";
            var unexpectedResult = "unexpectedResult";
            var cacheKey         = "key";

            using (var cache = new SaintModeCache())
            {
                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = DateTime.UtcNow.AddMilliseconds(10)
                };
                cache.GetOrCreate(cacheKey, (key, cancel) => unexpectedResult, ObjectCache.InfiniteAbsoluteExpiration);
                cache.SetOrUpdateWithoutCreate(cacheKey, expectedResult, policy);
                Thread.Sleep(20);

                // act
                var result = cache.Stale(cacheKey);

                // assert
                Assert.That(result, Is.True);
            }
        }
コード例 #18
0
        public void WhenCacheIsEmptyAndCancellationTokenUsedOnUpdateThenDontCacheAndReturnNull()
        {
            // arrange
            var unexpectedResult = "unexpectedResult";
            var cacheKey         = "key";

            using (var cache = new SaintModeCache())
            {
                var startTests = new EventWaitHandle(false, EventResetMode.ManualReset);

                // act
                var cacheValue = cache.GetOrCreate(cacheKey, (key, cancel) =>
                {
                    cancel.IsCancellationRequested = true;
                    startTests.Set();
                    return(unexpectedResult);
                });

                // assert
                startTests.WaitOne();
                Thread.Sleep(20);
                Assert.That(cacheValue, Is.Null);
            }
        }
コード例 #19
0
        public void WhenStaleCacheAndMultipleThreadsGetValueThenOnlyOneThreadUpdatesValue()
        {
            // arrange
            var expectedResult = "expectedResult";
            var cacheKey       = "key";

            using (var cache = new SaintModeCache())
            {
                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = DateTime.UtcNow.AddMilliseconds(10)
                };
                cache.GetOrCreate(cacheKey, (key, cancel) => expectedResult, policy);
                Thread.Sleep(200);
                var counter       = 0;
                var startTests    = new EventWaitHandle(false, EventResetMode.ManualReset);
                var waitForTests  = new EventWaitHandle(false, EventResetMode.ManualReset);
                var waitForResult = new EventWaitHandle(false, EventResetMode.ManualReset);
                var triggerUpdate = false;

                // act //assert
                for (var i = 0; i < 1000; i++)
                {
                    Func <object> testCache = () =>
                    {
                        startTests.WaitOne();
                        return(cache.GetOrCreate(cacheKey, (key, cancel) =>
                        {
                            triggerUpdate = true;
                            Interlocked.Increment(ref counter);
                            waitForResult.Set();
                            return expectedResult;
                        }, policy));
                    };

                    testCache.BeginInvoke(re =>
                    {
                        var result = testCache.EndInvoke(re);
                        try
                        {
                            Assert.That(result, Is.EqualTo(expectedResult));
                            Assert.That(counter, Is.LessThanOrEqualTo(1));
                        }
                        catch
                        {
                            waitForTests.Set();
                            waitForResult.Set();
                            throw;
                        }

                        if ((int)re.AsyncState != 999)
                        {
                            return;
                        }

                        waitForTests.Set();
                    }, i);
                }

                startTests.Set();
                waitForTests.WaitOne();
                waitForResult.WaitOne();

                Assert.That(triggerUpdate, Is.True);
            }
        }