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); } }
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); } }
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"]); } }
public void TestTryAddSameTwice() { using (var externalCache = new ExternalCache()) { Assert.IsTrue(externalCache.Add("Item 1", "Value 1")); var item = (string)externalCache.Get("Item 1"); Assert.AreEqual("Value 1", item); Assert.IsFalse(externalCache.Add("Item 1", "Value 2")); item = (string)externalCache.Get("Item 1"); Assert.AreEqual("Value 1", item); } }
public void TestAddStringAndLetItExpireByTicks() { using (var externalCache = new ExternalCache()) { externalCache.Add("Item 1", "Value 1", new TimeSpan(10000000)); // 10000 ticks = 1ms CheckItem1AsString(externalCache); } }
public void TestAddStringAndLetItExpireByAbsoluteTime() { using (var externalCache = new ExternalCache()) { externalCache.Add("Item 1", "Value 1", new DateTimeOffset(DateTime.Now.AddMilliseconds(1000))); CheckItem1AsString(externalCache); } }
public void TestAddDecimalAndGet() { using (var externalCache = new ExternalCache()) { externalCache.Add("Item 1", (decimal)10.5); var item = (decimal)externalCache.Get("Item 1"); Assert.AreEqual((decimal)10.5, item); } }
public void TestAddStringAndGet() { using (var externalCache = new ExternalCache()) { externalCache.Add("Item 1", "Value 1"); var item = (string)externalCache.Get("Item 1"); Assert.AreEqual("Value 1", item); } }
public void TestAddOrGetExistingString() { using (var externalCache = new ExternalCache()) { externalCache.Add("Item 1", "Value 1"); var item = (string)externalCache.AddOrGetExisting("Item 1", "Value 2"); Assert.AreEqual("Value 1", item); } }
public void TestAddObjectAndLetItExpireByAbsoluteTime() { using (var externalCache = new ExternalCache()) { var item = new Dynamo(); item["P1"] = "Property 1"; externalCache.Add("Item 1", item, DateTime.Now.AddMilliseconds(1000)); CheckItem1(externalCache); } }
public void TestAddObjectAndLetItExpireByTicks() { using (var externalCache = new ExternalCache()) { var item = new Dynamo(); item["P1"] = "Property 1"; externalCache.Add("Item 1", item, new TimeSpan(10000000)); // 10000 ticks = 1ms CheckItem1(externalCache); } }
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"]); } }
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 TestAdd10000StringsAndGet() { using (var externalCache = new ExternalCache()) { // ReSharper disable once InconsistentNaming for (var i = 0; i < 10000; i++) { externalCache.Add($"Item {i}", $"Hello {i}"); var item = (string)externalCache.Get($"Item {i}"); Assert.AreEqual($"Hello {i}", item); } } }
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 TestAddOrGetExisting() { using (var externalCache = new ExternalCache()) { // ReSharper disable once InconsistentNaming var _item = new Dynamo(); _item["P1"] = "Item 1"; externalCache.Add("Item 1", _item); Dynamo item = new Dynamo(); item["P1"] = "Item 2"; item = (Dynamo)externalCache.AddOrGetExisting("Item 1", item); Assert.AreEqual("Item 1", item["P1"]); } }
public void TestAddObjectAndEnumerate() { 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); foreach (var item in externalCache) { Assert.AreEqual("Property 1", ((dynamic)item.Value).P1); Assert.AreEqual("Property 2", ((dynamic)item.Value).P2); } } }
public void TestClear() { using (var externalCache = new ExternalCache()) { // ReSharper disable once InconsistentNaming var _item = new Dynamo(); _item["P1"] = "Property 1"; externalCache.Add("Item 1", _item); var item = (Dynamo)externalCache.Get("Item 1"); Assert.AreEqual("Property 1", item["P1"]); externalCache.Clear(); item = (Dynamo)externalCache.Get("Item 1"); Assert.IsNull(item); } }
public void TestAdd10000ObjectsAndGet() { using (var externalCache = new ExternalCache()) { // ReSharper disable once InconsistentNaming for (var i = 0; i < 10000; i++) { var _item = new Dynamo(); _item["P1"] = "Property 1"; _item["P2"] = i; externalCache.Add($"Item {i}", _item); var item = (Dynamo)externalCache.Get($"Item {i}"); Assert.AreEqual("Property 1", item["P1"]); Assert.AreEqual(i, item["P2"]); } } }
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); }
public void TestAddArrayWith100000StringsAsJsonAndGet() { using (var externalCache = new ExternalCache()) { // ReSharper disable once InconsistentNaming var arr = new dynamic[100000]; for (var i = 0; i < 100000; i++) { arr[i] = new ExpandoObject(); arr[i].P1 = i; arr[i].P2 = "Hello World"; } var json = JsonSerializer.Serialize(arr); externalCache.Add($"Item", json); var item = (byte[])externalCache.Get($"Item"); // ReSharper disable once CoVariantArrayConversion var objs = (dynamic[])JsonSerializer.Deserialize <ExpandoObject[]>(item); for (var i = 0; i < 100000; i++) { Assert.AreEqual(objs[i].P1, i); Assert.AreEqual(objs[i].P2, "Hello World"); } } }
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(); }