public void ContinueWithOnAbortedTestCase() { ParallelTestHelper.Repeat(delegate { bool result = false; bool taskResult = false; CancellationTokenSource src = new CancellationTokenSource(); Task t = new Task(delegate { taskResult = true; }, src.Token); src.Cancel(); Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.ExecuteSynchronously); t.Start(); cont.Wait(); Assert.IsFalse(taskResult, "#-1"); Assert.AreEqual(TaskStatus.Canceled, t.Status, "#0"); Assert.IsTrue(t.IsCanceled, "#0bis"); Assert.IsNull(cont.Exception, "#1"); Assert.IsNotNull(cont, "#2"); Assert.IsTrue(result, "#3"); }); }
[Category("RaceCondition")] // This test creates a race condition public void ContinueWithOnAnyTestCase() { ParallelTestHelper.Repeat ( delegate { var result = false; var t = Task.Factory.StartNew ( delegate { // Empty } ); var cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.None); Assert.IsTrue(t.Wait(2000), "First wait, (status, {0})", t.Status); Assert.IsTrue(cont.Wait(2000), "Cont wait, (result, {0}) (parent status, {2}) (status, {1})", result, cont.Status, t.Status); Assert.IsNull(cont.Exception, "#1"); Assert.IsNotNull(cont, "#2"); Assert.IsTrue(result, "#3"); } ); }
public void MultipleTasks() { ParallelTestHelper.Repeat(delegate { bool r1 = false, r2 = false, r3 = false; Task t1 = Task.Factory.StartNew(delegate { r1 = true; }); Task t2 = Task.Factory.StartNew(delegate { r2 = true; }); Task t3 = Task.Factory.StartNew(delegate { r3 = true; }); t1.Wait(2000); t2.Wait(2000); t3.Wait(2000); Assert.IsTrue(r1, "#1"); Assert.IsTrue(r2, "#2"); Assert.IsTrue(r3, "#3"); }, 100); }
public void DoubleWaitTest() { ParallelTestHelper.Repeat(delegate { Console.WriteLine("run"); var evt = new ManualResetEventSlim(); var t = Task.Factory.StartNew(() => evt.Wait(2000)); var cntd = new CountdownEvent(2); bool r1 = false, r2 = false; ThreadPool.QueueUserWorkItem(delegate { cntd.Signal(); r1 = t.Wait(1000); Console.WriteLine("out 1 {0}", r1); cntd.Signal(); }); ThreadPool.QueueUserWorkItem(delegate { cntd.Signal(); r2 = t.Wait(1000); Console.WriteLine("out 2 {0}", r2); cntd.Signal(); }); cntd.Wait(2000); cntd.Reset(); evt.Set(); cntd.Wait(2000); Assert.IsTrue(r1); Assert.IsTrue(r2); }, 5); }
[Category("RaceCondition")] // This test creates a race condition public void ContinueWithOnCompletedSuccessfullyTestCase() { ParallelTestHelper.Repeat ( delegate { var result = false; var t = Task.Factory.StartNew ( delegate { // Empty } ); var cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion); Assert.IsTrue(t.Wait(1000), "#4"); Assert.IsTrue(cont.Wait(1000), "#5"); Assert.IsNull(cont.Exception, "#1"); Assert.IsNotNull(cont, "#2"); Assert.IsTrue(result, "#3"); } ); }
public void WaitAnyTest() { ParallelTestHelper.Repeat(delegate { int flag = 0; int finished = 0; InitWithDelegate(delegate { int times = Interlocked.Exchange(ref flag, 1); if (times == 1) { SpinWait sw = new SpinWait(); while (finished == 0) { sw.SpinOnce(); } } else { Interlocked.Increment(ref finished); } }); int index = Task.WaitAny(tasks); Assert.AreNotEqual(-1, index, "#3"); Assert.AreEqual(1, flag, "#1"); Assert.AreEqual(1, finished, "#2"); Task.WaitAll(tasks); }); }
public void ContinueWithChildren() { ParallelTestHelper.Repeat(delegate { var result = false; var t = Task.Factory.StartNew ( () => Task.Factory.StartNew ( () => { // Empty }, TaskCreationOptions.AttachedToParent ) ); var manualResetEvents = new ManualResetEvent[1]; using (manualResetEvents[0] = new ManualResetEvent(false)) { t.ContinueWith ( _ => { result = true; manualResetEvents[0].Set(); } ); Assert.IsTrue(manualResetEvents[0].WaitOne(1000), "#1"); Assert.IsTrue(result, "#2"); } }, 2); }
public void WaitChildTestCase() { ParallelTestHelper.Repeat(delegate { bool r1 = false, r2 = false, r3 = false; Task t = Task.Factory.StartNew(delegate { Task.Factory.StartNew(delegate { Thread.Sleep(50); r1 = true; }, TaskCreationOptions.AttachedToParent); Task.Factory.StartNew(delegate { Thread.Sleep(300); r2 = true; }, TaskCreationOptions.AttachedToParent); Task.Factory.StartNew(delegate { Thread.Sleep(150); r3 = true; }, TaskCreationOptions.AttachedToParent); }); Assert.IsTrue(t.Wait(2000), "#0"); Assert.IsTrue(r2, "#1"); Assert.IsTrue(r3, "#2"); Assert.IsTrue(r1, "#3"); Assert.AreEqual(TaskStatus.RanToCompletion, t.Status, "#4"); }, 10); }
public void WaitChildTestCase() { ParallelTestHelper.Repeat (delegate { bool r1 = false, r2 = false, r3 = false; var mre = new ManualResetEventSlim (false); var mreStart = new ManualResetEventSlim (false); Task t = Task.Factory.StartNew(delegate { Task.Factory.StartNew(delegate { mre.Wait (300); r1 = true; }, TaskCreationOptions.AttachedToParent); Task.Factory.StartNew(delegate { r2 = true; }, TaskCreationOptions.AttachedToParent); Task.Factory.StartNew(delegate { r3 = true; }, TaskCreationOptions.AttachedToParent); mreStart.Set (); }); mreStart.Wait (300); Assert.IsFalse (t.Wait (10), "#0a"); mre.Set (); Assert.IsTrue (t.Wait (500), "#0b"); Assert.IsTrue(r2, "#1"); Assert.IsTrue(r3, "#2"); Assert.IsTrue(r1, "#3"); Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#4"); }, 10); }
public void ContinueWithChildren() // TODO: Review { ParallelTestHelper.Repeat(delegate { var result = false; var t = Task.Factory.StartNew(() => Task.Factory.StartNew(() => { }, TaskCreationOptions.AttachedToParent)); using (var mre = new ManualResetEvent(false)) { t.ContinueWith ( l => { result = true; mre.Set(); } ); Assert.IsTrue(mre.WaitOne(1000), "#1"); Assert.IsTrue(result, "#2"); } }, 2); }
public void ParallelForSmallRangeTest() { ParallelTestHelper.Repeat(() => { int test = -1; Parallel.For(0, 1, (i) => test = i); Assert.AreEqual(0, test, "#1"); }); }
public void WaitAllTest() { ParallelTestHelper.Repeat(delegate { int achieved = 0; InitWithDelegate(delegate { Interlocked.Increment(ref achieved); }); Task.WaitAll(tasks); Assert.AreEqual(max, achieved, "#1"); }); }
public void NestedFutureTest() { ParallelTestHelper.Repeat(delegate { var t = CreateNestedFuture(10); var t2 = CreateNestedFuture(100); var t3 = CreateNestedFuture(100); Assert.AreEqual(11, t.Result); Assert.AreEqual(101, t2.Result); Assert.AreEqual(101, t3.Result); }, 50); }
public void ContinueWithOnFailedTestCase() { ParallelTestHelper.Repeat(delegate { bool result = false; Task t = Task.Factory.StartNew(delegate { throw new Exception("foo"); }); Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnFaulted); Assert.IsTrue(cont.Wait(1000), "#0"); Assert.IsNotNull(t.Exception, "#1"); Assert.IsNotNull(cont, "#2"); Assert.IsTrue(result, "#3"); }); }
public void ParallelForTestCase() { int[] expected = Enumerable.Range(1, 1000).Select((e) => e * 2).ToArray(); ParallelTestHelper.Repeat(() => { int[] actual = Enumerable.Range(1, 1000).ToArray(); SpinWait sw = new SpinWait(); Parallel.For(0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce(); }); CollectionAssert.AreEquivalent(expected, actual, "#1, same"); CollectionAssert.AreEqual(expected, actual, "#2, in order"); }); }
public void ParallelForEachTestCase() { ParallelTestHelper.Repeat(() => { IEnumerable <int> e = Enumerable.Repeat(1, 500); ConcurrentQueue <int> queue = new ConcurrentQueue <int> (); SpinWait sw = new SpinWait(); int count = 0; Parallel.ForEach(e, (element) => { Interlocked.Increment(ref count); queue.Enqueue(element); sw.SpinOnce(); }); Assert.AreEqual(500, count, "#1"); CollectionAssert.AreEquivalent(e, queue, "#2"); }); }
public void ContinueWithChildren() { ParallelTestHelper.Repeat(delegate { bool result = false; var t = Task.Factory.StartNew(() => Task.Factory.StartNew(() => Thread.Sleep(100), TaskCreationOptions.AttachedToParent)); t.ContinueWith(_ => result = true); while (!t.IsCompleted) { Thread.Sleep(200); } Assert.IsTrue(result); }, 2); }
public void ContinueWithOnCompletedSuccessfullyTestCase() { ParallelTestHelper.Repeat(delegate { bool result = false; Task t = Task.Factory.StartNew(delegate { }); Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion); t.Wait(); cont.Wait(); Assert.IsNull(cont.Exception, "#1"); Assert.IsNotNull(cont, "#2"); Assert.IsTrue(result, "#3"); }); }
public void ContinueWithOnFailedTestCase() { ParallelTestHelper.Repeat ( () => { var result = false; var t = Task.Factory.StartNew(() => { throw new Exception("foo"); }); var cont = t.ContinueWith(_ => result = true, TaskContinuationOptions.OnlyOnFaulted); Assert.IsTrue(cont.Wait(1000), "#0"); Assert.IsNotNull(t.Exception, "#1"); Assert.IsNotNull(cont, "#2"); Assert.IsTrue(result, "#3"); } ); }
public void DoubleWaitTest () { ParallelTestHelper.Repeat (delegate { var evt = new ManualResetEventSlim (); var t = Task.Factory.StartNew (() => evt.Wait (5000)); var cntd = new CountdownEvent (2); var cntd2 = new CountdownEvent (2); bool r1 = false, r2 = false; ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r1 = t.Wait (1000) && t.Result; cntd2.Signal (); }); ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r2 = t.Wait (1000) && t.Result; cntd2.Signal (); }); Assert.IsTrue (cntd.Wait (2000), "#1"); evt.Set (); Assert.IsTrue (cntd2.Wait (2000), "#2"); Assert.IsTrue (r1, "r1"); Assert.IsTrue (r2, "r2"); }, 10); }
public void DoubleWaitTest() { ParallelTestHelper.Repeat(delegate { var evt = new ManualResetEventSlim(); var monitor = new object(); int finished = 0; var t = Task.Factory.StartNew(delegate { var r = evt.Wait(5000); lock (monitor) { finished++; Monitor.Pulse(monitor); } return(r ? 1 : 10); //1 -> ok, 10 -> evt wait failed }); var cntd = new CountdownEvent(2); var cntd2 = new CountdownEvent(2); int r1 = 0, r2 = 0; ThreadPool.QueueUserWorkItem(delegate { cntd.Signal(); if (!t.Wait(1000)) { r1 = 20; // 20 -> task wait failed } else if (t.Result != 1) { r1 = 30 + t.Result; // 30 -> task result is bad } else { r1 = 2; //2 -> ok } cntd2.Signal(); lock (monitor) { finished++; Monitor.Pulse(monitor); } }); ThreadPool.QueueUserWorkItem(delegate { cntd.Signal(); if (!t.Wait(1000)) { r2 = 40; // 40 -> task wait failed } else if (t.Result != 1) { r2 = 50 + t.Result; // 50 -> task result is bad } else { r2 = 3; //3 -> ok } cntd2.Signal(); lock (monitor) { finished++; Monitor.Pulse(monitor); } }); Assert.IsTrue(cntd.Wait(2000), "#1"); evt.Set(); Assert.IsTrue(cntd2.Wait(2000), "#2"); Assert.AreEqual(2, r1, "r1"); Assert.AreEqual(3, r2, "r2"); // Wait for everything to finish to avoid overloading the tpool lock (monitor) { while (true) { if (finished == 3) { break; } else { Monitor.Wait(monitor); } } } }, 10); }