コード例 #1
0
 public void TestSelect()
 {
     using (var externalCache = new ExternalCache())
     {
         // ReSharper disable once InconsistentNaming
         using (var _item = new ExternalCacheItem())
         {
             _item["P1"] = "Property 1";
             externalCache.Add("Item 1", _item);
         }
         var item = (ExternalCacheItem)externalCache.Get("Item 1");
         Assert.IsNotNull(item);
         externalCache.Select("Second Cache");
         item = (ExternalCacheItem)externalCache.Get("Item 1");
         Assert.IsNull(item);
         // ReSharper disable once InconsistentNaming
         using (var _item = new ExternalCacheItem())
         {
             _item["P1"] = "Property 1";
             externalCache.Add("Item 2", _item);
         }
         item = (ExternalCacheItem)externalCache.Get("Item 2");
         Assert.IsNotNull(item);
         externalCache.Select("default");
         item = (ExternalCacheItem)externalCache.Get("Item 1");
         Assert.IsNotNull(item);
         item = (ExternalCacheItem)externalCache.Get("Item 2");
         Assert.IsNull(item);
     }
 }
コード例 #2
0
        public void TestClearAllCaches()
        {
            using (var externalCache = new ExternalCache())
            {
                // ReSharper disable once InconsistentNaming
                using (var _item = new ExternalCacheItem())
                {
                    _item["P1"] = "Property 1";
                    externalCache.Add("Item 1", _item);
                }

                var item = (ExternalCacheItem)externalCache.Get("Item 1");
                Assert.AreEqual("Property 1", item["P1"]);
                externalCache.Select("Cache 2");
                externalCache.Add("Item 2", item);
                item = (ExternalCacheItem)externalCache.Get("Item 2");
                Assert.AreEqual("Property 1", item["P1"]);
                using (var externalCacheManager = new ExternalCacheManager())
                    externalCacheManager.ClearAllCaches();
                Thread.Sleep(1000);
                item = (ExternalCacheItem)externalCache.Get("Item 1");
                Assert.IsNull(item);
                item = (ExternalCacheItem)externalCache.Get("Item 2");
                Assert.IsNull(item);
                externalCache.Select("default");
                item = (ExternalCacheItem)externalCache.Get("Item 1");
                Assert.IsNull(item);
                item = (ExternalCacheItem)externalCache.Get("Item 2");
                Assert.IsNull(item);
            }
        }
コード例 #3
0
        public void TestAddObjectAndGet()
        {
            using (var externalCache = new ExternalCache())
            {
                // ReSharper disable once InconsistentNaming
                using (var _item = new ExternalCacheItem())
                {
                    _item["P1"] = "Property 1";
                    _item["P2"] = "Property 2";
                    externalCache.Add("Item 1", _item);
                }
                var item = (ExternalCacheItem)externalCache.Get("Item 1");
                Assert.AreEqual("Property 1", item["P1"]);
                Assert.AreEqual("Property 2", item["P2"]);
                item["P1"] = "Property 3";
                item["P2"] = "Property 4";

                /* Contrary to documentation, method Add() will operate the same as
                 * AddOrGetExisting() yet return False if the item already exists */
                Assert.IsFalse(externalCache.Add("Item 1", item));
                item = (ExternalCacheItem)externalCache.Get("Item 1");
                Assert.AreEqual("Property 3", item["P1"]);
                Assert.AreEqual("Property 4", item["P2"]);
            }
        }
コード例 #4
0
 public void TestSetPropertiesUsingArray()
 {
     using (var item = new ExternalCacheItem())
     {
         item["Prop1"] = 1;
         item["Prop2"] = "Hello";
         Assert.AreEqual(1, item["Prop1"]);
         Assert.AreEqual("Hello", item["Prop2"]);
     }
 }
コード例 #5
0
 public void TestSetPropertiesOfContainer()
 {
     using (var item = new ExternalCacheItem())
     {
         item.Container.Prop1 = 1;
         item.Container.Prop2 = "Hello";
         Assert.AreEqual(1, item.Container.Prop1);
         Assert.AreEqual("Hello", item.Container.Prop2);
     }
 }
