| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- //
- // TaskFactoryTest.cs
- //
- // Authors:
- // Jérémie "Garuma" Laval <[email protected]>
- // Marek Safar <[email protected]>
- //
- // Copyright (c) 2010 Jérémie "Garuma" Laval
- // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- //
- #if NET_4_0 || MOBILE
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using NUnit.Framework;
- #if !MOBILE
- using NUnit.Framework.SyntaxHelpers;
- #endif
- namespace MonoTests.System.Threading.Tasks
- {
- [TestFixture]
- public class TaskFactoryTests
- {
- class CompletedAsyncResult : IAsyncResult
- {
- public object AsyncState
- {
- get { throw new NotImplementedException (); }
- }
- public WaitHandle AsyncWaitHandle
- {
- get { throw new NotImplementedException (); }
- }
- public bool CompletedSynchronously
- {
- get { throw new NotImplementedException (); }
- }
- public bool IsCompleted
- {
- get { return true; }
- }
- }
- class TestAsyncResult : IAsyncResult
- {
- WaitHandle wh = new ManualResetEvent (true);
- public object AsyncState
- {
- get { throw new NotImplementedException (); }
- }
- public WaitHandle AsyncWaitHandle
- {
- get
- {
- return wh;
- }
- }
- public bool CompletedSynchronously
- {
- get { throw new NotImplementedException (); }
- }
- public bool IsCompleted
- {
- get { return false; }
- }
- }
- class TestScheduler : TaskScheduler
- {
- public bool ExecutedInline { get; set; }
- protected override void QueueTask (Task task)
- {
- throw new NotImplementedException ();
- }
- protected override bool TryDequeue (Task task)
- {
- throw new NotImplementedException ();
- }
- protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
- {
- if (taskWasPreviouslyQueued)
- throw new ArgumentException ("taskWasPreviouslyQueued");
- if (task.Status != TaskStatus.WaitingToRun)
- throw new ArgumentException ("task.Status");
- ExecutedInline = true;
- return TryExecuteTask (task);
- }
- protected override IEnumerable<Task> GetScheduledTasks ()
- {
- throw new NotImplementedException ();
- }
- }
- TaskFactory factory;
- [SetUp]
- public void Setup ()
- {
- this.factory = Task.Factory;
- }
- [Test]
- public void StartNewTest ()
- {
- bool result = false;
- factory.StartNew (() => result = true).Wait ();
- Assert.IsTrue (result);
- }
- [Test]
- public void NoDefaultScheduler ()
- {
- Assert.IsNull (factory.Scheduler, "#1");
- }
- [Test]
- public void ContinueWhenAll_Simple ()
- {
- var mre = new ManualResetEventSlim (false);
- Task[] tasks = new Task[3];
- tasks[0] = new Task (() => { Thread.Sleep (0); Assert.IsTrue (mre.Wait (3000)); });
- tasks[1] = new Task (() => { Assert.IsTrue (mre.Wait (3000)); });
- tasks[2] = new Task (() => { Assert.IsTrue (mre.Wait (3000)); });
- bool ran = false;
- Task cont = factory.ContinueWhenAll (tasks, ts => {
- Assert.AreEqual (tasks, ts, "#0");
- ran = true;
- });
- foreach (Task t in tasks)
- t.Start ();
- mre.Set ();
- Assert.IsTrue (cont.Wait (1000), "#1");
- 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 ()
- {
- var t1 = new ManualResetEvent (false);
- var t2 = new ManualResetEvent (false);
- var tasks = new Task[2] {
- Task.Factory.StartNew (() => { t1.WaitOne (3000); }),
- Task.Factory.StartNew (() => { t2.WaitOne (3000); })
- };
- bool ran = false;
- var ct = new CancellationToken ();
- Task cont = factory.ContinueWhenAny (tasks, t => {
- Assert.AreEqual (tasks[0], t, "#1");
- ran = true;
- }, ct);
- Assert.AreEqual (TaskStatus.WaitingForActivation, cont.Status, "#2");
- t1.Set ();
- Assert.IsTrue (cont.Wait (2000), "#10");
- Assert.IsTrue (ran, "#11");
- t2.Set ();
- }
- [Test]
- public void ContinueWhenAny_InvalidArguments ()
- {
- try {
- factory.ContinueWhenAny (null, delegate { });
- Assert.Fail ("#1");
- } catch (ArgumentNullException) {
- }
- try {
- factory.ContinueWhenAny (new Task[0], delegate { });
- Assert.Fail ("#2");
- } catch (ArgumentException) {
- }
- try {
- factory.ContinueWhenAny (new Task[] { null }, delegate { });
- Assert.Fail ("#3");
- } catch (ArgumentException) {
- }
- var tasks = new Task [] {
- factory.StartNew (delegate {})
- };
- try {
- factory.ContinueWhenAny (tasks, null);
- Assert.Fail ("#4");
- } catch (ArgumentException) {
- }
- try {
- factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.None, null);
- Assert.Fail ("#5");
- } catch (ArgumentException) {
- }
- try {
- factory.ContinueWhenAny (tasks, delegate { }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, null);
- Assert.Fail ("#6");
- } catch (ArgumentException) {
- }
- }
- [Test]
- public void FromAsyncBeginInvoke_WithResult ()
- {
- bool result = false;
- Func<int, int> func = (i) => {
- Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread);
- result = true; return i + 3;
- };
- var task = factory.FromAsync<int, int> (func.BeginInvoke, func.EndInvoke, 1, "state", TaskCreationOptions.AttachedToParent);
- Assert.IsTrue (task.Wait (5000), "#1");
- Assert.IsTrue (result, "#2");
- Assert.AreEqual (4, task.Result, "#3");
- Assert.AreEqual ("state", (string) task.AsyncState, "#4");
- Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#5");
- }
- [Test]
- public void FromAsyncBeginMethod_DirectResult ()
- {
- bool result = false;
- bool continuationTest = false;
- Func<int, int> func = (i) => { result = true; return i + 3; };
- Task<int> task = factory.FromAsync<int> (func.BeginInvoke (1, delegate { }, null), func.EndInvoke);
- var cont = task.ContinueWith (_ => continuationTest = true, TaskContinuationOptions.ExecuteSynchronously);
- task.Wait ();
- cont.Wait ();
- Assert.IsTrue (result);
- Assert.IsTrue (continuationTest);
- Assert.AreEqual (4, task.Result);
- }
- [Test]
- public void FromAsyncBeginMethod_Exception ()
- {
- bool result = false;
- bool continuationTest = false;
- 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 {
- task.Wait ();
- } catch { }
- cont.Wait ();
- Assert.IsTrue (result);
- Assert.IsTrue (continuationTest);
- Assert.IsNotNull (task.Exception);
- var agg = task.Exception;
- Assert.AreEqual (1, agg.InnerExceptions.Count);
- Assert.That (agg.InnerExceptions[0], Is.TypeOf (typeof (ApplicationException)));
- Assert.AreEqual (TaskStatus.Faulted, task.Status);
- try {
- var a = task.Result;
- Assert.Fail ();
- } catch (AggregateException) {
- }
- }
- [Test]
- public void FromAsync_ArgumentsCheck ()
- {
- var result = new CompletedAsyncResult ();
- try {
- factory.FromAsync (null, l => { });
- Assert.Fail ("#1");
- } catch (ArgumentNullException) {
- }
- try {
- factory.FromAsync (result, null);
- Assert.Fail ("#2");
- } catch (ArgumentNullException) {
- }
- try {
- factory.FromAsync (result, l => { }, TaskCreationOptions.LongRunning);
- Assert.Fail ("#3");
- } catch (ArgumentOutOfRangeException) {
- }
- try {
- factory.FromAsync (result, l => { }, TaskCreationOptions.PreferFairness);
- Assert.Fail ("#4");
- } catch (ArgumentOutOfRangeException) {
- }
- try {
- factory.FromAsync (result, l => { }, TaskCreationOptions.None, null);
- Assert.Fail ("#5");
- } catch (ArgumentNullException) {
- }
- try {
- factory.FromAsync (null, l => { }, null, TaskCreationOptions.None);
- Assert.Fail ("#6");
- } catch (ArgumentNullException) {
- }
- try {
- factory.FromAsync ((a, b) => null, l => { }, null, TaskCreationOptions.LongRunning);
- Assert.Fail ("#7");
- } catch (ArgumentOutOfRangeException) {
- }
- }
- [Test]
- public void FromAsync_Completed ()
- {
- var completed = new CompletedAsyncResult ();
- bool? valid = null;
- Action<IAsyncResult> end = l => {
- Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#2");
- valid = l == completed;
- };
- Task task = factory.FromAsync (completed, end);
- Assert.IsTrue (valid == true, "#1");
- }
- [Test]
- public void FromAsync_CompletedWithException ()
- {
- var completed = new CompletedAsyncResult ();
- Action<IAsyncResult> end = l => {
- throw new ApplicationException ();
- };
- Task task = factory.FromAsync (completed, end);
- Assert.AreEqual (TaskStatus.Faulted, task.Status, "#1");
- }
- [Test]
- public void FromAsync_CompletedCanceled ()
- {
- var completed = new CompletedAsyncResult ();
- Action<IAsyncResult> end = l => {
- throw new OperationCanceledException ();
- };
- Task task = factory.FromAsync (completed, end);
- Assert.AreEqual (TaskStatus.Canceled, task.Status, "#1");
- Assert.IsNull (task.Exception, "#2");
- }
- [Test]
- public void FromAsync_SimpleAsyncResult ()
- {
- var result = new TestAsyncResult ();
- bool called = false;
- var task = factory.FromAsync (result, l => {
- called = true;
- });
- Assert.IsTrue (task.Wait (1000), "#1");
- Assert.IsTrue (called, "#2");
- }
- [Test]
- public void FromAsync_ResultException ()
- {
- var result = new TestAsyncResult ();
- var task = factory.FromAsync (result, l => {
- throw new ApplicationException ();
- });
- try {
- Assert.IsFalse (task.Wait (1000), "#1");
- } catch (AggregateException) {
- }
- Assert.AreEqual (TaskStatus.Faulted, task.Status, "#2");
- }
- [Test]
- public void FromAsync_ReturnInt ()
- {
- var result = new TestAsyncResult ();
- bool called = false;
- var task = factory.FromAsync<int> (result, l => {
- called = true;
- return 4;
- });
- Assert.IsTrue (task.Wait (1000), "#1");
- Assert.IsTrue (called, "#2");
- Assert.AreEqual (4, task.Result, "#3");
- }
- [Test]
- public void FromAsync_Scheduler_Explicit ()
- {
- var result = new TestAsyncResult ();
- bool called = false;
- var scheduler = new TestScheduler ();
- var task = factory.FromAsync (result, l => {
- called = true;
- }, TaskCreationOptions.None, scheduler);
- Assert.IsTrue (task.Wait (5000), "#1");
- Assert.IsTrue (called, "#2");
- Assert.IsTrue (scheduler.ExecutedInline, "#3");
- }
- [Test]
- public void FromAsync_Scheduler_Implicit ()
- {
- var result = new TestAsyncResult ();
- bool called = false;
- var scheduler = new TestScheduler ();
- factory = new TaskFactory (scheduler);
- Task task = factory.FromAsync (result, l => {
- Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "#6");
- called = true;
- }, TaskCreationOptions.AttachedToParent);
- Assert.AreEqual (TaskCreationOptions.AttachedToParent, task.CreationOptions, "#1");
- Assert.IsNull (task.AsyncState, "#2");
- Assert.IsTrue (task.Wait (5000), "#3");
- Assert.IsTrue (called, "#4");
- Assert.IsTrue (scheduler.ExecutedInline, "#5");
- }
- [Test]
- public void FromAsync_BeginCallback ()
- {
- bool called = false;
- bool called2 = false;
- var task = factory.FromAsync (
- (a, b, c) => {
- if (a != "h")
- Assert.Fail ("#10");
- if ((TaskCreationOptions) c != TaskCreationOptions.AttachedToParent)
- Assert.Fail ("#11");
- Assert.IsFalse (Thread.CurrentThread.IsThreadPoolThread, "#12");
- called2 = true;
- b.Invoke (null);
- return null;
- },
- l => {
- called = true;
- },
- "h", TaskCreationOptions.AttachedToParent);
- Assert.AreEqual (TaskCreationOptions.None, task.CreationOptions, "#1");
- Assert.AreEqual (TaskCreationOptions.AttachedToParent, (TaskCreationOptions) task.AsyncState, "#2");
- Assert.IsTrue (task.Wait (5000), "#3");
- Assert.IsTrue (called, "#4");
- Assert.IsTrue (called2, "#5");
- }
- [Test]
- public void StartNewCancelled ()
- {
- var cts = new CancellationTokenSource ();
- cts.Cancel ();
- var task = factory.StartNew (() => Assert.Fail ("Should never be called"), cts.Token);
- try {
- task.Start ();
- } catch (InvalidOperationException) {
- }
- Assert.IsTrue (task.IsCanceled, "#2");
- }
- }
- }
- #endif
|