Ejemplo n.º 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);
    }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
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);

    }