4 // Copyright (c) 2008 Jérémie "Garuma" Laval
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Threading;
28 using System.Threading.Tasks;
30 using NUnit.Framework;
32 namespace MonoTests.System.Threading.Tasks
35 [Category ("NotWorking")] //This hangs on my 4-core machine 100% of the time
36 public class TaskTests
39 static readonly int max = 3 * Environment.ProcessorCount;
44 tasks = new Task[max];
47 void InitWithDelegate(Action action)
49 for (int i = 0; i < max; i++) {
50 tasks[i] = Task.Factory.StartNew(action);
55 public void WaitAnyTest()
57 ParallelTestHelper.Repeat (delegate {
61 InitWithDelegate(delegate {
62 int times = Interlocked.Exchange (ref flag, 1);
64 SpinWait sw = new SpinWait ();
65 while (finished == 0) sw.SpinOnce ();
67 Interlocked.Increment (ref finished);
71 int index = Task.WaitAny(tasks);
73 Assert.IsTrue (flag == 1, "#1");
74 Assert.AreEqual (1, finished, "#2");
75 Assert.AreNotEqual (-1, index, "#3");
82 public void WaitAllTest()
84 ParallelTestHelper.Repeat (delegate {
86 InitWithDelegate(delegate { Interlocked.Increment(ref achieved); });
88 Assert.AreEqual(max, achieved, "#1");
93 public void CancelTestCase()
97 CancellationTokenSource src = new CancellationTokenSource ();
99 Task t = new Task (delegate {
109 } catch (Exception e) {
113 Assert.IsNotNull (ex, "#1");
114 Assert.IsInstanceOfType (typeof(AggregateException), t.Exception, "#2");
115 Assert.AreEqual (t.Exception, ex, "#3");
117 AggregateException aggr = (AggregateException)ex;
118 Assert.AreEqual (1, aggr.InnerExceptions.Count, "#4");
119 Assert.IsInstanceOfType (typeof (OperationCanceledException), aggr.InnerExceptions[0], "#5");
123 public void ContinueWithOnAnyTestCase()
125 ParallelTestHelper.Repeat (delegate {
128 Task t = Task.Factory.StartNew(delegate { });
129 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.None);
133 Assert.IsNull(cont.Exception, "#1");
134 Assert.IsNotNull(cont, "#2");
135 Assert.IsTrue(result, "#3");
140 public void ContinueWithOnCompletedSuccessfullyTestCase()
142 ParallelTestHelper.Repeat (delegate {
145 Task t = Task.Factory.StartNew(delegate { });
146 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion);
150 Assert.IsNull(cont.Exception, "#1");
151 Assert.IsNotNull(cont, "#2");
152 Assert.IsTrue(result, "#3");
157 public void ContinueWithOnAbortedTestCase()
159 ParallelTestHelper.Repeat (delegate {
161 bool taskResult = false;
163 CancellationTokenSource src = new CancellationTokenSource ();
164 Task t = new Task(delegate { taskResult = true; }, src.Token);
167 Task cont = t.ContinueWith (delegate { result = true; }, TaskContinuationOptions.OnlyOnCanceled);
172 Assert.IsFalse (taskResult, "#-1");
173 Assert.AreEqual (TaskStatus.Canceled, t.Status, "#0");
174 Assert.IsTrue (t.IsCanceled, "#0bis");
176 Assert.IsNull(cont.Exception, "#1");
177 Assert.IsNotNull(cont, "#2");
178 Assert.IsTrue(result, "#3");
183 public void ContinueWithOnFailedTestCase()
185 ParallelTestHelper.Repeat (delegate {
188 Task t = Task.Factory.StartNew(delegate { throw new Exception("foo"); });
189 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnFaulted);
193 Assert.IsNotNull (t.Exception, "#1");
194 Assert.IsNotNull (cont, "#2");
195 Assert.IsTrue (result, "#3");
200 public void MultipleTaskTestCase()
202 ParallelTestHelper.Repeat (delegate {
203 bool r1 = false, r2 = false, r3 = false;
205 Task t1 = Task.Factory.StartNew(delegate {
208 Task t2 = Task.Factory.StartNew(delegate {
211 Task t3 = Task.Factory.StartNew(delegate {
219 Assert.IsTrue(r1, "#1");
220 Assert.IsTrue(r2, "#2");
221 Assert.IsTrue(r3, "#3");
226 public void WaitChildTestCase()
228 ParallelTestHelper.Repeat (delegate {
229 bool r1 = false, r2 = false, r3 = false;
231 Task t = Task.Factory.StartNew(delegate {
232 Task.Factory.StartNew(delegate {
235 }, TaskCreationOptions.AttachedToParent);
236 Task.Factory.StartNew(delegate {
240 }, TaskCreationOptions.AttachedToParent);
241 Task.Factory.StartNew(delegate {
245 }, TaskCreationOptions.AttachedToParent);
249 Assert.IsTrue(r2, "#1");
250 Assert.IsTrue(r3, "#2");
251 Assert.IsTrue(r1, "#3");
252 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#4");