public void TestContains()
        {
            using (var externalCache = new ExternalCache())
            {
                Assert.IsFalse(externalCache.Contains("Item 1"));
                var item = new Dynamo();
                item["P1"] = "Property 1";
                externalCache.Add("Item 1", item, new TimeSpan(10000000)); // 10000 ticks = 1ms

                Assert.IsTrue(externalCache.Contains("Item 1"));
            }
        }
        public void TestRemove()
        {
            using (var externalCache = new ExternalCache())
            {
                var item = new Dynamo();
                item["P1"] = "Property 1";
                externalCache.Add("Item 1", item);

                Assert.IsTrue(externalCache.Contains("Item 1"));
                externalCache.Remove("Item 1");
                Assert.IsFalse(externalCache.Contains("Item 1"));
            }
        }
Esempio n. 3
0
        public void TestStressConcurrentAddGetAndRemove()
        {
            const int threadCount = 4;
            const int loops       = 1000;
            var       totalLoops  = 0;
            var       threads     = new Thread[threadCount];

            for (var i = 0; i < threadCount; i++)
            {
                (threads[i] = new Thread(context =>
                {
                    using (var externalCache = new ExternalCache())
                    {
                        for (var j = 0; j < loops; j++)
                        {
                            using (var item = new ExternalCacheItem())
                            {
                                item["P1"] = "Property " + j;
                                externalCache.Add($"Item {(int) context}-{j}", item);
                            }

                            Assert.IsTrue(externalCache.Contains($"Item {(int)context}-{j}"));
                            var returnedItem = (ExternalCacheItem)externalCache.Get($"Item {(int) context}-{j}");
                            Assert.AreEqual("Property " + j, returnedItem["P1"]);
                            externalCache.Remove($"Item {(int)context}-{j}");
                            Assert.IsFalse(externalCache.Contains($"Item {(int)context}-{j}"));
                            Interlocked.Increment(ref totalLoops);
                        }
                    }
                })).Start(i);
            }
            foreach (var thread in threads)
            {
                thread.Join();
            }
            Assert.AreEqual(threadCount * loops, totalLoops);
        }