5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (c) 2008 Jérémie "Garuma" Laval
8 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 using System.Threading;
34 using System.Threading.Tasks;
35 using System.Collections.Generic;
36 using NUnit.Framework;
39 using NUnit.Framework.SyntaxHelpers;
42 namespace MonoTests.System.Threading.Tasks
45 public class TaskTests
47 class MockScheduler : TaskScheduler
49 public event Action<Task, bool> TryExecuteTaskInlineHandler;
51 protected override IEnumerable<Task> GetScheduledTasks ()
53 throw new NotImplementedException ();
56 protected override void QueueTask (Task task)
61 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
63 if (TryExecuteTaskInlineHandler != null)
64 TryExecuteTaskInlineHandler (task, taskWasPreviouslyQueued);
66 return base.TryExecuteTask (task);
70 class NonInlineableScheduler : TaskScheduler
72 protected override IEnumerable<Task> GetScheduledTasks ()
74 throw new NotImplementedException ();
77 protected override void QueueTask (Task task)
79 if (!base.TryExecuteTask (task))
80 throw new ApplicationException ();
83 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
89 class ExceptionScheduler : TaskScheduler
91 protected override IEnumerable<Task> GetScheduledTasks ()
93 throw new ApplicationException ("1");
96 protected override void QueueTask (Task task)
98 throw new ApplicationException ("2");
101 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
103 throw new ApplicationException ("3");
114 tasks = new Task[max];
117 void InitWithDelegate(Action action)
119 for (int i = 0; i < max; i++) {
120 tasks[i] = Task.Factory.StartNew(action);
125 public void WaitAnyTest()
127 ParallelTestHelper.Repeat (delegate {
131 InitWithDelegate(delegate {
132 int times = Interlocked.Exchange (ref flag, 1);
134 SpinWait sw = new SpinWait ();
135 while (finished == 0) sw.SpinOnce ();
137 Interlocked.Increment (ref finished);
141 int index = Task.WaitAny(tasks, 1000);
143 Assert.AreNotEqual (-1, index, "#3");
144 Assert.AreEqual (1, flag, "#1");
145 Assert.AreEqual (1, finished, "#2");
150 public void WaitAny_Empty ()
152 Assert.AreEqual (-1, Task.WaitAny (new Task[0]));
156 public void WaitAny_Zero ()
158 Assert.AreEqual (-1, Task.WaitAny (new[] { new Task (delegate { })}, 0), "#1");
159 Assert.AreEqual (-1, Task.WaitAny (new[] { new Task (delegate { }) }, 20), "#1");
163 public void WaitAny_Cancelled ()
165 var cancelation = new CancellationTokenSource ();
166 var tasks = new Task[] {
167 new Task (delegate { }),
168 new Task (delegate { }, cancelation.Token)
171 cancelation.Cancel ();
173 Assert.AreEqual (1, Task.WaitAny (tasks, 1000), "#1");
174 Assert.IsTrue (tasks[1].IsCompleted, "#2");
175 Assert.IsTrue (tasks[1].IsCanceled, "#3");
179 public void WaitAny_CancelledWithoutExecution ()
181 var cancelation = new CancellationTokenSource ();
182 var tasks = new Task[] {
183 new Task (delegate { }),
184 new Task (delegate { })
188 var mre = new ManualResetEventSlim (false);
189 ThreadPool.QueueUserWorkItem (delegate {
190 res = Task.WaitAny (tasks, 20);
194 cancelation.Cancel ();
195 Assert.IsTrue (mre.Wait (1000), "#1");
196 Assert.AreEqual (-1, res);
200 public void WaitAny_OneException ()
202 var mre = new ManualResetEventSlim (false);
203 var tasks = new Task[] {
204 Task.Factory.StartNew (delegate { mre.Wait (1000); }),
205 Task.Factory.StartNew (delegate { throw new ApplicationException (); })
208 Assert.AreEqual (1, Task.WaitAny (tasks, 1000), "#1");
209 Assert.IsFalse (tasks[0].IsCompleted, "#2");
210 Assert.IsTrue (tasks[1].IsFaulted, "#3");
216 public void WaitAny_SingleCanceled ()
218 var src = new CancellationTokenSource ();
219 var t = Task.Factory.StartNew (() => { Thread.Sleep (200); src.Cancel (); src.Token.ThrowIfCancellationRequested (); }, src.Token);
220 Assert.AreEqual (0, Task.WaitAny (new [] { t }));
223 public void WaitAny_ManyExceptions ()
225 CountdownEvent cde = new CountdownEvent (3);
227 Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
228 Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
229 Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } })
232 Assert.IsTrue (cde.Wait (1000), "#1");
235 Assert.IsTrue (Task.WaitAll (tasks, 1000), "#2");
236 } catch (AggregateException e) {
237 Assert.AreEqual (3, e.InnerExceptions.Count, "#3");
242 public void WaitAny_ManyCanceled ()
244 var cancellation = new CancellationToken (true);
246 Task.Factory.StartNew (delegate { }, cancellation),
247 Task.Factory.StartNew (delegate { }, cancellation),
248 Task.Factory.StartNew (delegate { }, cancellation)
252 Assert.IsTrue (Task.WaitAll (tasks, 1000), "#1");
253 } catch (AggregateException e) {
254 Assert.AreEqual (3, e.InnerExceptions.Count, "#2");
259 public void WaitAllTest ()
261 ParallelTestHelper.Repeat (delegate {
263 InitWithDelegate(delegate { Interlocked.Increment(ref achieved); });
265 Assert.AreEqual(max, achieved, "#1");
270 public void WaitAll_ManyTasks ()
272 for (int r = 0; r < 2000; ++r) {
273 var tasks = new Task[60];
275 for (int i = 0; i < tasks.Length; i++) {
276 tasks[i] = Task.Factory.StartNew (delegate { Thread.Sleep (0); });
279 Assert.IsTrue (Task.WaitAll (tasks, 2000));
284 public void WaitAll_Zero ()
286 Assert.IsFalse (Task.WaitAll (new Task[1] { new Task (delegate { }) }, 0), "#0");
287 Assert.IsFalse (Task.WaitAll (new Task[1] { new Task (delegate { }) }, 10), "#1");
291 public void WaitAll_WithExceptions ()
293 InitWithDelegate (delegate { throw new ApplicationException (); });
296 Task.WaitAll (tasks);
298 } catch (AggregateException e) {
299 Assert.AreEqual (6, e.InnerExceptions.Count, "#2");
302 Assert.IsNotNull (tasks[0].Exception, "#3");
306 public void WaitAll_TimeoutWithExceptionsAfter ()
308 CountdownEvent cde = new CountdownEvent (2);
309 var mre = new ManualResetEvent (false);
311 Task.Factory.StartNew (delegate { mre.WaitOne (); }),
312 Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
313 Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } })
316 Assert.IsTrue (cde.Wait (1000), "#1");
317 Assert.IsFalse (Task.WaitAll (tasks, 1000), "#2");
322 Assert.IsTrue (Task.WaitAll (tasks, 1000), "#3");
324 } catch (AggregateException e) {
325 Assert.AreEqual (2, e.InnerExceptions.Count, "#5");
330 public void WaitAll_TimeoutWithExceptionsBefore ()
332 CountdownEvent cde = new CountdownEvent (2);
333 var mre = new ManualResetEvent (false);
335 Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
336 Task.Factory.StartNew (delegate { try { throw new ApplicationException (); } finally { cde.Signal (); } }),
337 Task.Factory.StartNew (delegate { mre.WaitOne (); })
340 Assert.IsTrue (cde.Wait (1000), "#1");
341 Assert.IsFalse (Task.WaitAll (tasks, 1000), "#2");
346 Assert.IsTrue (Task.WaitAll (tasks, 1000), "#3");
348 } catch (AggregateException e) {
349 Assert.AreEqual (2, e.InnerExceptions.Count, "#5");
354 public void WaitAll_Cancelled ()
356 var cancelation = new CancellationTokenSource ();
357 var tasks = new Task[] {
358 new Task (delegate { cancelation.Cancel (); }),
359 new Task (delegate { }, cancelation.Token)
365 Task.WaitAll (tasks);
367 } catch (AggregateException e) {
368 var inner = (TaskCanceledException) e.InnerException;
369 Assert.AreEqual (tasks[1], inner.Task, "#2");
372 Assert.IsTrue (tasks[0].IsCompleted, "#3");
373 Assert.IsTrue (tasks[1].IsCanceled, "#4");
378 public void WaitAll_CancelledAndTimeout ()
380 var ct = new CancellationToken (true);
381 var t1 = new Task (() => {}, ct);
382 var t2 = Task.Delay (3000);
383 Assert.IsFalse (Task.WaitAll (new[] { t1, t2 }, 10));
388 public void WaitAllExceptionThenCancelled ()
390 var cancelation = new CancellationTokenSource ();
391 var tasks = new Task[] {
392 new Task (delegate { cancelation.Cancel (); throw new ApplicationException (); }),
393 new Task (delegate { }, cancelation.Token)
399 Task.WaitAll (tasks);
401 } catch (AggregateException e) {
402 Assert.That (e.InnerException, Is.TypeOf (typeof (ApplicationException)), "#2");
403 var inner = (TaskCanceledException) e.InnerExceptions[1];
404 Assert.AreEqual (tasks[1], inner.Task, "#3");
407 Assert.IsTrue (tasks[0].IsCompleted, "#4");
408 Assert.IsTrue (tasks[1].IsCanceled, "#5");
412 public void WaitAll_StartedUnderWait ()
414 var task1 = new Task (delegate { });
416 ThreadPool.QueueUserWorkItem (delegate {
417 // Sleep little to let task to start and hit internal wait
422 Assert.IsTrue (Task.WaitAll (new [] { task1 }, 1000), "#1");
426 public void CancelBeforeStart ()
428 var src = new CancellationTokenSource ();
430 Task t = new Task (delegate { }, src.Token);
432 Assert.AreEqual (TaskStatus.Canceled, t.Status, "#1");
437 } catch (InvalidOperationException) {
442 public void Wait_CancelledTask ()
444 var src = new CancellationTokenSource ();
446 Task t = new Task (delegate { }, src.Token);
452 } catch (AggregateException e) {
453 var details = (TaskCanceledException) e.InnerException;
454 Assert.AreEqual (t, details.Task, "#1e");
460 } catch (AggregateException e) {
461 var details = (TaskCanceledException) e.InnerException;
462 Assert.AreEqual (t, details.Task, "#2e");
463 Assert.IsNull (details.Task.Exception, "#2e2");
468 public void Wait_Inlined ()
470 bool? previouslyQueued = null;
472 var scheduler = new MockScheduler ();
473 scheduler.TryExecuteTaskInlineHandler += (task, b) => {
474 previouslyQueued = b;
477 var tf = new TaskFactory (scheduler);
478 var t = tf.StartNew (() => { });
481 Assert.AreEqual (true, previouslyQueued);
484 [Test, ExpectedException (typeof (InvalidOperationException))]
485 public void CreationWhileInitiallyCanceled ()
487 var token = new CancellationToken (true);
488 var task = new Task (() => { }, token);
489 Assert.AreEqual (TaskStatus.Canceled, task.Status);
494 public void ContinueWithInvalidArguments ()
496 var task = new Task (() => { });
498 task.ContinueWith (null);
500 } catch (ArgumentException) {
504 task.ContinueWith (delegate { }, null);
506 } catch (ArgumentException) {
510 task.ContinueWith (delegate { }, TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.NotOnCanceled);
512 } catch (ArgumentException) {
516 task.ContinueWith (delegate { }, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion);
518 } catch (ArgumentException) {
523 public void ContinueWithOnAnyTestCase()
525 ParallelTestHelper.Repeat (delegate {
528 Task t = Task.Factory.StartNew(delegate { });
529 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.None);
530 Assert.IsTrue (t.Wait (2000), "First wait, (status, {0})", t.Status);
531 Assert.IsTrue (cont.Wait(2000), "Cont wait, (result, {0}) (parent status, {2}) (status, {1})", result, cont.Status, t.Status);
532 Assert.IsNull(cont.Exception, "#1");
533 Assert.IsNotNull(cont, "#2");
534 Assert.IsTrue(result, "#3");
539 public void ContinueWithOnCompletedSuccessfullyTestCase()
541 ParallelTestHelper.Repeat (delegate {
544 Task t = Task.Factory.StartNew(delegate { });
545 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion);
546 Assert.IsTrue (t.Wait(1000), "#4");
547 Assert.IsTrue (cont.Wait(1000), "#5");
549 Assert.IsNull(cont.Exception, "#1");
550 Assert.IsNotNull(cont, "#2");
551 Assert.IsTrue(result, "#3");
556 public void ContinueWithOnAbortedTestCase()
559 bool taskResult = false;
561 CancellationTokenSource src = new CancellationTokenSource ();
562 Task t = new Task (delegate { taskResult = true; }, src.Token);
564 Task cont = t.ContinueWith (delegate { result = true; },
565 TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.ExecuteSynchronously);
569 Assert.AreEqual (TaskStatus.Canceled, t.Status, "#1a");
570 Assert.IsTrue (cont.IsCompleted, "#1b");
571 Assert.IsTrue (result, "#1c");
576 } catch (InvalidOperationException) {
579 Assert.IsTrue (cont.Wait (1000), "#3");
581 Assert.IsFalse (taskResult, "#4");
583 Assert.IsNull (cont.Exception, "#5");
584 Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status, "#6");
588 public void ContinueWithOnFailedTestCase()
590 ParallelTestHelper.Repeat (delegate {
593 Task t = Task.Factory.StartNew(delegate { throw new Exception("foo"); });
594 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnFaulted);
596 Assert.IsTrue (cont.Wait(1000), "#0");
597 Assert.IsNotNull (t.Exception, "#1");
598 Assert.IsNotNull (cont, "#2");
599 Assert.IsTrue (result, "#3");
604 public void ContinueWithWithStart ()
606 Task t = new Task<int> (() => 1);
607 t = t.ContinueWith (l => { });
611 } catch (InvalidOperationException) {
616 public void ContinueWithChildren ()
618 ParallelTestHelper.Repeat (delegate {
621 var t = Task.Factory.StartNew (() => Task.Factory.StartNew (() => {}, TaskCreationOptions.AttachedToParent));
623 var mre = new ManualResetEvent (false);
624 t.ContinueWith (l => {
629 Assert.IsTrue (mre.WaitOne (1000), "#1");
630 Assert.IsTrue (result, "#2");
635 public void ContinueWithDifferentOptionsAreCanceledTest ()
637 var mre = new ManualResetEventSlim ();
638 var task = Task.Factory.StartNew (() => mre.Wait (200));
639 var contFailed = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnFaulted);
640 var contCanceled = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnCanceled);
641 var contSuccess = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnRanToCompletion);
644 contSuccess.Wait (100);
646 Assert.IsTrue (contSuccess.IsCompleted);
647 Assert.IsTrue (contFailed.IsCompleted);
648 Assert.IsTrue (contCanceled.IsCompleted);
649 Assert.IsFalse (contSuccess.IsCanceled);
650 Assert.IsTrue (contFailed.IsCanceled);
651 Assert.IsTrue (contCanceled.IsCanceled);
655 public void MultipleTasks()
657 ParallelTestHelper.Repeat (delegate {
658 bool r1 = false, r2 = false, r3 = false;
660 Task t1 = Task.Factory.StartNew(delegate {
663 Task t2 = Task.Factory.StartNew(delegate {
666 Task t3 = Task.Factory.StartNew(delegate {
674 Assert.IsTrue(r1, "#1");
675 Assert.IsTrue(r2, "#2");
676 Assert.IsTrue(r3, "#3");
681 public void WaitChildTestCase()
683 ParallelTestHelper.Repeat (delegate {
684 bool r1 = false, r2 = false, r3 = false;
685 var mre = new ManualResetEventSlim (false);
686 var mreStart = new ManualResetEventSlim (false);
688 Task t = Task.Factory.StartNew(delegate {
689 Task.Factory.StartNew(delegate {
692 }, TaskCreationOptions.AttachedToParent);
693 Task.Factory.StartNew(delegate {
695 }, TaskCreationOptions.AttachedToParent);
696 Task.Factory.StartNew(delegate {
698 }, TaskCreationOptions.AttachedToParent);
703 Assert.IsFalse (t.Wait (10), "#0a");
705 Assert.IsTrue (t.Wait (500), "#0b");
706 Assert.IsTrue(r2, "#1");
707 Assert.IsTrue(r3, "#2");
708 Assert.IsTrue(r1, "#3");
709 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#4");
716 public void WaitingForChildrenToComplete ()
719 var mre = new ManualResetEvent (false);
721 parent_wfc = Task.Factory.StartNew (() => {
722 nested = Task.Factory.StartNew (() => {
723 Assert.IsTrue (mre.WaitOne (4000), "parent_wfc needs to be set first");
724 Assert.IsFalse (parent_wfc.Wait (10), "#1a");
725 Assert.AreEqual (TaskStatus.WaitingForChildrenToComplete, parent_wfc.Status, "#1b");
726 }, TaskCreationOptions.AttachedToParent).ContinueWith (l => {
727 Assert.IsTrue (parent_wfc.Wait (2000), "#2a");
728 Assert.AreEqual (TaskStatus.RanToCompletion, parent_wfc.Status, "#2b");
729 }, TaskContinuationOptions.ExecuteSynchronously);
733 Assert.IsTrue (parent_wfc.Wait (2000), "#3");
734 Assert.IsTrue (nested.Wait (2000), "#4");
738 public void WaitChildWithContinuationAttachedTest ()
741 var task = new Task(() =>
743 Task.Factory.StartNew(() => {
745 }, TaskCreationOptions.AttachedToParent).ContinueWith(t => {
748 }, TaskContinuationOptions.AttachedToParent);
752 Assert.IsTrue (result);
756 public void WaitChildWithContinuationNotAttachedTest ()
758 var task = new Task(() =>
760 Task.Factory.StartNew(() => {
762 }, TaskCreationOptions.AttachedToParent).ContinueWith(t => {
767 Assert.IsTrue (task.Wait(400));
771 public void WaitChildWithNesting ()
774 var t = Task.Factory.StartNew (() => {
775 Task.Factory.StartNew (() => {
776 Task.Factory.StartNew (() => {
779 }, TaskCreationOptions.AttachedToParent);
780 }, TaskCreationOptions.AttachedToParent);
783 Assert.IsTrue (result);
787 public void DoubleWaitTest ()
789 ParallelTestHelper.Repeat (delegate {
790 var evt = new ManualResetEventSlim ();
791 var t = Task.Factory.StartNew (() => evt.Wait (5000));
792 var cntd = new CountdownEvent (2);
793 var cntd2 = new CountdownEvent (2);
795 bool r1 = false, r2 = false;
796 ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r1 = t.Wait (1000) && t.Result; cntd2.Signal (); });
797 ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r2 = t.Wait (1000) && t.Result; cntd2.Signal (); });
799 Assert.IsTrue (cntd.Wait (2000), "#1");
801 Assert.IsTrue (cntd2.Wait (2000), "#2");
802 Assert.IsTrue (r1, "r1");
803 Assert.IsTrue (r2, "r2");
808 public void DoubleTimeoutedWaitTest ()
810 var evt = new ManualResetEventSlim ();
811 var t = new Task (delegate { });
812 var cntd = new CountdownEvent (2);
814 bool r1 = false, r2 = false;
815 ThreadPool.QueueUserWorkItem (delegate { r1 = !t.Wait (100); cntd.Signal (); });
816 ThreadPool.QueueUserWorkItem (delegate { r2 = !t.Wait (100); cntd.Signal (); });
824 public void RunSynchronously ()
827 Task t = new Task (() => { Thread.Sleep (100); val = 1; });
828 t.RunSynchronously ();
830 Assert.AreEqual (1, val, "#1");
832 t = new Task (() => { Thread.Sleep (0); val = 2; });
834 bool? previouslyQueued = null;
836 var scheduler = new MockScheduler ();
837 scheduler.TryExecuteTaskInlineHandler += (task, b) => {
838 previouslyQueued = b;
841 t.RunSynchronously (scheduler);
843 Assert.AreEqual (2, val, "#2");
844 Assert.AreEqual (false, previouslyQueued, "#2a");
848 public void RunSynchronouslyArgumentChecks ()
850 Task t = new Task (() => { });
852 t.RunSynchronously (null);
854 } catch (ArgumentNullException) {
859 public void RunSynchronously_SchedulerException ()
861 var scheduler = new MockScheduler ();
862 scheduler.TryExecuteTaskInlineHandler += (task, b) => {
863 throw new ApplicationException ();
866 Task t = new Task (() => { });
868 t.RunSynchronously (scheduler);
870 } catch (Exception e) {
871 Assert.AreEqual (t.Exception.InnerException, e);
876 public void RunSynchronouslyWithAttachedChildren ()
879 var t = new Task (() => {
880 Task.Factory.StartNew (() => { Thread.Sleep (500); result = true; }, TaskCreationOptions.AttachedToParent);
882 t.RunSynchronously ();
883 Assert.IsTrue (result);
887 public void RunSynchronouslyOnContinuation ()
889 Task t = new Task<int> (() => 1);
890 t = t.ContinueWith (l => { });
892 t.RunSynchronously ();
894 } catch (InvalidOperationException) {
899 public void UnobservedExceptionOnFinalizerThreadTest ()
901 bool wasCalled = false;
902 TaskScheduler.UnobservedTaskException += (o, args) => {
906 var inner = new ApplicationException ();
907 Task.Factory.StartNew (() => { throw inner; });
911 GC.WaitForPendingFinalizers ();
913 Assert.IsTrue (wasCalled);
916 [Test, ExpectedException (typeof (InvalidOperationException))]
917 public void StartFinishedTaskTest ()
919 var t = Task.Factory.StartNew (delegate () { });
926 public void Start_NullArgument ()
928 var t = Task.Factory.StartNew (delegate () { });
932 } catch (ArgumentNullException) {
936 [Test, ExpectedException (typeof (InvalidOperationException))]
937 public void DisposeUnstartedTest ()
939 var t = new Task (() => { });
944 public void ThrowingUnrelatedCanceledExceptionTest ()
946 Task t = new Task (() => {
947 throw new TaskCanceledException ();
950 t.RunSynchronously ();
951 Assert.IsTrue (t.IsFaulted);
952 Assert.IsFalse (t.IsCanceled);
956 public void CanceledContinuationExecuteSynchronouslyTest ()
958 var source = new CancellationTokenSource();
959 var token = source.Token;
960 var evt = new ManualResetEventSlim ();
964 var task = Task.Factory.StartNew (() => evt.Wait (100));
965 var cont = task.ContinueWith (t => result = true, token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
972 } catch (Exception ex) {
976 Assert.IsTrue (task.IsCompleted);
977 Assert.IsTrue (cont.IsCanceled);
978 Assert.IsFalse (result);
979 Assert.IsTrue (thrown);
983 public void WhenChildTaskErrorIsThrownParentTaskShouldBeFaulted ()
985 Task innerTask = null;
986 var testTask = new Task (() =>
988 innerTask = new Task (() =>
990 throw new InvalidOperationException ();
991 }, TaskCreationOptions.AttachedToParent);
992 innerTask.RunSynchronously ();
994 testTask.RunSynchronously ();
996 Assert.AreNotEqual (TaskStatus.Running, testTask.Status);
997 Assert.IsNotNull (innerTask);
998 Assert.IsTrue (innerTask.IsFaulted);
999 Assert.IsNotNull (testTask.Exception);
1000 Assert.IsTrue (testTask.IsFaulted);
1001 Assert.IsNotNull (innerTask.Exception);
1005 public void WhenChildTaskErrorIsThrownOnlyOnFaultedContinuationShouldExecute ()
1007 var continuationRan = false;
1008 var testTask = new Task (() =>
1010 var task = new Task (() =>
1012 throw new InvalidOperationException();
1013 }, TaskCreationOptions.AttachedToParent);
1014 task.RunSynchronously ();
1016 var onErrorTask = testTask.ContinueWith (x => continuationRan = true, TaskContinuationOptions.OnlyOnFaulted);
1017 testTask.RunSynchronously ();
1018 onErrorTask.Wait (100);
1019 Assert.IsTrue (continuationRan);
1023 public void WhenChildTaskErrorIsThrownNotOnFaultedContinuationShouldNotBeExecuted ()
1025 var continuationRan = false;
1026 var testTask = new Task (() =>
1028 var task = new Task (() =>
1030 throw new InvalidOperationException();
1031 }, TaskCreationOptions.AttachedToParent);
1032 task.RunSynchronously();
1034 var onErrorTask = testTask.ContinueWith (x => continuationRan = true, TaskContinuationOptions.NotOnFaulted);
1035 testTask.RunSynchronously ();
1036 Assert.IsTrue (onErrorTask.IsCompleted);
1037 Assert.IsFalse (onErrorTask.IsFaulted);
1038 Assert.IsFalse (continuationRan);
1042 public void WhenChildTaskSeveralLevelsDeepHandlesAggregateExceptionErrorStillBubblesToParent ()
1044 var continuationRan = false;
1045 AggregateException e = null;
1046 var testTask = new Task (() =>
1048 var child1 = new Task (() =>
1050 var child2 = new Task (() =>
1052 throw new InvalidOperationException();
1053 }, TaskCreationOptions.AttachedToParent);
1054 child2.RunSynchronously ();
1055 }, TaskCreationOptions.AttachedToParent);
1057 child1.RunSynchronously();
1058 e = child1.Exception;
1059 child1.Exception.Handle (ex => true);
1061 var onErrorTask = testTask.ContinueWith (x => continuationRan = true, TaskContinuationOptions.OnlyOnFaulted);
1062 testTask.RunSynchronously ();
1063 onErrorTask.Wait (100);
1064 Assert.IsNotNull (e);
1065 Assert.IsTrue (continuationRan);
1069 public void AlreadyCompletedChildTaskShouldRunContinuationImmediately ()
1071 string result = "Failed";
1072 var testTask = new Task (() =>
1074 var child = new Task<string> (() =>
1077 }, TaskCreationOptions.AttachedToParent);
1078 child.RunSynchronously ();
1079 child.ContinueWith (x => { Thread.Sleep (50); result = x.Result; }, TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.NotOnFaulted);
1081 testTask.RunSynchronously ();
1083 Assert.AreEqual ("Success", result);
1087 public void InlineNotTrashingParentRelationship ()
1089 bool r1 = false, r2 = false;
1090 var t = new Task (() => {
1091 new Task (() => { r1 = true; }, TaskCreationOptions.AttachedToParent).RunSynchronously ();
1092 Task.Factory.StartNew (() => { Thread.Sleep (100); r2 = true; }, TaskCreationOptions.AttachedToParent);
1094 t.RunSynchronously ();
1101 public void AsyncWaitHandleSet ()
1103 var task = new TaskFactory ().StartNew (() => { });
1104 var ar = (IAsyncResult)task;
1105 ar.AsyncWaitHandle.WaitOne ();
1109 public void StartOnBrokenScheduler ()
1111 var t = new Task (delegate { });
1114 t.Start (new ExceptionScheduler ());
1116 } catch (TaskSchedulerException e) {
1117 Assert.AreEqual (TaskStatus.Faulted, t.Status, "#2");
1118 Assert.AreSame (e, t.Exception.InnerException, "#3");
1119 Assert.IsTrue (e.InnerException is ApplicationException, "#4");
1125 public void ContinuationOnBrokenScheduler ()
1127 var s = new ExceptionScheduler ();
1128 Task t = new Task(delegate {});
1130 var t2 = t.ContinueWith (delegate {
1131 }, TaskContinuationOptions.ExecuteSynchronously, s);
1133 var t3 = t.ContinueWith (delegate {
1134 }, TaskContinuationOptions.ExecuteSynchronously, s);
1139 Assert.IsTrue (t3.Wait (2000), "#0");
1141 } catch (AggregateException e) {
1144 Assert.AreEqual (TaskStatus.Faulted, t2.Status, "#2");
1145 Assert.AreEqual (TaskStatus.Faulted, t3.Status, "#3");
1149 public void Delay_Invalid ()
1153 } catch (ArgumentOutOfRangeException) {
1158 public void Delay_Start ()
1160 var t = Task.Delay (5000);
1163 } catch (InvalidOperationException) {
1168 public void Delay_Simple ()
1170 var t = Task.Delay (300);
1171 Assert.IsTrue (TaskStatus.WaitingForActivation == t.Status || TaskStatus.Running == t.Status, "#1");
1172 Assert.IsTrue (t.Wait (400), "#2");
1176 public void Delay_Cancelled ()
1178 var cancelation = new CancellationTokenSource ();
1180 var t = Task.Delay (5000, cancelation.Token);
1181 Assert.IsTrue (TaskStatus.WaitingForActivation == t.Status || TaskStatus.Running == t.Status, "#1");
1182 cancelation.Cancel ();
1186 } catch (AggregateException) {
1187 Assert.AreEqual (TaskStatus.Canceled, t.Status, "#3");
1190 cancelation = new CancellationTokenSource ();
1191 t = Task.Delay (Timeout.Infinite, cancelation.Token);
1192 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#11");
1193 cancelation.Cancel ();
1196 Assert.Fail ("#12");
1197 } catch (AggregateException) {
1198 Assert.AreEqual (TaskStatus.Canceled, t.Status, "#13");
1203 public void Delay_TimeManagement ()
1205 var delay1 = Task.Delay(50);
1206 var delay2 = Task.Delay(25);
1207 Assert.IsTrue (Task.WhenAny(new[] { delay1, delay2 }).Wait (1000));
1208 Assert.AreEqual (TaskStatus.RanToCompletion, delay2.Status);
1212 public void WaitAny_WithNull ()
1214 var tasks = new [] {
1215 Task.FromResult (2),
1220 Task.WaitAny (tasks);
1222 } catch (ArgumentException) {
1227 public void WhenAll_Empty ()
1229 var tasks = new Task[0];
1231 Task t = Task.WhenAll(tasks);
1233 Assert.IsTrue(t.Wait(1000), "#1");
1237 public void WhenAll_WithNull ()
1240 Task.FromResult (2),
1245 Task.WhenAll (tasks);
1247 } catch (ArgumentException) {
1252 Task.WhenAll (tasks);
1254 } catch (ArgumentException) {
1259 public void WhenAll_Start ()
1261 Task[] tasks = new[] {
1262 Task.FromResult (2),
1265 var t = Task.WhenAll (tasks);
1266 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1271 } catch (InvalidOperationException) {
1275 new Task (delegate { }),
1278 t = Task.WhenAll (tasks);
1279 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#11");
1283 Assert.Fail ("#12");
1284 } catch (InvalidOperationException) {
1289 public void WhenAll_Cancelled ()
1291 var cancelation = new CancellationTokenSource ();
1292 var tasks = new Task[] {
1293 new Task (delegate { }),
1294 new Task (delegate { }, cancelation.Token)
1297 cancelation.Cancel ();
1299 var t = Task.WhenAll (tasks);
1300 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
1304 Assert.IsTrue (t.Wait (1000), "#2");
1305 Assert.Fail ("#2a");
1306 } catch (AggregateException e) {
1307 Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
1312 public void WhenAll_Faulted ()
1314 var tcs = new TaskCompletionSource<object> ();
1315 tcs.SetException (new ApplicationException ());
1317 var tcs2 = new TaskCompletionSource<object> ();
1318 tcs2.SetException (new InvalidTimeZoneException ());
1320 var cancelation = new CancellationTokenSource ();
1321 var tasks = new Task[] {
1322 new Task (delegate { }),
1323 new Task (delegate { }, cancelation.Token),
1328 cancelation.Cancel ();
1330 var t = Task.WhenAll (tasks);
1331 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
1335 Assert.IsTrue (t.Wait (1000), "#2");
1336 Assert.Fail ("#2a");
1337 } catch (AggregateException e) {
1338 Assert.That (e.InnerException, Is.TypeOf (typeof (ApplicationException)), "#3");
1339 Assert.That (e.InnerExceptions[1], Is.TypeOf (typeof (InvalidTimeZoneException)), "#4");
1344 public void WhenAll ()
1346 var t1 = new Task (delegate { });
1347 var t2 = new Task (delegate { t1.Start (); });
1349 var tasks = new Task[] {
1354 var t = Task.WhenAll (tasks);
1355 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
1358 Assert.IsTrue (t.Wait (1000), "#2");
1362 public void WhenAllResult_Empty ()
1364 var tasks = new Task<int>[0];
1366 Task<int[]> t = Task.WhenAll(tasks);
1368 Assert.IsTrue(t.Wait(1000), "#1");
1369 Assert.IsNotNull(t.Result, "#2");
1370 Assert.AreEqual(t.Result.Length, 0, "#3");
1374 public void WhenAllResult_WithNull ()
1377 Task.FromResult (2),
1382 Task.WhenAll<int> (tasks);
1384 } catch (ArgumentException) {
1389 Task.WhenAll<int> (tasks);
1391 } catch (ArgumentException) {
1396 public void WhenAllResult_Cancelled ()
1398 var cancelation = new CancellationTokenSource ();
1399 var tasks = new [] {
1400 new Task<int> (delegate { return 9; }),
1401 new Task<int> (delegate { return 1; }, cancelation.Token)
1404 cancelation.Cancel ();
1406 var t = Task.WhenAll (tasks);
1407 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
1411 Assert.IsTrue (t.Wait (1000), "#2");
1412 Assert.Fail ("#2a");
1413 } catch (AggregateException e) {
1414 Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
1420 } catch (AggregateException) {
1425 public void WhenAllResult ()
1427 var t1 = new Task<string> (delegate { return "a"; });
1428 var t2 = new Task<string> (delegate { t1.Start (); return "b"; });
1430 var tasks = new [] {
1435 var t = Task.WhenAll<string> (tasks);
1436 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
1439 Assert.IsTrue (t.Wait (1000), "#2");
1440 Assert.AreEqual (2, t.Result.Length, "#3");
1441 Assert.AreEqual ("a", t.Result[0], "#3a");
1442 Assert.AreEqual ("b", t.Result[1], "#3b");
1446 public void WhenAllResult_Completed ()
1449 Task.FromResult (1),
1453 var t = Task.WhenAll<int> (tasks);
1454 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1455 Assert.AreEqual (2, t.Result.Length, "#2");
1456 Assert.AreEqual (1, t.Result[0], "#2a");
1457 Assert.AreEqual (2, t.Result[1], "#2b");
1461 public void WhenAny_WithNull ()
1463 var tasks = new Task[] {
1464 Task.FromResult (2),
1469 Task.WhenAny (tasks);
1471 } catch (ArgumentException) {
1476 Task.WhenAny (tasks);
1478 } catch (ArgumentException) {
1482 Task.WhenAny (new Task[0]);
1484 } catch (ArgumentException) {
1489 public void WhenAny_Start ()
1491 Task[] tasks = new[] {
1492 Task.FromResult (2),
1495 var t = Task.WhenAny (tasks);
1496 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1501 } catch (InvalidOperationException) {
1505 new Task (delegate { }),
1508 t = Task.WhenAny (tasks);
1509 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#11");
1513 Assert.Fail ("#12");
1514 } catch (InvalidOperationException) {
1519 public void WhenAny_Cancelled ()
1521 var cancelation = new CancellationTokenSource ();
1522 var tasks = new Task[] {
1523 new Task (delegate { }),
1524 new Task (delegate { }, cancelation.Token)
1527 cancelation.Cancel ();
1529 var t = Task.WhenAny (tasks);
1530 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1533 Assert.IsTrue (t.Wait (1000), "#2");
1534 Assert.AreEqual (TaskStatus.Canceled, t.Result.Status, "#3");
1538 public void WhenAny_Faulted ()
1540 var tcs = new TaskCompletionSource<object> ();
1541 tcs.SetException (new ApplicationException ());
1543 var tcs2 = new TaskCompletionSource<object> ();
1544 tcs2.SetException (new InvalidTimeZoneException ());
1546 var cancelation = new CancellationTokenSource ();
1547 var tasks = new Task[] {
1548 new Task (delegate { }),
1550 new Task (delegate { }, cancelation.Token),
1554 cancelation.Cancel ();
1556 var t = Task.WhenAny (tasks);
1557 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1560 Assert.IsTrue (t.Wait (1000), "#2");
1561 Assert.IsNull (t.Exception, "#3");
1563 Assert.That (t.Result.Exception.InnerException, Is.TypeOf (typeof (ApplicationException)), "#4");
1567 public void WhenAny ()
1569 var t1 = new Task (delegate { });
1570 var t2 = new Task (delegate { t1.Start (); });
1572 var tasks = new Task[] {
1577 var t = Task.WhenAny (tasks);
1578 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
1581 Assert.IsTrue (t.Wait (1000), "#2");
1582 Assert.IsNotNull (t.Result, "#3");
1586 public void WhenAnyResult_WithNull ()
1588 var tasks = new [] {
1589 Task.FromResult (2),
1594 Task.WhenAny<int> (tasks);
1596 } catch (ArgumentException) {
1601 Task.WhenAny<int> (tasks);
1603 } catch (ArgumentException) {
1607 Task.WhenAny<short> (new Task<short>[0]);
1609 } catch (ArgumentException) {
1614 public void WhenAnyResult_Start ()
1617 Task.FromResult (2),
1620 var t = Task.WhenAny<int> (tasks);
1621 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1626 } catch (InvalidOperationException) {
1630 new Task<int> (delegate { return 55; }),
1633 t = Task.WhenAny<int> (tasks);
1634 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#11");
1638 Assert.Fail ("#12");
1639 } catch (InvalidOperationException) {
1644 public void WhenAnyResult_Cancelled ()
1646 var cancelation = new CancellationTokenSource ();
1647 var tasks = new [] {
1648 new Task<double> (delegate { return 1.1; }),
1649 new Task<double> (delegate { return -4.4; }, cancelation.Token)
1652 cancelation.Cancel ();
1654 var t = Task.WhenAny<double> (tasks);
1655 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1658 Assert.IsTrue (t.Wait (1000), "#2");
1659 Assert.AreEqual (TaskStatus.Canceled, t.Result.Status, "#3");
1663 public void WhenAnyResult_Faulted ()
1665 var tcs = new TaskCompletionSource<object> ();
1666 tcs.SetException (new ApplicationException ());
1668 var tcs2 = new TaskCompletionSource<object> ();
1669 tcs2.SetException (new InvalidTimeZoneException ());
1671 var cancelation = new CancellationTokenSource ();
1672 var tasks = new Task<object>[] {
1673 new Task<object> (delegate { return null; }),
1675 new Task<object> (delegate { return ""; }, cancelation.Token),
1679 cancelation.Cancel ();
1681 var t = Task.WhenAny<object> (tasks);
1682 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#1");
1685 Assert.IsTrue (t.Wait (1000), "#2");
1686 Assert.IsNull (t.Exception, "#3");
1688 Assert.That (t.Result.Exception.InnerException, Is.TypeOf (typeof (ApplicationException)), "#4");
1692 public void WhenAnyResult ()
1694 var t1 = new Task<byte> (delegate { return 3; });
1695 var t2 = new Task<byte> (delegate { t1.Start (); return 2; });
1697 var tasks = new [] {
1702 var t = Task.WhenAny<byte> (tasks);
1703 Assert.AreEqual (TaskStatus.WaitingForActivation, t.Status, "#1");
1706 Assert.IsTrue (t.Wait (1000), "#2");
1707 Assert.IsTrue (t.Result.Result > 1, "#3");
1711 public void ContinueWith_StateValue ()
1713 var t = Task.Factory.StartNew (l => {
1714 Assert.AreEqual (1, l, "a-1");
1717 var c = t.ContinueWith ((a, b) => {
1718 Assert.AreEqual (t, a, "c-1");
1719 Assert.AreEqual (2, b, "c-2");
1722 var d = t.ContinueWith ((a, b) => {
1723 Assert.AreEqual (t, a, "d-1");
1724 Assert.AreEqual (3, b, "d-2");
1728 Assert.IsTrue (d.Wait (1000), "#1");
1730 Assert.AreEqual (1, t.AsyncState, "#2");
1731 Assert.AreEqual (2, c.AsyncState, "#3");
1732 Assert.AreEqual (3, d.AsyncState, "#4");
1736 public void ContinueWith_StateValueGeneric ()
1738 var t = Task<int>.Factory.StartNew (l => {
1739 Assert.AreEqual (1, l, "a-1");
1743 var c = t.ContinueWith ((a, b) => {
1744 Assert.AreEqual (t, a, "c-1");
1745 Assert.AreEqual (2, b, "c-2");
1749 var d = t.ContinueWith ((a, b) => {
1750 Assert.AreEqual (t, a, "d-1");
1751 Assert.AreEqual (3, b, "d-2");
1755 Assert.IsTrue (d.Wait (1000), "#1");
1757 Assert.AreEqual (1, t.AsyncState, "#2");
1758 Assert.AreEqual (80, t.Result, "#2r");
1759 Assert.AreEqual (2, c.AsyncState, "#3");
1760 Assert.AreEqual ("c", c.Result, "#3r");
1761 Assert.AreEqual (3, d.AsyncState, "#4");
1762 Assert.AreEqual ('d', d.Result, "#3r");
1766 public void ContinueWith_CustomScheduleRejected ()
1768 var scheduler = new NonInlineableScheduler ();
1769 var t = Task.Factory.StartNew (delegate { }).
1770 ContinueWith (r => {}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, scheduler);
1772 Assert.IsTrue (t.Wait (5000));
1776 public void FromResult ()
1778 var t = Task.FromResult<object> (null);
1779 Assert.IsTrue (t.IsCompleted, "#1");
1780 Assert.AreEqual (null, t.Result, "#2");
1786 public void LongRunning ()
1790 var t = new Task (() => { is_tp = Thread.CurrentThread.IsThreadPoolThread; is_bg = Thread.CurrentThread.IsBackground; });
1792 Assert.IsTrue (t.Wait (100));
1793 Assert.IsTrue ((bool)is_tp, "#1");
1794 Assert.IsTrue ((bool)is_bg, "#2");
1798 t = new Task (() => { is_tp = Thread.CurrentThread.IsThreadPoolThread; is_bg = Thread.CurrentThread.IsBackground; }, TaskCreationOptions.LongRunning);
1801 Assert.IsTrue (t.Wait (100));
1802 Assert.IsFalse ((bool) is_tp, "#11");
1803 Assert.IsTrue ((bool) is_bg, "#12");
1807 public void Run_ArgumentCheck ()
1810 Task.Run (null as Action);
1812 } catch (ArgumentNullException) {
1819 bool ranOnDefaultScheduler = false;
1820 var t = Task.Run (delegate { ranOnDefaultScheduler = Thread.CurrentThread.IsThreadPoolThread; });
1821 Assert.AreEqual (TaskCreationOptions.DenyChildAttach, t.CreationOptions, "#1");
1823 Assert.IsTrue (ranOnDefaultScheduler, "#2");
1827 public void Run_Cancel ()
1829 var t = Task.Run (() => 1, new CancellationToken (true));
1833 } catch (AggregateException) {
1836 Assert.IsTrue (t.IsCanceled, "#2");
1840 public void Run_ExistingTaskT ()
1842 var t = new Task<int> (() => 5);
1843 var t2 = Task.Run (() => { t.Start (); return t; });
1845 Assert.IsTrue (t2.Wait (1000), "#1");
1846 Assert.AreEqual (5, t2.Result, "#2");
1850 public void Run_ExistingTask ()
1852 var t = new Task (delegate { throw new Exception ("Foo"); });
1853 var t2 = Task.Run (() => { t.Start (); return t; });
1858 } catch (Exception) {}
1860 Assert.AreEqual (TaskStatus.Faulted, t.Status, "#2");
1864 public void DenyChildAttachTest ()
1866 var mre = new ManualResetEventSlim ();
1868 Task parent = Task.Factory.StartNew (() => {
1869 nested = Task.Factory.StartNew (() => mre.Wait (2000), TaskCreationOptions.AttachedToParent);
1870 }, TaskCreationOptions.DenyChildAttach);
1871 Assert.IsTrue (parent.Wait (1000), "#1");
1873 Assert.IsTrue (nested.Wait (2000), "#2");
1876 class SynchronousScheduler : TaskScheduler
1878 protected override IEnumerable<Task> GetScheduledTasks ()
1880 throw new NotImplementedException ();
1883 protected override void QueueTask (Task task)
1885 TryExecuteTaskInline (task, false);
1888 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
1890 return base.TryExecuteTask (task);
1895 public void HideSchedulerTest ()
1897 var mre = new ManualResetEventSlim ();
1898 var ranOnDefault = false;
1899 var scheduler = new SynchronousScheduler ();
1901 Task parent = Task.Factory.StartNew (() => {
1902 Task.Factory.StartNew (() => {
1903 ranOnDefault = Thread.CurrentThread.IsThreadPoolThread;
1906 }, CancellationToken.None, TaskCreationOptions.HideScheduler, scheduler);
1908 Assert.IsTrue (mre.Wait (1000), "#1");
1909 Assert.IsTrue (ranOnDefault, "#2");
1913 public void LazyCancelationTest ()
1915 var source = new CancellationTokenSource ();
1917 var parent = new Task (delegate {});
1918 var cont = parent.ContinueWith (delegate {}, source.Token, TaskContinuationOptions.LazyCancellation, TaskScheduler.Default);
1920 Assert.AreNotEqual (TaskStatus.Canceled, cont.Status, "#1");
1923 Assert.IsTrue (cont.Wait (1000), "#2");
1925 } catch (AggregateException ex) {
1926 Assert.That (ex.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");