public static void AddStressTest(IProducerConsumerCollection <int> coll) { ParallelTestHelper.Repeat(delegate { int amount = -1; const int count = 10; const int threads = 5; ParallelTestHelper.ParallelStressTest(coll, (q) => { int t = Interlocked.Increment(ref amount); for (int i = 0; i < count; i++) { coll.TryAdd(t); } }, threads); Assert.AreEqual(threads * count, coll.Count, "#-1"); int[] values = new int[threads]; int temp; while (coll.TryTake(out temp)) { values[temp]++; } for (int i = 0; i < threads; i++) { Assert.AreEqual(count, values[i], "#" + i); } }); }
public void StressTryPeekTestCase() { ParallelTestHelper.Repeat(delegate { var queue = new ConcurrentQueue <object> (); queue.Enqueue(new object()); const int threads = 10; int threadCounter = 0; bool success = true; ParallelTestHelper.ParallelStressTest(queue, (q) => { int threadId = Interlocked.Increment(ref threadCounter); object temp; if (threadId < threads) { while (queue.TryPeek(out temp)) { if (temp == null) { success = false; } } } else { queue.TryDequeue(out temp); } }, threads); Assert.IsTrue(success, "TryPeek returned unexpected null value."); }, 10); }
public void AddParallelWithoutDuplicateTest() { ParallelTestHelper.Repeat(delegate { Setup(); int index = 0; ParallelTestHelper.ParallelStressTest(map, delegate { int own = Interlocked.Increment(ref index); while (!map.TryAdd("monkey" + own.ToString(), own)) { ; } }, 4); Assert.AreEqual(7, map.Count); int value; Assert.IsTrue(map.TryGetValue("monkey1", out value), "#1"); Assert.AreEqual(1, value, "#1b"); Assert.IsTrue(map.TryGetValue("monkey2", out value), "#2"); Assert.AreEqual(2, value, "#2b"); Assert.IsTrue(map.TryGetValue("monkey3", out value), "#3"); Assert.AreEqual(3, value, "#3b"); Assert.IsTrue(map.TryGetValue("monkey4", out value), "#4"); Assert.AreEqual(4, value, "#4b"); }); }
public static void AddStressTest(IProducerConsumerCollection <int> coll) { ParallelTestHelper.Repeat(delegate { var amount = -1; const int Count = 10; const int Threads = 5; ParallelTestHelper.ParallelStressTest(coll, (q) => { var t = Interlocked.Increment(ref amount); for (var i = 0; i < Count; i++) { coll.TryAdd(t); } }, Threads); Assert.AreEqual(Threads * Count, coll.Count, "#-1"); var values = new int[Threads]; int temp; while (coll.TryTake(out temp)) { values[temp]++; } for (var i = 0; i < Threads; i++) { Assert.AreEqual(Count, values[i], "#" + i.ToString()); } }); }
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 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 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); }