Merge pull request #799 from kebby/master
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskTest.cs
index 81aea66fdbed29f84abe0851328c22cd189dd407..f61a64d4aa0f89fceb29833e418b9910cafabce2 100644 (file)
@@ -35,6 +35,10 @@ using System.Threading.Tasks;
 using System.Collections.Generic;
 using NUnit.Framework;
 
+#if !MOBILE
+using NUnit.Framework.SyntaxHelpers;
+#endif
+
 namespace MonoTests.System.Threading.Tasks
 {
        [TestFixture]
@@ -63,6 +67,25 @@ namespace MonoTests.System.Threading.Tasks
                        }
                }
 
+               class NonInlineableScheduler : TaskScheduler
+               {
+                       protected override IEnumerable<Task> GetScheduledTasks ()
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       protected override void QueueTask (Task task)
+                       {
+                               if (!base.TryExecuteTask (task))
+                                       throw new ApplicationException ();
+                       }
+
+                       protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
+                       {
+                               return false;
+                       }
+               }
+
 
                Task[] tasks;
                const int max = 6;
@@ -358,7 +381,7 @@ namespace MonoTests.System.Threading.Tasks
                                Task.WaitAll (tasks);
                                Assert.Fail ("#1");
                        } catch (AggregateException e) {
-                               Assert.IsInstanceOfType (typeof (ApplicationException), e.InnerException, "#2");
+                               Assert.That (e.InnerException, Is.TypeOf (typeof (ApplicationException)), "#2");
                                var inner = (TaskCanceledException) e.InnerExceptions[1];
                                Assert.AreEqual (tasks[1], inner.Task, "#3");
                        }
@@ -675,9 +698,11 @@ namespace MonoTests.System.Threading.Tasks
                public void WaitingForChildrenToComplete ()
                {
                        Task nested = null;
+                       var mre = new ManualResetEvent (false);
 
                        parent_wfc = Task.Factory.StartNew (() => {
                                nested = Task.Factory.StartNew (() => {
+                                       Assert.IsTrue (mre.WaitOne (4000), "parent_wfc needs to be set first");
                                        Assert.IsFalse (parent_wfc.Wait (10), "#1a");
                                        Assert.AreEqual (TaskStatus.WaitingForChildrenToComplete, parent_wfc.Status, "#1b");
                                }, TaskCreationOptions.AttachedToParent).ContinueWith (l => {
@@ -686,6 +711,7 @@ namespace MonoTests.System.Threading.Tasks
                                }, TaskContinuationOptions.ExecuteSynchronously);
                        });
 
+                       mre.Set ();
                        Assert.IsTrue (parent_wfc.Wait (2000), "#3");
                        Assert.IsTrue (nested.Wait (2000), "#4");
                }
@@ -1036,6 +1062,14 @@ namespace MonoTests.System.Threading.Tasks
                        Assert.IsTrue (r2);
                }
 
+               [Test]
+               public void AsyncWaitHandleSet ()
+               {
+                       var task = new TaskFactory ().StartNew (() => { });
+                       var ar = (IAsyncResult)task;
+                       ar.AsyncWaitHandle.WaitOne ();
+               }
+
 #if NET_4_5
                [Test]
                public void Delay_Invalid ()
@@ -1091,6 +1125,15 @@ namespace MonoTests.System.Threading.Tasks
                        }
                }
 
+               [Test]
+               public void Delay_TimeManagement ()
+               {
+                       var delay1 = Task.Delay(50);
+                       var delay2 = Task.Delay(25);
+                       Assert.IsTrue (Task.WhenAny(new[] { delay1, delay2 }).Wait (1000));
+                       Assert.AreEqual (TaskStatus.RanToCompletion, delay2.Status);
+               }
+
                [Test]
                public void WaitAny_WithNull ()
                {
@@ -1106,6 +1149,16 @@ namespace MonoTests.System.Threading.Tasks
                        }
                }
 
