From: Marek Safar Date: Sat, 7 Dec 2013 14:40:52 +0000 (+0100) Subject: [corlib] Add check for launching cancelled task to all overloads X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=395cf8f623cdf3754f1af0df096e83676133a643;p=mono.git [corlib] Add check for launching cancelled task to all overloads --- diff --git a/mcs/class/corlib/System.Threading.Tasks/TaskFactory.cs b/mcs/class/corlib/System.Threading.Tasks/TaskFactory.cs index 607f08da4ec..a558c622c5e 100644 --- a/mcs/class/corlib/System.Threading.Tasks/TaskFactory.cs +++ b/mcs/class/corlib/System.Threading.Tasks/TaskFactory.cs @@ -214,9 +214,14 @@ namespace System.Threading.Tasks TaskCreationOptions creationOptions, TaskScheduler scheduler) { - Task t = new Task (function, state, cancellationToken, creationOptions); - t.Start (scheduler); + var t = new Task (function, state, cancellationToken, creationOptions); + // + // Don't start cancelled task it would throw an exception + // + if (!t.IsCompleted) + t.Start (scheduler); + return t; } #endregion diff --git a/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest.cs b/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest.cs index f0f87a898f2..857841e326f 100644 --- a/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest.cs +++ b/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest.cs @@ -604,16 +604,24 @@ namespace MonoTests.System.Threading.Tasks [Test] public void StartNewCancelled () { - var cts = new CancellationTokenSource (); - cts.Cancel (); + var ct = new CancellationToken (true); - var task = factory.StartNew (() => Assert.Fail ("Should never be called"), cts.Token); + var task = factory.StartNew (() => Assert.Fail ("Should never be called"), ct); try { task.Start (); + Assert.Fail ("#1"); } catch (InvalidOperationException) { } Assert.IsTrue (task.IsCanceled, "#2"); + + task = factory.StartNew (() => { }, ct); + try { + task.Wait (); + } catch (AggregateException e) { + Assert.IsTrue (task.IsCanceled, "#3"); + Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#4"); + } } } } diff --git a/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest_T.cs b/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest_T.cs index 0c30a093b03..ff964bb9cfc 100644 --- a/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest_T.cs +++ b/mcs/class/corlib/Test/System.Threading.Tasks/TaskFactoryTest_T.cs @@ -33,6 +33,9 @@ using System.Threading; using System.Threading.Tasks; using NUnit.Framework; +#if !MOBILE +using NUnit.Framework.SyntaxHelpers; +#endif namespace MonoTests.System.Threading.Tasks { @@ -249,6 +252,29 @@ namespace MonoTests.System.Threading.Tasks Assert.AreEqual ("1", task.Result, "#2"); } + [Test] + public void StartNewCancelled () + { + var ct = new CancellationToken (true); + var factory = new TaskFactory (); + + 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"); + } + } } }