public void StructTest() { SingleItemMemoryCache<Cat> actual = new SingleItemMemoryCache<Cat>(TestLoadCatMethod); Cat cat = actual.GetItem(); Assert.AreNotEqual<Guid>(Guid.Empty, cat.UniqueId); Guid expectedUniqueId = cat.UniqueId; Cat cat2 = actual.GetItem(); Assert.AreNotEqual<Guid>(Guid.Empty, cat.UniqueId); Assert.AreEqual<Guid>(expectedUniqueId, cat2.UniqueId); Assert.AreSame(cat, cat2); int expectedClawLength = 7; cat.ClawLength = expectedClawLength; Assert.AreEqual<int>(expectedClawLength, cat2.ClawLength); actual.RemoveItem(); Cat cat3 = actual.GetItem(); Assert.AreNotEqual<Guid>(Guid.Empty, cat.UniqueId); Assert.AreNotEqual<Guid>(expectedUniqueId, cat3.UniqueId); }
public void ImmediateExpirationTest() { CacheOptions cacheOptions = new CacheOptions() { Expiration = CacheExpiration.Immediate }; SingleItemMemoryCache<Cat> actual = new SingleItemMemoryCache<Cat>(TestLoadCatMethod, cacheOptions); Cat cat = actual.GetItem(); Assert.IsNotNull(cat); Guid expectedUniqueId = cat.UniqueId; Cat cat2 = actual.GetItem(); Assert.IsNotNull(cat2); Assert.AreNotSame(cat, cat2); Assert.AreNotEqual<Guid>(expectedUniqueId, cat2.UniqueId); int expectedClawLength = 7; cat.ClawLength = expectedClawLength; Assert.AreNotEqual<int>(expectedClawLength, cat2.ClawLength); }
public void SlidingExpirationTest() { CacheOptions cacheOptions = new CacheOptions() { Expiration = CacheExpiration.Sliding, CacheDuration = TimeSpan.FromSeconds(2D) }; SingleItemMemoryCache<Cat> actual = new SingleItemMemoryCache<Cat>(TestLoadCatMethod, cacheOptions); Cat cat = actual.GetItem(); Assert.IsNotNull(cat); Guid originalUniqueId = cat.UniqueId; for (int counter = 0; counter < 5; counter++) { System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1D)); Cat cat2 = actual.GetItem(); Assert.IsNotNull(cat2); Assert.AreSame(cat, cat2); Assert.AreEqual<Guid>(originalUniqueId, cat2.UniqueId); } System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3D)); Cat cat3 = actual.GetItem(); Assert.IsNotNull(cat3); Assert.AreNotSame(cat, cat3); Assert.AreNotEqual<Guid>(originalUniqueId, cat3.UniqueId); }