Exemple #1
0
        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);
            }));
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        public void TestGetOrAddReferenceTracker()
        {
            // 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
            TestStringItem item = new TestStringItem("Real Key");

            cache.AddOrReplace(item);

            // Retrieve the item and verify it was retrieved from the reference tracker
            long cacheCountBefore     = cache.Statistics.CacheHits;
            long referenceCountBefore = cache.Statistics.ReferenceHits;

            cache.GetOrAdd(item.CacheKey, () => item);
            long cacheCountAfter     = cache.Statistics.CacheHits;
            long referenceCountAfter = cache.Statistics.ReferenceHits;

            Assert.That(cacheCountAfter == cacheCountBefore);
            Assert.That(referenceCountAfter == referenceCountBefore + 1);
        }