public static void ParallelAdder(IProducerConsumerCollection <int> collection, int numThread) { int startIndex = -10; ParallelTestHelper.ParallelStressTest(collection, delegate(IProducerConsumerCollection <int> c) { int start = Interlocked.Add(ref startIndex, 10); for (int i = start; i < start + 10; i++) { c.TryAdd(i); } }, numThread); }
public void CountTestCase() { const int numThread = 5; ParallelTestHelper.ParallelAdder(queue, numThread); Assert.AreEqual(10 * numThread, queue.Count, "#1"); int value; queue.TryPeek(out value); ParallelTestHelper.ParallelRemover(queue, numThread, 3); Assert.AreEqual(10 * numThread - 3, queue.Count, "#2"); }
public static void ParallelRemover(IProducerConsumerCollection <int> collection, int numThread, int times) { int t = -1; ParallelTestHelper.ParallelStressTest(collection, delegate(IProducerConsumerCollection <int> c) { int num = Interlocked.Increment(ref t); int value; if (num < times) { c.TryTake(out value); } }, numThread); }
public void RemoveParallelTest() { ParallelTestHelper.Repeat(delegate { Setup(); var index = 0; var r1 = false; var r2 = false; var r3 = false; ParallelTestHelper.ParallelStressTest ( _map, delegate { var own = Interlocked.Increment(ref index); switch (own) { case 1: r1 = _map.TryRemove("foo", out _); break; case 2: r2 = _map.TryRemove("bar", out _); break; case 3: r3 = _map.TryRemove("foobar", out _); break; default: break; } }, 3 ); Assert.AreEqual(0, _map.Count); int value; Assert.IsTrue(r1, "1"); Assert.IsTrue(r2, "2"); Assert.IsTrue(r3, "3"); Assert.IsFalse(_map.TryGetValue("foo", out value), "#1b " + value.ToString()); Assert.IsFalse(_map.TryGetValue("bar", out value), "#2b"); Assert.IsFalse(_map.TryGetValue("foobar", out value), "#3b"); }); }
public void CountTestCase() { const int numThread = 5; ParallelTestHelper.ParallelAdder(stack, numThread); Assert.AreEqual(10 * numThread, stack.Count, "#1"); int value; stack.TryPeek(out value); ParallelTestHelper.ParallelRemover(stack, numThread, 3); Assert.AreEqual(10 * numThread - 3, stack.Count, "#2"); stack.Clear(); Assert.AreEqual(0, stack.Count, "#3"); Assert.IsTrue(stack.IsEmpty, "#4"); }
public static void RemoveStressTest(IProducerConsumerCollection <int> coll, CheckOrderingType order) { ParallelTestHelper.Repeat(delegate { const int count = 10; const int threads = 5; const int delta = 5; for (int i = 0; i < (count + delta) * threads; i++) { while (!coll.TryAdd(i)) { ; } } bool state = true; bool ran = false; Assert.AreEqual((count + delta) * threads, coll.Count, "#0"); ParallelTestHelper.ParallelStressTest(coll, (q) => { ran = true; bool s = true; int t; for (int i = 0; i < count; i++) { s &= coll.TryTake(out t); // try again in case it was a transient failure if (!s && coll.TryTake(out t)) { s = true; } } if (!s) { state = false; } }, threads); Assert.IsTrue(ran, "#1-pre"); Assert.IsTrue(state, "#1"); Assert.AreEqual(delta * threads, coll.Count, "#2"); string actual = string.Empty; int temp; while (coll.TryTake(out temp)) { actual += temp.ToString();; } IEnumerable <int> range = Enumerable.Range(order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads); if (order == CheckOrderingType.Reversed) { range = range.Reverse(); } string expected = range.Aggregate(string.Empty, (acc, v) => acc + v); if (order == CheckOrderingType.DontCare) { CollectionAssert.AreEquivalent(expected, actual, "#3"); } else { Assert.AreEqual(expected, actual, "#3"); } }, 1000); }
public static void RemoveStressTest(IProducerConsumerCollection <int> coll, CheckOrderingType order) { ParallelTestHelper.Repeat(delegate { const int Count = 10; const int Threads = 5; const int Delta = 5; for (var i = 0; i < (Count + Delta) * Threads; i++) { while (!coll.TryAdd(i)) { } } var state = true; Assert.AreEqual((Count + Delta) * Threads, coll.Count, "#0"); ParallelTestHelper.ParallelStressTest(coll, (q) => { var s = true; int t; for (var i = 0; i < Count; i++) { s &= coll.TryTake(out t); // try again in case it was a transient failure if (!s && coll.TryTake(out t)) { s = true; } } state &= s; }, Threads); Assert.IsTrue(state, "#1"); Assert.AreEqual(Delta * Threads, coll.Count, "#2"); var actual = string.Empty; int temp; var builder = new StringBuilder(); builder.Append(actual); while (coll.TryTake(out temp)) { builder.Append(temp.ToString()); } actual = builder.ToString(); var range = Enumerable.Range(order == CheckOrderingType.Reversed ? 0 : Count * Threads, Delta * Threads); if (order == CheckOrderingType.Reversed) { range = range.Reverse(); } var expected = range.Aggregate(string.Empty, (acc, v) => acc + v); if (order == CheckOrderingType.DontCare) { Assert.That(actual, new CollectionEquivalentConstraint(expected), "#3"); } else { Assert.AreEqual(expected, actual, "#3"); } }, 10); }