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); }
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); }
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); }
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); }