Пример #1
0
        public void UpdateUsageTest()
        {
            string   key        = "key";
            object   value      = "value";
            TimeSpan expiration = TimeSpan.FromSeconds(2);

            var cachePolicy = new CachePolicy {
                SlidingExpiration = expiration
            };
            var cacheItem = new CacheItem(key, value, cachePolicy);

            bool hasExpiration = cacheItem.CanExpire();

            Assert.True(hasExpiration);
            bool isExpired = cacheItem.IsExpired();

            Assert.False(isExpired);

            Thread.Sleep(TimeSpan.FromSeconds(1));
            cacheItem.UpdateUsage();

            isExpired = cacheItem.IsExpired();
            Assert.False(isExpired);

            Thread.Sleep(expiration);
            isExpired = cacheItem.IsExpired();
            Assert.True(isExpired);

            cacheItem.UpdateUsage();
            isExpired = cacheItem.IsExpired();
            Assert.False(isExpired);
        }
Пример #2
0
        public void RaiseExpiredCallbackTest()
        {
            string   key          = "key";
            object   value        = "value";
            TimeSpan expiration   = TimeSpan.FromSeconds(2);
            bool     expireCalled = false;

            var cachePolicy = new CachePolicy
            {
                SlidingExpiration = expiration,
                ExpiredCallback   = e => { expireCalled = true; }
            };
            var  cacheItem     = new CacheItem(key, value, cachePolicy);
            bool hasExpiration = cacheItem.CanExpire();

            Assert.True(hasExpiration);
            bool isExpired = cacheItem.IsExpired();

            Assert.False(isExpired);

            Thread.Sleep(expiration);
            isExpired = cacheItem.IsExpired();
            Assert.True(isExpired);

            cacheItem.RaiseExpiredCallback();
            Thread.Sleep(expiration);

            Assert.True(expireCalled);
        }
Пример #3
0
        public void IsExpiredTest()
        {
            string key         = "key";
            object value       = "value";
            var    cachePolicy = new CachePolicy {
                AbsoluteExpiration = DateTimeOffset.Now.AddDays(1)
            };
            var  cacheItem     = new CacheItem(key, value, cachePolicy);
            bool hasExpiration = cacheItem.CanExpire();

            Assert.True(hasExpiration);
            bool isExpired = cacheItem.IsExpired();

            Assert.False(isExpired);

            TimeSpan expiration = TimeSpan.FromSeconds(2);

            cachePolicy = new CachePolicy {
                SlidingExpiration = expiration
            };
            cacheItem     = new CacheItem(key, value, cachePolicy);
            hasExpiration = cacheItem.CanExpire();
            Assert.True(hasExpiration);
            isExpired = cacheItem.IsExpired();
            Assert.False(isExpired);

            Thread.Sleep(expiration);
            Thread.Sleep(100);
            isExpired = cacheItem.IsExpired();
            Assert.True(isExpired);

            cachePolicy = new CachePolicy {
                AbsoluteExpiration = DateTimeOffset.Now.Add(expiration)
            };
            cacheItem     = new CacheItem(key, value, cachePolicy);
            hasExpiration = cacheItem.CanExpire();
            Assert.True(hasExpiration);
            isExpired = cacheItem.IsExpired();
            Assert.False(isExpired);

            Thread.Sleep(expiration);
            Thread.Sleep(100);
            isExpired = cacheItem.IsExpired();
            Assert.True(isExpired);
        }
Пример #4
0
        public void HasExpirationTest()
        {
            string key         = "key";
            object value       = "value";
            var    cachePolicy = new CachePolicy {
                SlidingExpiration = TimeSpan.FromSeconds(30)
            };
            var  cacheItem     = new CacheItem(key, value, cachePolicy);
            bool hasExpiration = cacheItem.CanExpire();

            Assert.True(hasExpiration);

            cachePolicy   = new CachePolicy();
            cacheItem     = new CacheItem(key, value, cachePolicy);
            hasExpiration = cacheItem.CanExpire();
            Assert.False(hasExpiration);

            cachePolicy = new CachePolicy {
                AbsoluteExpiration = DateTimeOffset.Now.AddDays(1)
            };
            cacheItem     = new CacheItem(key, value, cachePolicy);
            hasExpiration = cacheItem.CanExpire();
            Assert.True(hasExpiration);
        }