public void TestCachedItemExpiry() { ResultsCache<int, int> cache = new ResultsCache<int, int>(new TimeSpan(0,0,1)); cache.InsertResult(5, 25); System.Threading.Thread.Sleep(600); cache.InsertResult(6, 36); System.Threading.Thread.Sleep(600); int result; Assert.IsFalse(cache.LookupResult(5, out result)); Assert.IsTrue(cache.LookupResult(6, out result)); Assert.AreEqual(result, 36); }
public void TestCleansUpOldItems() { ResultsCache<int, int> cache = new ResultsCache<int, int>(new TimeSpan(0, 0, 1)); for (int i = 0; i < 100; i++) cache.InsertResult(i, i * i); Assert.AreEqual(cache.ItemCount, 100); // query the cache 100 times, which should trigger the periodic cleanup at some point int result; for (int i = 0; i < 100; i++) cache.LookupResult(i, out result); //however, since insufficient time has passed, all records still there Assert.AreEqual(cache.ItemCount, 100); System.Threading.Thread.Sleep(1100); for (int i = 0; i < 100; i++) cache.LookupResult(i, out result); // this time round the periodic cleanup will dump all records since >1sec passed Assert.AreEqual(cache.ItemCount, 0); }
public void TestPairAsQueryType() { ResultsCache<KeyValuePair<string, string>, int> cache = new ResultsCache<KeyValuePair<string, string>, int>(); cache.InsertResult(new KeyValuePair<string, string>("one", "two"), 12); cache.InsertResult(new KeyValuePair<string, string>("two", "two"), 22); cache.InsertResult(new KeyValuePair<string, string>("one", "one"), 11); int iRetVal; Assert.IsTrue(cache.LookupResult(new KeyValuePair<string, string>("one", "two"), out iRetVal)); Assert.AreEqual(iRetVal, 12); Assert.IsTrue(cache.LookupResult(new KeyValuePair<string, string>("two", "two"), out iRetVal)); Assert.AreEqual(iRetVal, 22); Assert.IsTrue(cache.LookupResult(new KeyValuePair<string, string>("one", "one"), out iRetVal)); Assert.AreEqual(iRetVal, 11); Assert.IsFalse(cache.LookupResult(new KeyValuePair<string, string>("two", "one"), out iRetVal)); Assert.AreEqual(0, iRetVal); }