Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskTest.cs
1 #if NET_4_0
2 // TaskTest.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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
22 // THE SOFTWARE.
23 //
24 //
25
26 using System;
27 using System.Threading;
28 using System.Threading.Tasks;
29
30 using NUnit.Framework;
31
32 namespace MonoTests.System.Threading.Tasks
33 {
34         [TestFixture]
35         [Category ("NotWorking")] //This hangs on my 4-core machine 100% of the time
36         public class TaskTests
37         {
38                 Task[]      tasks;
39                 static readonly int max = 3 * Environment.ProcessorCount;
40                 
41                 [SetUp]
42                 public void Setup()
43                 {
44                         tasks = new Task[max];                  
45                 }
46                 
47                 void InitWithDelegate(Action action)
48                 {
49                         for (int i = 0; i < max; i++) {
50                                 tasks[i] = Task.Factory.StartNew(action);
51                         }
52                 }
53                 
54                 [TestAttribute]
55                 public void WaitAnyTest()
56                 {
57                         ParallelTestHelper.Repeat (delegate {
58                                 int flag = 0;
59                                 int finished = 0;
60                                 
61                                 InitWithDelegate(delegate {
62                                         int times = Interlocked.Exchange (ref flag, 1);
63                                         if (times == 1) {
64                                                 SpinWait sw = new SpinWait ();
65                                                 while (finished == 0) sw.SpinOnce ();
66                                         } else {
67                                                 Interlocked.Increment (ref finished);
68                                         }
69                                 });
70                                 
71                                 int index = Task.WaitAny(tasks);
72                                 
73                                 Assert.IsTrue (flag == 1, "#1");
74                                 Assert.AreEqual (1, finished, "#2");
75                                 Assert.AreNotEqual (-1, index, "#3");
76                                 
77                                 Task.WaitAll (tasks);
78                         });
79                 }
80                 
81                 [TestAttribute]
82                 public void WaitAllTest()
83                 {
84                         ParallelTestHelper.Repeat (delegate {
85                                 int achieved = 0;
86                                 InitWithDelegate(delegate { Interlocked.Increment(ref achieved); });
87                                 Task.WaitAll(tasks);
88                                 Assert.AreEqual(max, achieved, "#1");
89                         });
90                 }
91                 
92                 [Test]
93                 public void CancelTestCase()
94                 {
95                         bool result = false;
96                         
97                         CancellationTokenSource src = new CancellationTokenSource ();
98                         
99                         Task t = new Task (delegate {
100                                 result = true;
101                         }, src.Token);
102                         src.Cancel ();
103                         
104                         t.Start ();
105                         Exception ex = null;
106                         
107                         try {
108                                 t.Wait ();
109                         } catch (Exception e) {
110                                 ex = e;
111                         }
112                         
113                         Assert.IsNotNull (ex, "#1");
114                         Assert.IsInstanceOfType (typeof(AggregateException), t.Exception, "#2");
115                         Assert.AreEqual (t.Exception, ex, "#3");
116                         
117                         AggregateException aggr = (AggregateException)ex;
118                         Assert.AreEqual (1, aggr.InnerExceptions.Count, "#4");
119                         Assert.IsInstanceOfType (typeof (OperationCanceledException), aggr.InnerExceptions[0], "#5");
120                 }
121                 
122                 [Test]
123                 public void ContinueWithOnAnyTestCase()
124                 {
125                         ParallelTestHelper.Repeat (delegate {
126                                 bool result = false;
127                                 
128                                 Task t = Task.Factory.StartNew(delegate { });
129                                 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.None);
130                                 t.Wait();
131                                 cont.Wait();
132                                 
133                                 Assert.IsNull(cont.Exception, "#1");
134                                 Assert.IsNotNull(cont, "#2");
135                                 Assert.IsTrue(result, "#3");
136                         });
137                 }
138                 
139                 [Test]
140                 public void ContinueWithOnCompletedSuccessfullyTestCase()
141                 {
142                         ParallelTestHelper.Repeat (delegate {
143                                 bool result = false;
144                                 
145                                 Task t = Task.Factory.StartNew(delegate { });
146                                 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion);
147                                 t.Wait();
148                                 cont.Wait();
149                                 
150                                 Assert.IsNull(cont.Exception, "#1");
151                                 Assert.IsNotNull(cont, "#2");
152                                 Assert.IsTrue(result, "#3");
153                         });
154                 }
155                 
156                 [Test]
157                 public void ContinueWithOnAbortedTestCase()
158                 {
159                         ParallelTestHelper.Repeat (delegate {
160                                 bool result = false;
161                                 bool taskResult = false;
162                                 
163                                 CancellationTokenSource src = new CancellationTokenSource ();
164                                 Task t = new Task(delegate { taskResult = true; }, src.Token);
165                                 src.Cancel ();
166                                 
167                                 Task cont = t.ContinueWith (delegate { result = true; }, TaskContinuationOptions.OnlyOnCanceled);
168
169                                 t.Start();
170                                 cont.Wait();
171                                 
172                                 Assert.IsFalse (taskResult, "#-1");
173                                 Assert.AreEqual (TaskStatus.Canceled, t.Status, "#0");
174                                 Assert.IsTrue (t.IsCanceled, "#0bis");
175                                 
176                                 Assert.IsNull(cont.Exception, "#1");
177                                 Assert.IsNotNull(cont, "#2");
178                                 Assert.IsTrue(result, "#3");
179                         });
180                 }
181                 
182                 [Test]
183                 public void ContinueWithOnFailedTestCase()
184                 {
185                         ParallelTestHelper.Repeat (delegate {
186                                 bool result = false;
187                                 
188                                 Task t = Task.Factory.StartNew(delegate { throw new Exception("foo"); });       
189                                 Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnFaulted);
190                         
191                                 cont.Wait();
192                                 
193                                 Assert.IsNotNull (t.Exception, "#1");
194                                 Assert.IsNotNull (cont, "#2");
195                                 Assert.IsTrue (result, "#3");
196                         });
197                 }
198
199                 [TestAttribute]
200                 public void MultipleTaskTestCase()
201                 {
202                         ParallelTestHelper.Repeat (delegate {
203                                 bool r1 = false, r2 = false, r3 = false;
204                                 
205                                 Task t1 = Task.Factory.StartNew(delegate {
206                                         r1 = true;
207                                 });
208                                 Task t2 = Task.Factory.StartNew(delegate {
209                                         r2 = true;
210                                 });
211                                 Task t3 = Task.Factory.StartNew(delegate {
212                                         r3 = true;
213                                 });
214                                 
215                                 t1.Wait();
216                                 t2.Wait();
217                                 t3.Wait();
218                                 
219                                 Assert.IsTrue(r1, "#1");
220                                 Assert.IsTrue(r2, "#2");
221                                 Assert.IsTrue(r3, "#3");
222                         });
223                 }
224                 
225                 [Test]
226                 public void WaitChildTestCase()
227                 {
228                         ParallelTestHelper.Repeat (delegate {
229                                 bool r1 = false, r2 = false, r3 = false;
230                                 
231                                 Task t = Task.Factory.StartNew(delegate {
232                                         Task.Factory.StartNew(delegate {
233                                                 Thread.Sleep(50);
234                                                 r1 = true;
235                                         }, TaskCreationOptions.AttachedToParent);
236                                         Task.Factory.StartNew(delegate {
237                                                 Thread.Sleep(300);
238                                                 
239                                                 r2 = true;
240                                         }, TaskCreationOptions.AttachedToParent);
241                                         Task.Factory.StartNew(delegate {
242                                                 Thread.Sleep(150);
243                                                 
244                                                 r3 = true;
245                                         }, TaskCreationOptions.AttachedToParent);
246                                 });
247                                 
248                                 t.Wait();
249                                 Assert.IsTrue(r2, "#1");
250                                 Assert.IsTrue(r3, "#2");
251                                 Assert.IsTrue(r1, "#3");
252                                 Assert.AreEqual (TaskStatus.RanToCompletion, t.Status, "#4");
253                         }, 10);
254                 }
255         }
256 }
257 #endif