public void TestExpiredItemsSweeped() { TimeSpan sweepTimer = TimeSpan.FromMilliseconds(800); bool timerInactivated = false; AgeBasedCache <int, int> cache = AgeBasedCache <int, int> .CreateInstanceForTestingOnly(sweepTimer, TimeSpan.MaxValue, () => timerInactivated = true); cache.Clear(); int i = 0; int cleanedUpItems = 0; for (; i < AgeBasedCache <int, int> .MaxCacheCount; ++i) { Assert.IsTrue(cache.TryAddOrUpdate(i, i, TimeSpan.FromMilliseconds(100), (item => cleanedUpItems++)), "Cache Add expected to Pass but failed"); } // Wait for sweep to happen Threading.Thread.Sleep(sweepTimer + sweepTimer); Assert.IsTrue(cleanedUpItems == i, string.Format("Actual cleaned up Count: {0}. Expected {1}", cleanedUpItems, i)); Assert.IsTrue(cache.Count == 0); Assert.IsFalse(timerInactivated, "Timer shouldn't have been inactivated"); Assert.IsTrue(cache.TryAddOrUpdate(i, i, sweepTimer, null), "Cache Add expected to Pass but failed"); Assert.IsTrue(cache.Count == 1); // Wait for sweep to happen Threading.Thread.Sleep(sweepTimer + sweepTimer); // Make sure item sweeped Assert.IsTrue(cache.Count == 0); }
public void TestCacheSweepsAfterTimerInactivity() { TimeSpan sweepTimer = TimeSpan.FromMilliseconds(800); bool timerInactivated = false; AgeBasedCache <int, int> cache = AgeBasedCache <int, int> .CreateInstanceForTestingOnly(sweepTimer, sweepTimer, () => timerInactivated = true); // start with clean slate cache.Clear(); int cleanedUpItems = 0; Assert.IsTrue(cache.TryAddOrUpdate(1, 1, TimeSpan.FromMilliseconds(100), (item => cleanedUpItems++)), "Cache Add expected to Pass but failed"); // Wait for sweep to happen Threading.Thread.Sleep(sweepTimer + sweepTimer); Assert.IsTrue(cleanedUpItems == 1, string.Format("Actual cleaned up Count: {0}. Expected 1", cleanedUpItems)); Assert.IsTrue(cache.Count == 0, "We expect 0 items in cache"); Assert.IsTrue(timerInactivated, "Time is supposed to be inactivated"); // Add another Item. Assert.IsTrue(cache.TryAddOrUpdate(2, 2, sweepTimer, null), "Cache Add expected to Pass but failed"); // Wait for sweep to happen Threading.Thread.Sleep(sweepTimer + sweepTimer); // Make sure timer get started again and item get sweeped Assert.IsTrue(cache.Count == 0); }
public void TestCacheMaxBoundScenario() { AgeBasedCache <int, int> cache = AgeBasedCache <int, int> .DefaultInstance; int i = 0; for (; i < AgeBasedCache <int, int> .MaxCacheCount; ++i) { Assert.IsTrue( cache.TryAddOrUpdate(i, i, TimeSpan.FromSeconds(AgeBasedCache <int, int> .DefaultCleanupDurationInSec + 10), null), "Cache Add expected to Pass but failed"); } Assert.IsTrue(cache.Count == AgeBasedCache <int, int> .MaxCacheCount, "Expected Count != Actual Count"); Assert.IsFalse(cache.TryAddOrUpdate(i, i, TimeSpan.FromSeconds(10), null), "Cache Add expected to fail but Passed"); cache.Clear(); Assert.IsTrue(cache.TryAddOrUpdate(i, i, TimeSpan.FromSeconds(10), null), "Cache Add expected to Pass but Failed"); // Cleanup cache.Clear(); }