コード例 #6
0
 public void TestAddObjectAndLetItExpireByAbsoluteTime()
 {
     using (var externalCache = new ExternalCache())
     {
         using (var item = new ExternalCacheItem())
         {
             item["P1"] = "Property 1";
             externalCache.Add("Item 1", item, new DateTimeOffset(DateTime.Now.AddMilliseconds(1000)));
             CheckItem1(externalCache);
         }
     }
 }
コード例 #7
0
 public void TestAddObjectAndLetItExpireByTicks()
 {
     using (var externalCache = new ExternalCache())
     {
         using (var item = new ExternalCacheItem())
         {
             item["P1"] = "Property 1";
             externalCache.Add("Item 1", item, new TimeSpan(10000000)); // 10000 ticks = 1ms
             CheckItem1(externalCache);
         }
     }
 }
コード例 #8
0
        public void TestContains()
        {
            using (var externalCache = new ExternalCache())
            {
                Assert.IsFalse(externalCache.Contains("Item 1"));
                using (var item = new ExternalCacheItem())
                {
                    item["P1"] = "Property 1";
                    externalCache.Add("Item 1", item, new TimeSpan(10000000)); // 10000 ticks = 1ms
                }

                Assert.IsTrue(externalCache.Contains("Item 1"));
            }
        }
コード例 #9
0
        public void TestRemove()
        {
            using (var externalCache = new ExternalCache())
            {
                using (var item = new ExternalCacheItem())
                {
                    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"));
            }
        }
コード例 #10
0
 public void TestAddOrGetExisting()
 {
     using (var externalCache = new ExternalCache())
     {
         // ReSharper disable once InconsistentNaming
         using (var _item = new ExternalCacheItem())
         {
             _item["P1"] = "Item 1";
             externalCache.Add("Item 1", _item);
         }
         ExternalCacheItem item = new ExternalCacheItem();
         item["P1"] = "Item 2";
         item       = (ExternalCacheItem)externalCache.AddOrGetExisting("Item 1", item);
         Assert.AreEqual("Item 1", item["P1"]);
     }
 }
コード例 #11
0
        public void TestClear()
        {
            using (var externalCache = new ExternalCache())
            {
                // ReSharper disable once InconsistentNaming
                using (var _item = new ExternalCacheItem())
                {
                    _item["P1"] = "Property 1";
                    externalCache.Add("Item 1", _item);
                }

                var item = (ExternalCacheItem)externalCache.Get("Item 1");
                Assert.AreEqual("Property 1", item["P1"]);
                externalCache.Clear();
                item = (ExternalCacheItem)externalCache.Get("Item 1");
                Assert.IsNull(item);
            }
        }
コード例 #12
0
 public void TestCopyFromAndTo()
 {
     using (var item = new ExternalCacheItem())
     {
         var obj = new TestClass {
             Prop1 = "P1", Prop2 = "P2", Prop3 = "P3", Prop4 = -1
         };
         item.CopyFrom(obj);
         Assert.AreEqual("P1", item.Container.Prop1);
         Assert.AreEqual("P2", item.Container.Prop2);
         Assert.AreEqual("P3", item.Container.Prop3);
         Assert.AreEqual(-1, item.Container.Prop4);
         obj = new TestClass();
         Assert.AreEqual(null, obj.Prop1);
         Assert.AreEqual(null, obj.Prop2);
         Assert.AreEqual(null, obj.Prop3);
         Assert.AreEqual(0, obj.Prop4);
         item.CopyTo(obj);
         Assert.AreEqual("P1", obj.Prop1);
         Assert.AreEqual("P2", obj.Prop2);
         Assert.AreEqual("P3", obj.Prop3);
         Assert.AreEqual(-1, obj.Prop4);
         using (var item2 = new ExternalCacheItem())
         {
             item.CopyTo(item2._container);
             Assert.AreEqual(4, item2.Container.Count);
             Assert.AreEqual("P1", item2.Container.Prop1);
             Assert.AreEqual("P2", item2.Container.Prop2);
             Assert.AreEqual("P3", item2.Container.Prop3);
             Assert.AreEqual(-1, item2.Container.Prop4);
             using (var item3 = new ExternalCacheItem())
             {
                 item3.CopyFrom(item2._container);
                 Assert.AreEqual(4, item3.Container.Count);
                 Assert.AreEqual("P1", item3.Container.Prop1);
                 Assert.AreEqual("P2", item3.Container.Prop2);
                 Assert.AreEqual("P3", item3.Container.Prop3);
                 Assert.AreEqual(-1, item3.Container.Prop4);
             }
         }
     }
 }
コード例 #13
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);
        }
