public void DependenciesTest3() { InMemoryCache imc = new InMemoryCache(); imc.PutItem("A", "A1", new string[] { "set1" }, TimeSpan.Zero, DateTime.MaxValue); imc.PutItem("B", "B1", new string[] { "set1", "set2" }, TimeSpan.Zero, DateTime.MaxValue); imc.PutItem("C", "C1", new string[] { "set2", "set3" }, TimeSpan.Zero, DateTime.MaxValue); imc.PutItem("D", "D1", new string[] { "set3", "set1" }, TimeSpan.Zero, DateTime.MaxValue); Assert.AreEqual("D|C|B|A", GetItemKeysInLruOrder(imc)); imc.InvalidateSets(new string[] { "set77" }); Assert.AreEqual("D|C|B|A", GetItemKeysInLruOrder(imc)); }
public void MicroStressTest() { // run a bunch of concurrent reads and writes, make sure we get no exceptions var imc = new InMemoryCache(100); int numberOfRequestBatches = 50; // will be multiplied by 5 (3 readers + 1 writer + 1 invalidator) int numberOfIterationsPerThread = 10000; ManualResetEvent startEvent = new ManualResetEvent(false); Action writer = () => { startEvent.WaitOne(); Random random = new Random(); for (int i = 0; i < numberOfIterationsPerThread; ++i) { string randomKey = Guid.NewGuid().ToString("N").Substring(0, 4); string randomValue = randomKey + "_V"; List<string> dependentSets = new List<string>(); int numberOfDependencies = random.Next(5); for (int j = 0; j < numberOfDependencies; ++j) { string randomSetName = new string((char)('A' + random.Next(26)), 1); dependentSets.Add(randomSetName); } imc.PutItem(randomKey, randomValue, dependentSets, TimeSpan.Zero, DateTime.MaxValue); } }; Action invalidator = () => { startEvent.WaitOne(); Random random = new Random(); for (int i = 0; i < numberOfIterationsPerThread; ++i) { List<string> dependentSets = new List<string>(); int numberOfDependencies = random.Next(5); for (int j = 0; j < numberOfDependencies; ++j) { string randomSetName = new string((char)('A' + random.Next(26)), 1); dependentSets.Add(randomSetName); } imc.InvalidateSets(dependentSets); } }; Action reader = () => { startEvent.WaitOne(); Random random = new Random(); for (int i = 0; i < numberOfIterationsPerThread; ++i) { string randomKey = Guid.NewGuid().ToString("N").Substring(0, 4); object value; if (imc.GetItem(randomKey, out value)) { Assert.AreEqual(randomKey + "_V", value); } } }; List<Thread> threads = new List<Thread>(); for (int i = 0; i < numberOfRequestBatches; ++i) { threads.Add(new Thread(() => writer())); threads.Add(new Thread(() => invalidator())); threads.Add(new Thread(() => reader())); threads.Add(new Thread(() => reader())); threads.Add(new Thread(() => reader())); } foreach (Thread t in threads) { t.Start(); } startEvent.Set(); foreach (Thread t in threads) { t.Join(); } }
public void MicroStressTest() { // run a bunch of concurrent reads and writes, make sure we get no exceptions var imc = new InMemoryCache(100); int numberOfRequestBatches = 50; // will be multiplied by 5 (3 readers + 1 writer + 1 invalidator) int numberOfIterationsPerThread = 10000; ManualResetEvent startEvent = new ManualResetEvent(false); Action writer = () => { startEvent.WaitOne(); Random random = new Random(); for (int i = 0; i < numberOfIterationsPerThread; ++i) { string randomKey = Guid.NewGuid().ToString("N").Substring(0, 4); string randomValue = randomKey + "_V"; List <string> dependentSets = new List <string>(); int numberOfDependencies = random.Next(5); for (int j = 0; j < numberOfDependencies; ++j) { string randomSetName = new string((char)('A' + random.Next(26)), 1); dependentSets.Add(randomSetName); } imc.PutItem(randomKey, randomValue, dependentSets, TimeSpan.Zero, DateTime.MaxValue); } }; Action invalidator = () => { startEvent.WaitOne(); Random random = new Random(); for (int i = 0; i < numberOfIterationsPerThread; ++i) { List <string> dependentSets = new List <string>(); int numberOfDependencies = random.Next(5); for (int j = 0; j < numberOfDependencies; ++j) { string randomSetName = new string((char)('A' + random.Next(26)), 1); dependentSets.Add(randomSetName); } imc.InvalidateSets(dependentSets); } }; Action reader = () => { startEvent.WaitOne(); Random random = new Random(); for (int i = 0; i < numberOfIterationsPerThread; ++i) { string randomKey = Guid.NewGuid().ToString("N").Substring(0, 4); object value; if (imc.GetItem(randomKey, out value)) { Assert.AreEqual(randomKey + "_V", value); } } }; List <Thread> threads = new List <Thread>(); for (int i = 0; i < numberOfRequestBatches; ++i) { threads.Add(new Thread(() => writer())); threads.Add(new Thread(() => invalidator())); threads.Add(new Thread(() => reader())); threads.Add(new Thread(() => reader())); threads.Add(new Thread(() => reader())); } foreach (Thread t in threads) { t.Start(); } startEvent.Set(); foreach (Thread t in threads) { t.Join(); } }