[Category("RaceToDeadLock")] // This test creates a race condition, that when resolved sequentially will be stuck public void ManualResetEventSlim_SetAfterDisposeTest() { var mre = new ManualResetEventSlim(); ParallelTestHelper.Repeat(delegate { Exception disp = null, setting = null; var evt = new CountdownEvent(2); var evtFinish = new CountdownEvent(2); Task.Factory.StartNew(delegate { try { evt.Signal(); evt.Wait(1000); mre.Dispose(); } catch (Exception e) { disp = e; } evtFinish.Signal(); }); Task.Factory.StartNew(delegate { try { evt.Signal(); evt.Wait(1000); mre.Set(); } catch (Exception e) { setting = e; } evtFinish.Signal(); }); var bb = evtFinish.Wait(1000); if (!bb) { Assert.AreEqual(true, evtFinish.IsSet); } Assert.IsTrue(bb, "#0"); Assert.IsNull(disp, "#1"); Assert.IsNull(setting, "#2"); evt.Dispose(); evtFinish.Dispose(); }); }
public void SetAfterDisposeTest() { ParallelTestHelper.Repeat ( () => { var countdownEvent = new CountdownEvent[2]; using (countdownEvent[0] = new CountdownEvent(2)) { using (countdownEvent[1] = new CountdownEvent(2)) { Exception exception = null, setting = null; ThreadPool.QueueUserWorkItem(delegate { try { countdownEvent[0].Signal(); countdownEvent[0].Wait(1000); _mre.Dispose(); } catch (Exception e) { exception = e; } countdownEvent[1].Signal(); }); ThreadPool.QueueUserWorkItem(delegate { try { countdownEvent[0].Signal(); countdownEvent[0].Wait(1000); _mre.Set(); } catch (Exception e) { setting = e; } countdownEvent[1].Signal(); }); countdownEvent[1].Wait(); Assert.IsNull(exception, "#1"); Assert.IsNull(setting, "#2"); countdownEvent[0].Dispose(); countdownEvent[1].Dispose(); } } } ); }
public void SetAfterDisposeTest() { ParallelTestHelper.Repeat(delegate { Exception disp = null, setting = null; var evt = new CountdownEvent(2); var evtFinish = new CountdownEvent(2); ThreadPool.QueueUserWorkItem(delegate { try { evt.Signal(); evt.Wait(1000); _mre.Dispose(); } catch (Exception e) { disp = e; } evtFinish.Signal(); }); ThreadPool.QueueUserWorkItem(delegate { try { evt.Signal(); evt.Wait(1000); _mre.Set(); } catch (Exception e) { setting = e; } evtFinish.Signal(); }); var bb = evtFinish.Wait(1000); if (!bb) { Assert.AreEqual(true, evtFinish.IsSet); } Assert.IsTrue(bb, "#0"); Assert.IsNull(disp, "#1"); Assert.IsNull(setting, "#2"); evt.Dispose(); evtFinish.Dispose(); }); }
public void RemoveParallelTest() { ParallelTestHelper.Repeat ( () => { var map = Setup(); var index = 0; var r1 = false; var r2 = false; var r3 = false; ParallelTestHelper.ParallelStressTest ( () => { 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); Assert.IsTrue(r1, "1"); Assert.IsTrue(r2, "2"); Assert.IsTrue(r3, "3"); Assert.IsFalse(map.TryGetValue("foo", out var value), "#1b " + value); Assert.IsFalse(map.TryGetValue("bar", out value), "#2b"); Assert.IsFalse(map.TryGetValue("foobar", out value), "#3b"); } ); }
public void ConcurrentCancelLinkedTokenSourceWhileDisposing() { ParallelTestHelper.Repeat ( () => { var src = new CancellationTokenSource[1]; using (src[0] = new CancellationTokenSource()) { var linked = CancellationTokenSource.CreateLinkedTokenSource(src[0].Token); var countdownEvent = new CountdownEvent[1]; using (countdownEvent[0] = new CountdownEvent(2)) { var t1 = new Thread ( () => { if (!countdownEvent[0].Signal()) { countdownEvent[0].Wait(200); } src[0].Cancel(); } ); var t2 = new Thread ( () => { if (!countdownEvent[0].Signal()) { countdownEvent[0].Wait(200); } linked.Dispose(); } ); t1.Start(); t2.Start(); t1.Join(); t2.Join(); } } }, 500 ); }
public void SetAfterDisposeTest() // TODO:Review { ParallelTestHelper.Repeat(delegate { Exception disp = null, setting = null; var evt = new CountdownEvent(2); var evtFinish = new CountdownEvent(2); ThreadPool.QueueUserWorkItem(delegate { try { evt.Signal(); evt.Wait(1000); _mre.Dispose(); } catch (Exception e) { disp = e; } evtFinish.Signal(); }); ThreadPool.QueueUserWorkItem(delegate { try { evt.Signal(); evt.Wait(1000); _mre.Set(); } catch (Exception e) { setting = e; } evtFinish.Signal(); }); evtFinish.Wait(); Assert.IsNull(disp, "#1"); Assert.IsNull(setting, "#2"); evt.Dispose(); evtFinish.Dispose(); }); }
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 ConcurrentCancelLinkedTokenSourceWhileDisposing() // TODO: Review { ParallelTestHelper.Repeat(delegate { using (var src = new CancellationTokenSource()) { var linked = CancellationTokenSource.CreateLinkedTokenSource(src.Token); using (var cntd = new CountdownEvent(2)) { var t1 = new Thread(() => { if (!cntd.Signal()) { cntd.Wait(200); } src.Cancel(); }); var t2 = new Thread(() => { if (!cntd.Signal()) { cntd.Wait(200); } linked.Dispose(); }); t1.Start(); t2.Start(); #if TARGETS_NET || GREATERTHAN_NETCOREAPP11 || GREATERTHAN_NETSTANDARD16 t1.Join(500); t2.Join(500); #else t1.Join(); t2.Join(); #endif } } }, 500); }
public static void Main(string[] args) { int iterations = 200; if (args.Length > 0) { iterations = Int32.Parse(args [0]); } ParallelTestHelper.Repeat(delegate { int currentCount = 0; bool fail = false; SpinLockWrapper wrapper = new SpinLockWrapper(); ParallelTestHelper.ParallelStressTest(wrapper, delegate { bool taken = false; wrapper.Lock.Enter(ref taken); //wrapper.Lock.TryEnter (200,ref taken); int current = currentCount++; if (current != 0) { fail = true; } SpinWait sw = new SpinWait(); for (int i = 0; i < 200; i++) { sw.SpinOnce(); } currentCount -= 1; wrapper.Lock.Exit(); }, 4); if (fail) { Environment.Exit(1); } }, iterations); Environment.Exit(0); }
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 AddParallelWithoutDuplicateTest() { ParallelTestHelper.Repeat ( () => { var map = Setup(); var index = 0; ParallelTestHelper.ParallelStressTest ( () => { var own = Interlocked.Increment(ref index); while (!map.TryAdd("monkey" + own, own)) { // Empty } }, 4 ); Assert.AreEqual(7, map.Count); Assert.IsTrue(map.TryGetValue("monkey1", out var 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 void StressTryPeekTestCase() { ParallelTestHelper.Repeat ( delegate { var queue = new ConcurrentQueue <object>(); queue.Enqueue(new object()); const int Threads = 10; var threadCounter = 0; var success = true; ParallelTestHelper.ParallelStressTest ( () => { var threadId = Interlocked.Increment(ref threadCounter); object temp; if (threadId < Threads) { while (queue.TryPeek(out temp)) { success &= temp != null; } } else { queue.TryDequeue(out temp); } }, Threads ); Assert.IsTrue(success, "TryPeek returned unexpected null value."); }, 10 ); }
public void WaitTest() { var count = 0; var s = false; ParallelTestHelper.ParallelStressTest ( () => { if (Interlocked.Increment(ref count) % 2 == 0) { Thread.Sleep(50); for (var i = 0; i < 10; i++) { if (i % 2 == 0) { _mre.Reset(); } else { _mre.Set(); } } } else { _mre.Wait(); s = true; } }, 2 ); Assert.IsTrue(s, "#1"); Assert.IsTrue(_mre.IsSet, "#2"); }
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; } }, 3); Assert.IsTrue(s, "#1"); Assert.IsTrue(evt.IsSet, "#2"); }
public void ConcurrentCancelLinkedTokenSourceWhileDisposing() { ParallelTestHelper.Repeat(delegate { using (var src = new CancellationTokenSource()) { var linked = CancellationTokenSource.CreateLinkedTokenSource(src.Token); using (var cntd = new CountdownEvent(2)) { var t1 = new Thread(() => { if (!cntd.Signal()) { cntd.Wait(200); } src.Cancel(); }); var t2 = new Thread(() => { if (!cntd.Signal()) { cntd.Wait(200); } linked.Dispose(); }); t1.Start(); t2.Start(); t1.Join(500); t2.Join(500); } } }, 500); }
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; Assert.AreEqual((count + delta) * threads, coll.Count, "#0"); ParallelTestHelper.ParallelStressTest(coll, (q) => { 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(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); }