public void TestSetAndGet() { var comPlusCache = new ComPlusCache("TestCache"); comPlusCache.Set("Entry 1", "Hello", new DateTime(9999, 1, 1)); Assert.AreEqual("Hello", comPlusCache.Get("Entry 1")); comPlusCache.Set("Entry 2", "Hello 2", new TimeSpan(365, 0, 0, 0)); Assert.AreEqual("Hello 2", comPlusCache.Get("Entry 2")); comPlusCache.Set("Entry 3", new object(), new DateTime(9999, 1, 1)); Assert.IsTrue(comPlusCache.Contains("Entry 3")); comPlusCache.Set("Entry 4", new object(), new TimeSpan(365, 0, 0, 0)); Assert.IsTrue(comPlusCache.Contains("Entry 4")); }
public void TestStressConcurrent() { 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 => { var externalCache = new ComPlusCache(); for (var j = 0; j < loops; j++) { var item = new Dynamo(); var item2 = new Dynamo(); item["P1"] = "Property " + j; externalCache.AddOrUpdate($"Item {(int) context}-{j}", () => item, () => item2); Assert.IsTrue(externalCache.Contains($"Item {(int) context}-{j}")); var returnedItem = (Dynamo)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}")); externalCache.AddOrUpdate($"Item {j}", () => item, () => item2); Assert.IsTrue(externalCache.Contains($"Item {j}")); var item3 = externalCache.GetOrAdd($"Item {j}", () => item2); Assert.IsNotNull(item3); Interlocked.Increment(ref totalLoops); } })).Start(i); } foreach (var thread in threads) { thread.Join(); } Assert.AreEqual(threadCount * loops, totalLoops); }