+               [Test]
+               public void WhenAll_Empty ()
+               {
+                       var tasks = new Task[0];
+
+                       Task t = Task.WhenAll(tasks);
+
+                       Assert.IsTrue(t.Wait(1000), "#1");
+               }
+
                [Test]
                public void WhenAll_WithNull ()
                {
@@ -1177,7 +1230,7 @@ namespace MonoTests.System.Threading.Tasks
                                Assert.IsTrue (t.Wait (1000), "#2");
                                Assert.Fail ("#2a");
                        } catch (AggregateException e) {
-                               Assert.IsInstanceOfType (typeof (TaskCanceledException), e.InnerException, "#3");
+                               Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
                        }
                }
 
@@ -1208,8 +1261,8 @@ namespace MonoTests.System.Threading.Tasks
                                Assert.IsTrue (t.Wait (1000), "#2");
                                Assert.Fail ("#2a");
                        } catch (AggregateException e) {
-                               Assert.IsInstanceOfType (typeof (ApplicationException), e.InnerException, "#3");
-                               Assert.IsInstanceOfType (typeof (InvalidTimeZoneException), e.InnerExceptions[1], "#4");
+                               Assert.That (e.InnerException, Is.TypeOf (typeof (ApplicationException)), "#3");
+                               Assert.That (e.InnerExceptions[1], Is.TypeOf (typeof (InvalidTimeZoneException)), "#4");
                        }
                }
 
@@ -1231,6 +1284,18 @@ namespace MonoTests.System.Threading.Tasks
                        Assert.IsTrue (t.Wait (1000), "#2");
                }
 
+               [Test]
+               public void WhenAllResult_Empty ()
+               {
+                       var tasks = new Task<int>[0];
+
+                       Task<int[]> t = Task.WhenAll(tasks);
+
+                       Assert.IsTrue(t.Wait(1000), "#1");
+                       Assert.IsNotNull(t.Result, "#2");
+                       Assert.AreEqual(t.Result.Length, 0, "#3");
+               }
+
                [Test]
                public void WhenAllResult_WithNull ()
                {
@@ -1272,7 +1337,7 @@ namespace MonoTests.System.Threading.Tasks
                                Assert.IsTrue (t.Wait (1000), "#2");
                                Assert.Fail ("#2a");
                        } catch (AggregateException e) {
-                               Assert.IsInstanceOfType (typeof (TaskCanceledException), e.InnerException, "#3");
+                               Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
                        }
 
                        try {
@@ -1421,7 +1486,7 @@ namespace MonoTests.System.Threading.Tasks
                        Assert.IsTrue (t.Wait (1000), "#2");
                        Assert.IsNull (t.Exception, "#3");
 
-                       Assert.IsInstanceOfType (typeof (ApplicationException), t.Result.Exception.InnerException, "#4");
+                       Assert.That (t.Result.Exception.InnerException, Is.TypeOf (typeof (ApplicationException)), "#4");
                }
 
                [Test]
@@ -1546,7 +1611,7 @@ namespace MonoTests.System.Threading.Tasks
                        Assert.IsTrue (t.Wait (1000), "#2");
                        Assert.IsNull (t.Exception, "#3");
 
-                       Assert.IsInstanceOfType (typeof (ApplicationException), t.Result.Exception.InnerException, "#4");
+                       Assert.That (t.Result.Exception.InnerException, Is.TypeOf (typeof (ApplicationException)), "#4");
                }
 
                [Test]
@@ -1623,6 +1688,16 @@ namespace MonoTests.System.Threading.Tasks
                        Assert.AreEqual ('d', d.Result, "#3r");
                }
 
+               [Test]
+               public void ContinueWith_CustomScheduleRejected ()
+               {
+                       var scheduler = new NonInlineableScheduler ();
+                       var t = Task.Factory.StartNew (delegate { }).
+                               ContinueWith (r => {}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, scheduler);
+                       
+                       Assert.IsTrue (t.Wait (5000));
+               }
+
                [Test]
                public void FromResult ()
                {
@@ -1774,7 +1849,7 @@ namespace MonoTests.System.Threading.Tasks
                                Assert.IsTrue (cont.Wait (1000), "#2");
                                Assert.Fail ();
                        } catch (AggregateException ex) {
-                               Assert.IsInstanceOfType (typeof (TaskCanceledException), ex.InnerException);
+                               Assert.That (ex.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
                        }
                }
 #endif