Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskFactoryTest_T.cs
index 3fe032f20c639e23229478d870089c4276e09c46..ff964bb9cfcc65e348b234958455b37394d8e901 100644 (file)
 //
 //
 
-#if NET_4_0 || MOBILE
+#if NET_4_0
 
 using System;
 using System.Threading;
 using System.Threading.Tasks;
 
 using NUnit.Framework;
+#if !MOBILE
+using NUnit.Framework.SyntaxHelpers;
+#endif
 
 namespace MonoTests.System.Threading.Tasks
 {
@@ -226,7 +229,53 @@ namespace MonoTests.System.Threading.Tasks
                        Assert.IsTrue (task.Wait (1000), "#1");
                        Assert.AreEqual ("1", task.Result, "#2");
                }
+
+               IAsyncResult BeginGetTestAsyncResultCompletedSynchronously2 (AsyncCallback cb, object obj)
+               {
+                       var result = new TestAsyncResultCompletedSynchronously ();
+                       cb (result);
+                       return result;
+               }
+               
+               string EndGetTestAsyncResultCompletedSynchronously2 (IAsyncResult res)
+               {
+                       return "1";
+               }
+               
+               [Test]
+               public void FromAsync_CompletedSynchronously_with_Callback ()
+               {
+                       var factory = new TaskFactory<string> ();
+                       var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously2, EndGetTestAsyncResultCompletedSynchronously2, null);
+                       
+                       Assert.IsTrue (task.Wait (1000), "#1");
+                       Assert.AreEqual ("1", task.Result, "#2");
+               }
+
+               [Test]
+               public void StartNewCancelled ()
+               {
+                       var ct = new CancellationToken (true);
+                       var factory = new TaskFactory<int> ();
+
+                       var task = factory.StartNew (() => { Assert.Fail ("Should never be called"); return 1; }, ct);
+                       try {
+                               task.Start ();
+                               Assert.Fail ("#1");
+                       } catch (InvalidOperationException) {
+                       }
+
+                       Assert.IsTrue (task.IsCanceled, "#2");
+
+                       task = factory.StartNew (() => 1, ct);
+                       try {
+                               task.Wait ();
+                       } catch (AggregateException e) {
+                               Assert.IsTrue (task.IsCanceled, "#3");
+                               Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#4");
+                       }
+               }
        }
 }
 
-#endif
\ No newline at end of file
+#endif