コード例 #14
0
        static void Main()
        {
            const int    loops       = 100000;
            const int    threadCount = 4;
            NumbersAdder adder       = new NumbersAdder();             // This object now created within the context of COM Surrogate (32 bit) process

            Console.WriteLine("10 + 12 = " + adder.add(10, 12));       // This call marshaled to COM Surrogate process hosting a copy of NumberOperators.dll
            NumbersSubstracter substracter = new NumbersSubstracter(); // This object created within current AppDomain as a normal .NET assembly

            Console.WriteLine("10 - 12 = " + substracter.sub(10, 12)); // Normal call to .NET object
            IntPair pair = new IntPair()
            {
                a = 10, b = 12
            };

            Console.WriteLine("pair.a = 10, pair.b = 12. adder.add(pair) = " + adder.add(pair));
            pair = adder.buildPair(5, 10);
            Console.WriteLine("Built pair in COM Server. pair.a = " + pair.a + ". pair.b = " + pair.b);
            pair.Clear();
            Console.WriteLine("Called pair.Clear()");
            Console.WriteLine("Built pair in COM Server. pair.a = " + pair.a + ". pair.b = " + pair.b);

            var initialTicks = Environment.TickCount;

            for (var i = 0; i < loops; i++)
            {
                adder.add(10, 2);
            }
            Console.WriteLine("Requests per second (Single threaded): " + (loops / ((float)(Environment.TickCount - initialTicks) / 1000)));

            initialTicks = Environment.TickCount;
            var threads = new Thread[threadCount];

            for (var i = 0; i < threadCount; i++)
            {
                (threads[i] = new Thread((innerAdder =>
                {
                    for (var j = 0; j < loops; j++)
                    {
                        ((NumbersAdder)innerAdder).add(10, 2);
                    }
                }))).Start(new NumbersAdder());
            }
            foreach (var thread in threads)
            {
                thread.Join();
            }
            Console.WriteLine($"Aggregate requests per second ({threadCount} threads): " + (int)(threadCount * loops / ((float)(Environment.TickCount - initialTicks) / 1000)));
            Console.WriteLine($"Per thread requests per second ({threadCount} threads): " + (int)(loops / ((float)(Environment.TickCount - initialTicks) / 1000)));

            initialTicks = Environment.TickCount;
            var adderi = (INumbersAdder)adder;

            for (var i = 0; i < loops; i++)
            {
                adderi.add(10, 2);
            }
            Console.WriteLine("Requests per second (Single threaded using interface): " + (loops / ((float)(Environment.TickCount - initialTicks) / 1000)));

            var externalCache = new ExternalCache();

            // externalCache.Select("CacheTest");
            externalCache.Add("test", 1);
            var item = new ExternalCacheItem();

            item["Test"] = "hello";
            externalCache.Add("test 2", item, new TimeSpan(5000));
            var retrieved = (ExternalCacheItem)externalCache.Get("test 2");

            Console.WriteLine(retrieved.Container.Test);
            Thread.Sleep(10000);
            retrieved = (ExternalCacheItem)externalCache.Get("test 2");
            Console.WriteLine("Retrieved = null:" + (retrieved == null));

            var tester = new Tester {
                Val = "Value 1"
            };

            item = new ExternalCacheItem();
            item.CopyFrom(tester);
            externalCache.Add("test 2", item);
            item   = (ExternalCacheItem)externalCache.Get("test 2");
            tester = new Tester();
            item.CopyTo(tester);
            Console.WriteLine(tester.Val);

            Console.WriteLine("Press any key to finish");
            Console.ReadLine();
        }
コード例 #15
0
 public void TestCreateExternalCacheItem()
 {
     using (var item = new ExternalCacheItem())
         Assert.IsNotNull(item);
 }