Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / TaskFactoryTest_T.cs
1 //
2 // TaskFactory_T_Test.cs
3 //
4 // Author:
5 //       Marek Safar <marek.safargmail.com>
6 //
7 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 //
27 //
28
29 #if NET_4_0
30
31 using System;
32 using System.Threading;
33 using System.Threading.Tasks;
34
35 using NUnit.Framework;
36 #if !MOBILE
37 using NUnit.Framework.SyntaxHelpers;
38 #endif
39
40 namespace MonoTests.System.Threading.Tasks
41 {
42         [TestFixture]
43         public class TaskFactory_T_Tests
44         {
45                 class CompletedAsyncResult : IAsyncResult
46                 {
47                         public object AsyncState
48                         {
49                                 get { throw new NotImplementedException (); }
50                         }
51
52                         public WaitHandle AsyncWaitHandle
53                         {
54                                 get { throw new NotImplementedException (); }
55                         }
56
57                         public bool CompletedSynchronously
58                         {
59                                 get { throw new NotImplementedException (); }
60                         }
61
62                         public bool IsCompleted
63                         {
64                                 get { return true; }
65                         }
66                 }
67
68                 class TestAsyncResult : IAsyncResult
69                 {
70                         WaitHandle wh = new ManualResetEvent (true);
71
72                         public object AsyncState
73                         {
74                                 get { throw new NotImplementedException (); }
75                         }
76
77                         public WaitHandle AsyncWaitHandle
78                         {
79                                 get
80                                 {
81                                         return wh;
82                                 }
83                         }
84
85                         public bool CompletedSynchronously
86                         {
87                                 get { throw new NotImplementedException (); }
88                         }
89
90                         public bool IsCompleted
91                         {
92                                 get { return false; }
93                         }
94                 }
95
96                 class TestAsyncResultCompletedSynchronously : IAsyncResult
97                 {
98                         public object AsyncState {
99                                 get {
100                                         throw new NotImplementedException ();
101                                 }
102                         }
103
104                         public WaitHandle AsyncWaitHandle {
105                                 get {
106                                         throw new NotImplementedException ();
107                                 }
108                         }
109
110                         public bool CompletedSynchronously {
111                                 get {
112                                         return true;
113                                 }
114                         }
115
116                         public bool IsCompleted {
117                                 get {
118                                         throw new NotImplementedException ();
119                                 }
120                         }
121                 }
122                 
123
124                 [SetUp]
125                 public void Setup ()
126                 {
127                 }
128                 
129                 [Test]
130                 public void ConstructorTest ()
131                 {
132                         try {
133                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
134                                 Assert.Fail ("#1");
135                         } catch (ArgumentOutOfRangeException) {
136                         }
137
138                         try {
139                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
140                                 Assert.Fail ("#2");
141                         } catch (ArgumentOutOfRangeException) {
142                         }
143
144                         try {
145                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
146                                 Assert.Fail ("#3");
147                         } catch (ArgumentOutOfRangeException) {
148                         }
149                 }
150
151                 [Test]
152                 public void NoDefaultScheduler ()
153                 {
154                         var tf = new TaskFactory<object> ();
155                         Assert.IsNull (tf.Scheduler, "#1");
156                 }
157
158                 [Test]
159                 public void FromAsync_ArgumentsCheck ()
160                 {
161                         var factory = new TaskFactory<object> ();
162
163                         var result = new CompletedAsyncResult ();
164                         try {
165                                 factory.FromAsync (null, l => 1);
166                                 Assert.Fail ("#1");
167                         } catch (ArgumentNullException) {
168                         }
169
170                         try {
171                                 factory.FromAsync (result, null);
172                                 Assert.Fail ("#2");
173                         } catch (ArgumentNullException) {
174                         }
175
176                         try {
177                                 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
178                                 Assert.Fail ("#3");
179                         } catch (ArgumentOutOfRangeException) {
180                         }
181
182                         try {
183                                 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
184                                 Assert.Fail ("#4");
185                         } catch (ArgumentOutOfRangeException) {
186                         }
187
188                         try {
189                                 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
190                                 Assert.Fail ("#5");
191                         } catch (ArgumentNullException) {
192                         }
193
194                         try {
195                                 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
196                                 Assert.Fail ("#6");
197                         } catch (ArgumentNullException) {
198                         }
199                 }
200
201                 [Test]
202                 public void FromAsync_SimpleAsyncResult ()
203                 {
204                         var result = new TestAsyncResult ();
205
206                         var factory = new TaskFactory<int> ();
207                         var task = factory.FromAsync (result, l => 5);
208
209                         Assert.IsTrue (task.Wait (1000), "#1");
210                         Assert.AreEqual (5, task.Result, "#2");
211                 }
212
213                 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously (AsyncCallback cb, object obj)
214                 {
215                         return new TestAsyncResultCompletedSynchronously ();
216                 }
217
218                 string EndGetTestAsyncResultCompletedSynchronously (IAsyncResult res)
219                 {
220                         return "1";
221                 }
222
223                 [Test]
224                 public void FromAsync_CompletedSynchronously ()
225                 {
226                         var factory = new TaskFactory<string> ();
227                         var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously, EndGetTestAsyncResultCompletedSynchronously, null);
228
229                         Assert.IsTrue (task.Wait (1000), "#1");
230                         Assert.AreEqual ("1", task.Result, "#2");
231                 }
232
233                 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously2 (AsyncCallback cb, object obj)
234                 {
235                         var result = new TestAsyncResultCompletedSynchronously ();
236                         cb (result);
237                         return result;
238                 }
239                 
240                 string EndGetTestAsyncResultCompletedSynchronously2 (IAsyncResult res)
241                 {
242                         return "1";
243                 }
244                 
245                 [Test]
246                 public void FromAsync_CompletedSynchronously_with_Callback ()
247                 {
248                         var factory = new TaskFactory<string> ();
249                         var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously2, EndGetTestAsyncResultCompletedSynchronously2, null);
250                         
251                         Assert.IsTrue (task.Wait (1000), "#1");
252                         Assert.AreEqual ("1", task.Result, "#2");
253                 }
254
255                 [Test]
256                 public void StartNewCancelled ()
257                 {
258                         var ct = new CancellationToken (true);
259                         var factory = new TaskFactory<int> ();
260
261                         var task = factory.StartNew (() => { Assert.Fail ("Should never be called"); return 1; }, ct);
262                         try {
263                                 task.Start ();
264                                 Assert.Fail ("#1");
265                         } catch (InvalidOperationException) {
266                         }
267
268                         Assert.IsTrue (task.IsCanceled, "#2");
269
270                         task = factory.StartNew (() => 1, ct);
271                         try {
272                                 task.Wait ();
273                         } catch (AggregateException e) {
274                                 Assert.IsTrue (task.IsCanceled, "#3");
275                                 Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#4");
276                         }
277                 }
278         }
279 }
280
281 #endif