Merge pull request #799 from kebby/master
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskTest.cs
index 3cb0c34773562b231ea05aea6697e17bc90f8da4..f61a64d4aa0f89fceb29833e418b9910cafabce2 100644 (file)
@@ -67,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;
@@ -1043,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 ()
@@ -1098,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 ()
                {
@@ -1113,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 ()
                {
@@ -1238,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 ()
                {
@@ -1630,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 ()
                {