Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskFactoryTest.cs
index 2bef86dea404ffac4641c91a88f0cd005133d03c..e1d7796e44b5099299ef9d0e0d10ba0570bd4372 100644 (file)
@@ -173,6 +173,83 @@ namespace MonoTests.System.Threading.Tasks
                        Assert.IsTrue (ran, "#2");
                }
 
+               [Test]
+               public void ContinueWhenAll_WithMixedCompletionState ()
+               {
+                       var mre = new ManualResetEventSlim ();
+                       var task = Task.Factory.StartNew (() => mre.Wait (200));
+                       var contFailed = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnFaulted);
+                       var contCanceled = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnCanceled);
+                       var contSuccess = task.ContinueWith (t => {}, TaskContinuationOptions.OnlyOnRanToCompletion);
+                       bool ran = false;
+
+                       var cont = Task.Factory.ContinueWhenAll (new Task[] { contFailed, contCanceled, contSuccess }, _ => ran = true);
+
+                       mre.Set ();
+                       cont.Wait (200);
+
+                       Assert.IsTrue (ran);
+                       Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status);
+               }
+
+               [Test]
+               public void ContinueWhenAll_InvalidArguments ()
+               {
+                       try {
+                               factory.ContinueWhenAll (null, delegate { });
+                               Assert.Fail ("#1");
+                       } catch (ArgumentNullException) {
+                       }
+
+                       try {
+                               factory.ContinueWhenAll (new Task[0], delegate { });
+                               Assert.Fail ("#2");
+                       } catch (ArgumentException) {
+                       }
+
+                       try {
+                               factory.ContinueWhenAll (new Task[] { null }, delegate { });
+                               Assert.Fail ("#3");
+                       } catch (ArgumentException) {
+                       }
+
+                       var tasks = new Task [] {
+                               factory.StartNew (delegate {})
+                       };
+
+                       try {
+                               factory.ContinueWhenAll (tasks, null);
+                               Assert.Fail ("#4");
+                       } catch (ArgumentException) {
+                       }
+
+                       try {
+                               factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
+                               Assert.Fail ("#5");
+                       } catch (ArgumentException) {
+                       }
+
+                       try {
+                               factory.ContinueWhenAll (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
+                               Assert.Fail ("#6");
+                       } catch (ArgumentException) {
+                       }
+               }
+
+               [Test]
+               public void ContinueWhenAll_WithExceptions ()
+               {
+                       var t1 = Task.Factory.StartNew (() => { throw new ApplicationException ("Foo"); });
+                       var t2 = Task.Factory.StartNew (() => { throw new ApplicationException ("Bar"); });
+
+                       var cont = Task.Factory.ContinueWhenAll (new[] { t1, t2 }, delegate {});
+                       cont.Wait (200);
+
+                       Assert.IsTrue (t1.IsFaulted);
+                       Assert.IsTrue (t2.IsFaulted);
+                       Assert.AreEqual (TaskStatus.RanToCompletion, cont.Status);
+               }
+
                [Test]
                public void ContinueWhenAny_Simple ()
                {
@@ -237,6 +314,12 @@ namespace MonoTests.System.Threading.Tasks
                                Assert.Fail ("#5");
                        } catch (ArgumentException) {
                        }
+
+                       try {
+                               factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
+                               Assert.Fail ("#6");
+                       } catch (ArgumentException) {
+                       }
                }
 
                [Test]
@@ -280,7 +363,7 @@ namespace MonoTests.System.Threading.Tasks
                        bool result = false;
                        bool continuationTest = false;
 
-                       Func<int, int> func = (i) => { result = true; throw new ApplicationException ("bleh"); return i + 3; };
+                       Func<int, int> func = (i) => { result = true; throw new ApplicationException ("bleh"); };
                        Task<int> task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, null);
                        var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
                        try {