Esempio 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);
        }
Esempio n. 2
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);
            }));
        }
Esempio 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"));
        }