public void TestTryGetValue() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); // Verify the cache is initially empty Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); // Verify no item with ID 5 is in the cache TestItem result; bool success = cache.TryGetValue(5, out result); Assert.IsFalse(success); Assert.IsNull(result); Assert.AreEqual(cache.Statistics.Hits, 0); Assert.AreEqual(cache.Statistics.Misses, 1); // Add a test item to the cache TestItem item = new TestItem(5); TestItem returnedItem = cache.GetOrAdd(item); Assert.AreEqual(cache.Statistics.Hits, 0); Assert.AreEqual(cache.Statistics.Misses, 2); // Verify the test item is returned success = cache.TryGetValue(5, out result); Assert.IsTrue(success); Assert.AreEqual(result, item); Assert.AreEqual(cache.Statistics.Hits, 1); Assert.AreEqual(cache.Statistics.Misses, 2); }
public void TestClearPermanent() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); TestItem[] permanentItems = { new TestItem(1), new TestItem(2), new TestItem(3), new TestItem(4) }; TestStringItem[] transientItems = { new TestStringItem("First"), new TestStringItem("Second"), new TestStringItem("Third") }; // First add the permanent items foreach (TestItem permanentItem in permanentItems) { cache.AddOrReplace(permanentItem, true); } // Then add the transient items foreach (TestStringItem transientItem in transientItems) { cache.AddOrReplace(transientItem, false); } // The inner cache should only contain the non-permanent items Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), transientItems.Length); Assert.That(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).All(x => transientItems.Contains(x.Value))); // Now clear the cache. All transient items should be removed. cache.Clear(); // Verify that all transient items were removed and the inner cache is empty Assert.AreEqual(cache.InnerCache.Count(), 0); // Verify that all permanent items remain Assert.That(permanentItems.All(x => { TestItem result; if (!cache.TryGetValue <TestItem>(x.CacheKey, out result)) { return(false); } if (!object.Equals(x, result)) { return(false); } return(true); })); }
public void TestAddOrReplace() { // Create the cache and register our test types EveCache cache = new EveCache(new MemoryCache("Eve.Tests")); // Verify the cache is initially empty Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0); // Add a test item to the cache TestItem item = new TestItem(5); TestItem returnedItem = cache.GetOrAdd(item); Assert.AreEqual(cache.Statistics.Hits, 0); Assert.AreEqual(cache.Statistics.CacheHits, 0); Assert.AreEqual(cache.Statistics.ReferenceHits, 0); Assert.AreEqual(cache.Statistics.Misses, 1); // Verify the method returns the correct value Assert.That(item == returnedItem); Assert.AreEqual(cache.Statistics.Writes, 1); // Verify that the item was added to the cache Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 1); // Add a new item with the same ID TestItem newItem = new TestItem(5); cache.AddOrReplace(newItem); // Retrieve the newly-added item and make sure the cache returns // the new value cache.TryGetValue(5, out item); Assert.That(item == newItem); Assert.AreEqual(cache.Statistics.Writes, 2); // Verify that the cache count remains the same Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 1); }