Beispiel #1
0
        public static void GetExpiredEntries_WhenCacheTimesExpired_ReturnsNonEmptyCollection()
        {
            var policy = new SlidingTimespanCachePolicy <string>(TimeSpan.FromSeconds(30));

            var cacheEntry1 = new Mock <ICacheEntry <string> >();

            cacheEntry1.Setup(k => k.CreationTime).Returns(DateTime.Now - TimeSpan.FromMinutes(2));
            cacheEntry1.Setup(k => k.LastAccessed).Returns(DateTime.Now - TimeSpan.FromMinutes(1));
            cacheEntry1.Setup(k => k.Size).Returns(5);
            cacheEntry1.Setup(k => k.Key).Returns("test1");

            var cacheEntry2 = new Mock <ICacheEntry <string> >();

            cacheEntry2.Setup(k => k.CreationTime).Returns(DateTime.Now);
            cacheEntry2.Setup(k => k.LastAccessed).Returns(DateTime.Now);
            cacheEntry2.Setup(k => k.Size).Returns(5);
            cacheEntry2.Setup(k => k.Key).Returns("test2");

            var cacheEntry3 = new Mock <ICacheEntry <string> >();

            cacheEntry3.Setup(k => k.CreationTime).Returns(DateTime.Now - TimeSpan.FromMinutes(2));
            cacheEntry3.Setup(k => k.LastAccessed).Returns(DateTime.Now - TimeSpan.FromMinutes(1));
            cacheEntry3.Setup(k => k.Size).Returns(5);
            cacheEntry3.Setup(k => k.Key).Returns("test3");

            var expired = policy.GetExpiredEntries(new[] { cacheEntry1.Object, cacheEntry2.Object, cacheEntry3.Object }, 20).ToList();

            Assert.AreEqual(2, expired.Count);
        }
Beispiel #2
0
        public static void GetExpiredEntries_WhenOnlyValueExceedsTimePeriod_ReturnsValue()
        {
            var policy = new SlidingTimespanCachePolicy <string>(TimeSpan.FromMilliseconds(1));

            var cacheEntry = new Mock <ICacheEntry <string> >();

            cacheEntry.Setup(k => k.CreationTime).Returns(DateTime.Now - TimeSpan.FromSeconds(10));
            cacheEntry.Setup(k => k.LastAccessed).Returns(DateTime.Now - TimeSpan.FromSeconds(10));
            cacheEntry.Setup(k => k.Size).Returns(1);
            cacheEntry.Setup(k => k.Key).Returns("test");

            var expired = policy.GetExpiredEntries(new[] { cacheEntry.Object }, 100).ToList();

            Assert.AreEqual(1, expired.Count);
        }
Beispiel #3
0
        public static void GetExpiredEntries_WhenCacheNotExhausted_ReturnsEmptySet()
        {
            var policy = new SlidingTimespanCachePolicy <string>(TimeSpan.FromSeconds(100));

            var cacheEntry = new Mock <ICacheEntry <string> >();

            cacheEntry.Setup(k => k.CreationTime).Returns(DateTime.Now);
            cacheEntry.Setup(k => k.LastAccessed).Returns(DateTime.Now);
            cacheEntry.Setup(k => k.Size).Returns(1);
            cacheEntry.Setup(k => k.Key).Returns("test");

            var expired = policy.GetExpiredEntries(new[] { cacheEntry.Object }, 100).ToList();

            Assert.AreEqual(0, expired.Count);
        }
Beispiel #4
0
        public static void GetExpiredEntries_WhenCacheTimespansExpired_ReturnsLeastRecentlyAccessedValue()
        {
            var policy = new SlidingTimespanCachePolicy <string>(TimeSpan.FromSeconds(30));

            var cacheEntry1 = new Mock <ICacheEntry <string> >();

            cacheEntry1.Setup(k => k.CreationTime).Returns(DateTime.Now - TimeSpan.FromMinutes(2));
            cacheEntry1.Setup(k => k.LastAccessed).Returns(DateTime.Now - TimeSpan.FromMinutes(1));
            cacheEntry1.Setup(k => k.Size).Returns(5);
            cacheEntry1.Setup(k => k.Key).Returns("test1");

            var cacheEntry2 = new Mock <ICacheEntry <string> >();

            cacheEntry2.Setup(k => k.CreationTime).Returns(DateTime.Now);
            cacheEntry2.Setup(k => k.LastAccessed).Returns(DateTime.Now);
            cacheEntry2.Setup(k => k.Size).Returns(5);
            cacheEntry2.Setup(k => k.Key).Returns("test2");

            var cacheEntry3 = new Mock <ICacheEntry <string> >();

            cacheEntry3.Setup(k => k.CreationTime).Returns(DateTime.Now - TimeSpan.FromMinutes(2));
            cacheEntry3.Setup(k => k.LastAccessed).Returns(DateTime.Now - TimeSpan.FromMinutes(1));
            cacheEntry3.Setup(k => k.Size).Returns(5);
            cacheEntry3.Setup(k => k.Key).Returns("test3");

            var expired = policy.GetExpiredEntries(new[] { cacheEntry1.Object, cacheEntry2.Object, cacheEntry3.Object }, 20).ToList();

            Assert.AreEqual(2, expired.Count);
            var firstExpiredKey = expired[0].Key;
            var lastExpiredKey  = expired.Last().Key;

            Assert.Multiple(() =>
            {
                Assert.AreEqual(cacheEntry1.Object.Key, firstExpiredKey);
                Assert.AreEqual(cacheEntry3.Object.Key, lastExpiredKey);
            });
        }
Beispiel #5
0
        public static void GetExpiredEntries_GivenZeroSize_ThrowsArgOutOfRangeException()
        {
            var policy = new SlidingTimespanCachePolicy <string>(TimeSpan.FromMilliseconds(1));

            Assert.Throws <ArgumentOutOfRangeException>(() => policy.GetExpiredEntries(Enumerable.Empty <ICacheEntry <string> >(), 0));
        }
Beispiel #6
0
        public static void GetExpiredEntries_GivenNullEntries_ThrowsArgNullException()
        {
            var policy = new SlidingTimespanCachePolicy <string>(TimeSpan.FromMilliseconds(1));

            Assert.Throws <ArgumentNullException>(() => policy.GetExpiredEntries(null, 1));
        }