public void StressZeroTest() { ParallelTestHelper.Repeat(delegate { int times = 0; ParallelTestHelper.ParallelStressTest(snzi, (s) => { int t = Interlocked.Increment(ref times); for (int i = 0; i < 20; i++) { if (i % 2 == 0) { snzi.Increment(); } else { snzi.Decrement(); } if (i % (3 * t) == 0) { Thread.Sleep(0); } } }); Assert.IsTrue(snzi.IsSet, "#1"); }); }
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(), 3)) { ; } }, 4); Assert.AreEqual(7, map.Count); int value; Assert.IsTrue(map.TryGetValue("monkey1", out value), "#1"); Assert.AreEqual(3, value, "#1"); Assert.IsTrue(map.TryGetValue("monkey2", out value), "#2"); Assert.AreEqual(3, value, "#2"); Assert.IsTrue(map.TryGetValue("monkey3", out value), "#3"); Assert.AreEqual(3, value, "#3"); Assert.IsTrue(map.TryGetValue("monkey4", out value), "#4"); Assert.AreEqual(3, value, "#4"); }); }
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 WaitTest() { int count = 0; bool s = false; ParallelTestHelper.ParallelStressTest(mre, delegate(ManualResetEventSlim m) { if (Interlocked.Increment(ref count) % 2 == 0) { Thread.Sleep(50); for (int i = 0; i < 10; i++) { if (i % 2 == 0) { m.Reset(); } else { m.Set(); } } } else { m.Wait(); s = true; } }); Assert.IsTrue(s, "#1"); Assert.IsTrue(mre.IsSet, "#2"); }
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 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++) { coll.TryAdd(i); } bool state = true; ParallelTestHelper.ParallelStressTest(coll, (q) => { int t; for (int i = 0; i < count; i++) { state &= coll.TryTake(out t); } }, threads); 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"); } }); }
public void WaitStressTest() { int count = -1; bool[] array = new bool[7]; ParallelTestHelper.ParallelStressTest(sem, delegate(SemaphoreSlim s) { int index = Interlocked.Increment(ref count); s.Wait(); Thread.Sleep(40); s.Release(); array[index] = true; }, 7); bool result = array.Aggregate((acc, e) => acc && e); Assert.IsTrue(result, "#1"); Assert.AreEqual(5, sem.CurrentCount, "#2"); }
public void AddCountSignalStressTestCase() { int count = 0; ParallelTestHelper.ParallelStressTest(evt, delegate(CountdownEvent e) { int num = Interlocked.Increment(ref count); if (num % 2 == 0) { e.AddCount(); } else { e.Signal(); } }, 7); Assert.AreEqual(4, evt.CurrentCount, "#1"); Assert.IsFalse(evt.IsSet, "#2"); }
public void StressNonZeroTest() { ParallelTestHelper.Repeat(delegate { ParallelTestHelper.ParallelStressTest(snzi, (s) => { snzi.Increment(); for (int i = 0; i < 1; i++) { if (i % 2 == 0) { snzi.Increment(); } else { snzi.Decrement(); } } }); Assert.IsFalse(snzi.IsSet, "#1"); }); }
public void RemoveParallelTest() { ParallelTestHelper.Repeat(delegate { Setup(); int index = 0; bool r1 = false, r2 = false, r3 = false; int val; ParallelTestHelper.ParallelStressTest(map, delegate { int own = Interlocked.Increment(ref index); switch (own) { case 1: r1 = map.TryRemove("foo", out val); break; case 2: r2 = map.TryRemove("bar", out val); break; case 3: r3 = map.TryRemove("foobar", out val); 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), "#1"); Assert.IsFalse(map.TryGetValue("bar", out value), "#2"); Assert.IsFalse(map.TryGetValue("foobar", out value), "#3"); }); }
public void WaitTestCase() { int count = 0; bool s = false; ParallelTestHelper.ParallelStressTest(evt, delegate(CountdownEvent e) { if (Interlocked.Increment(ref count) % 2 == 0) { Thread.Sleep(100); while (!e.IsSet) { e.Signal(); } } else { e.Wait(); s = true; } }); Assert.IsTrue(s, "#1"); Assert.IsTrue(evt.IsSet, "#2"); }