예제 #1
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.AreEqual(true, hasExpiration);
      bool isExpired = cacheItem.IsExpired();
      Assert.AreEqual(false, isExpired);

      TimeSpan expiration = TimeSpan.FromSeconds(2);

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

      Thread.Sleep(expiration);
      isExpired = cacheItem.IsExpired();
      Assert.AreEqual(true, isExpired);

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

      Thread.Sleep(expiration);
      isExpired = cacheItem.IsExpired();
      Assert.AreEqual(true, isExpired);
    }
예제 #2
0
    public void CacheItemConstructorTest()
    {
      string key = "key";
      object value = "value";
      var cachePolicy = new CachePolicy();
      var cacheItem = new CacheItem(key, value, cachePolicy);

      Assert.IsNotNull(cacheItem);
      Assert.AreEqual("key", cacheItem.Key);
      Assert.AreEqual("value", cacheItem.Value);
      Assert.IsNotNull(cacheItem.CachePolicy);
      Assert.AreEqual(CacheManager.NoSlidingExpiration, cacheItem.CachePolicy.SlidingExpiration);
      Assert.AreEqual(CacheManager.InfiniteAbsoluteExpiration, cacheItem.CachePolicy.AbsoluteExpiration);
    }
예제 #3
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.AreEqual(true, hasExpiration);
      
      cachePolicy = new CachePolicy();
      cacheItem = new CacheItem(key, value, cachePolicy);
      hasExpiration = cacheItem.CanExpire();
      Assert.AreEqual(false, hasExpiration);

      cachePolicy = new CachePolicy { AbsoluteExpiration = DateTimeOffset.Now.AddDays(1) };
      cacheItem = new CacheItem(key, value, cachePolicy);
      hasExpiration = cacheItem.CanExpire();
      Assert.AreEqual(true, hasExpiration);
    }
예제 #4
0
 public void CacheItemConstructorNullValueTest()
 {
   string key = "key";
   object value = null;
   var cacheItem = new CacheItem(key, value);
 }
예제 #5
0
 public void CacheItemConstructorNullKeyTest()
 {
   string key = null;
   object value = "value";
   var cacheItem = new CacheItem(key, value);
 }
예제 #6
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.AreEqual(true, hasExpiration);
      bool isExpired = cacheItem.IsExpired();
      Assert.AreEqual(false, isExpired);

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

      isExpired = cacheItem.IsExpired();
      Assert.AreEqual(false, isExpired);

      Thread.Sleep(expiration);
      isExpired = cacheItem.IsExpired();
      Assert.AreEqual(true, isExpired);
      
      cacheItem.UpdateUsage();
      isExpired = cacheItem.IsExpired();
      Assert.AreEqual(false, isExpired);
    }
예제 #7
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.AreEqual(true, hasExpiration);
      bool isExpired = cacheItem.IsExpired();
      Assert.AreEqual(false, isExpired);

      Thread.Sleep(expiration);
      isExpired = cacheItem.IsExpired();
      Assert.AreEqual(true, isExpired);

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

      Assert.AreEqual(true, expireCalled);

    }