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"));
            }
        }
 public void TestAddObjectAndGet()
 {
     using (var externalCache = new ExternalCache())
     {
         // ReSharper disable once InconsistentNaming
         var _item = new Dynamo();
         _item["P1"] = "Property 1";
         _item["P2"] = "Property 2";
         externalCache.Add("Item 1", _item);
         var item = (Dynamo)externalCache.Get("Item 1");
         Assert.AreEqual("Property 1", item["P1"]);
         Assert.AreEqual("Property 2", item["P2"]);
         item["P1"] = "Property 3";
         item["P2"] = "Property 4";
         Assert.IsNotNull(externalCache.Remove("Item 1"));
         Assert.IsTrue(externalCache.Add("Item 1", item));
         item = (Dynamo)externalCache.Get("Item 1");
         Assert.AreEqual("Property 3", item["P1"]);
         Assert.AreEqual("Property 4", item["P2"]);
     }
 }
Beispiel #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);
        }