Exemplo n.º 1
0
        public void TestClear()
        {
            // Create the cache and register our test types
            EveCache cache = new EveCache(new MemoryCache("Eve.Tests"));

            TestItem[]       items       = new TestItem[10];
            TestChildItem[]  childItems  = new TestChildItem[10];
            TestStringItem[] stringItems = new TestStringItem[10];

            // Populate with initial items
            for (int i = 0; i < 10; i++)
            {
                items[i]       = new TestItem(i);
                childItems[i]  = new TestChildItem(i + 100); // Add 100 to avoid ID collisions
                stringItems[i] = new TestStringItem("Test" + i.ToString());
                cache.GetOrAdd(items[i]);
                cache.GetOrAdd(childItems[i]);
                cache.GetOrAdd(stringItems[i]);
            }

            // Verify that all test items have been added
            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 30);
            Assert.AreEqual(cache.Statistics.Writes, 30);

            // Clean the cache
            cache.Clear();

            // Verify that the objects have been removed
            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0);
        }
Exemplo n.º 2
0
        public void TestGetOrAddValueFactoryWithInvalidKey()
        {
            // 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 with an incorrect key
            TestStringItem item = new TestStringItem("Real Key");

            Assert.Throws(typeof(InvalidOperationException), () => cache.GetOrAdd("Requested Key", () => item));
        }
Exemplo n.º 3
0
        public void TestClearNonPrefix()
        {
            // Create the cache and register our test types
            EveCache cache = new EveCache(new MemoryCache("Eve.Tests"));

            TestItem[]       items       = new TestItem[10];
            TestChildItem[]  childItems  = new TestChildItem[10];
            TestStringItem[] stringItems = new TestStringItem[10];

            // Populate with initial items
            for (int i = 0; i < 10; i++)
            {
                items[i]       = new TestItem(i);
                childItems[i]  = new TestChildItem(i + 100); // Add 100 to avoid ID collisions
                stringItems[i] = new TestStringItem("Test" + i.ToString());
                cache.GetOrAdd(items[i]);
                cache.GetOrAdd(childItems[i]);
                cache.GetOrAdd(stringItems[i]);
            }

            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 30);
            Assert.AreEqual(cache.Statistics.Writes, 30);

            cache.InnerCache.Add("NON-EveCacheRegionPrefix-1", "Test1", new CacheItemPolicy());
            cache.InnerCache.Add("NON-EveCacheRegionPrefix-2", "Test2", new CacheItemPolicy());

            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 32);

            // Verify that all test items have been added

            // Clean the cache
            cache.Clear();

            // Verify that the objects with the EVE prefix were removed but the others were not affected
            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 2);
            Assert.That(cache.InnerCache.Contains("NON-EveCacheRegionPrefix-1"));
            Assert.That(cache.InnerCache.Contains("NON-EveCacheRegionPrefix-2"));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void TestRemove()
        {
            // Create the cache and register our test types
            EveCache cache = new EveCache(new MemoryCache("Eve.Tests"));

            TestItem[]       items       = new TestItem[10];
            TestChildItem[]  childItems  = new TestChildItem[10];
            TestStringItem[] stringItems = new TestStringItem[10];

            // Populate with initial items
            for (int i = 0; i < 10; i++)
            {
                items[i]       = new TestItem(i);
                childItems[i]  = new TestChildItem(i + 100); // Add 100 to avoid ID collisions
                stringItems[i] = new TestStringItem("Test" + i.ToString());
                cache.GetOrAdd(items[i]);
                cache.GetOrAdd(childItems[i]);
                cache.GetOrAdd(stringItems[i]);
            }

            // Verify that all test items have been added
            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 30);

            // Remove the first 10 items
            foreach (TestItem test1 in items)
            {
                var result = cache.Remove <TestItem>(test1.Id);

                // Verify the return value
                Assert.AreEqual(result, test1);
            }

            // Verify that the objects have been removed
            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 20);

            // Try to remove a non-existent item
            var value = cache.Remove <TestItem>("Nonexistent");

            Assert.IsNull(value);

            // Remove the next 10 items
            foreach (TestChildItem test2 in childItems)
            {
                var result = cache.Remove <TestChildItem>(test2.Id);

                // Verify the return value
                Assert.AreEqual(result, test2);
            }

            // Verify that the objects have been removed
            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 10);

            // Remove the last 10 items
            foreach (TestStringItem test3 in stringItems)
            {
                var result = cache.Remove <TestStringItem>(test3.Id);

                // Verify the return value
                Assert.AreEqual(result, test3);
            }

            // Verify that the objects have been removed
            Assert.AreEqual(((IEnumerable <KeyValuePair <string, object> >)cache.InnerCache).Count(), 0);
        }