[bcl] Remove NET_4_0 defines from class libs
[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
30 using System;
31 using System.Threading;
32 using System.Threading.Tasks;
33
34 using NUnit.Framework;
35 #if !MOBILE
36 using NUnit.Framework.SyntaxHelpers;
37 #endif
38
39 namespace MonoTests.System.Threading.Tasks
40 {
41         [TestFixture]
42         public class TaskFactory_T_Tests
43         {
44                 class CompletedAsyncResult : IAsyncResult
45                 {
46                         public object AsyncState
47                         {
48                                 get { throw new NotImplementedException (); }
49                         }
50
51                         public WaitHandle AsyncWaitHandle
52                         {
53                                 get { throw new NotImplementedException (); }
54                         }
55
56                         public bool CompletedSynchronously
57                         {
58                                 get { throw new NotImplementedException (); }
59                         }
60
61                         public bool IsCompleted
62                         {
63                                 get { return true; }
64                         }
65                 }
66
67                 class TestAsyncResult : IAsyncResult
68                 {
69                         WaitHandle wh = new ManualResetEvent (true);
70
71                         public object AsyncState
72                         {
73                                 get { throw new NotImplementedException (); }
74                         }
75
76                         public WaitHandle AsyncWaitHandle
77                         {
78                                 get
79                                 {
80                                         return wh;
81                                 }
82                         }
83
84                         public bool CompletedSynchronously
85                         {
86                                 get { throw new NotImplementedException (); }
87                         }
88
89                         public bool IsCompleted
90                         {
91                                 get { return false; }
92                         }
93                 }
94
95                 class TestAsyncResultCompletedSynchronously : IAsyncResult
96                 {
97                         public object AsyncState {
98                                 get {
99                                         throw new NotImplementedException ();
100                                 }
101                         }
102
103                         public WaitHandle AsyncWaitHandle {
104                                 get {
105                                         throw new NotImplementedException ();
106                                 }
107                         }
108
109                         public bool CompletedSynchronously {
110                                 get {
111                                         return true;
112                                 }
113                         }
114
115                         public bool IsCompleted {
116                                 get {
117                                         throw new NotImplementedException ();
118                                 }
119                         }
120                 }
121                 
122
123                 [SetUp]
124                 public void Setup ()
125                 {
126                 }
127                 
128                 [Test]
129                 public void ConstructorTest ()
130                 {
131                         try {
132                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
133                                 Assert.Fail ("#1");
134                         } catch (ArgumentOutOfRangeException) {
135                         }
136
137                         try {
138                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
139                                 Assert.Fail ("#2");
140                         } catch (ArgumentOutOfRangeException) {
141                         }
142
143                         try {
144                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
145                                 Assert.Fail ("#3");
146                         } catch (ArgumentOutOfRangeException) {
147                         }
148                 }
149
150                 [Test]
151                 public void NoDefaultScheduler ()
152                 {
153                         var tf = new TaskFactory<object> ();
154                         Assert.IsNull (tf.Scheduler, "#1");
155                 }
156
157                 [Test]
158                 public void FromAsync_ArgumentsCheck ()
159                 {
160                         var factory = new TaskFactory<object> ();
161
162                         var result = new CompletedAsyncResult ();
163                         try {
164                                 factory.FromAsync (null, l => 1);
165                                 Assert.Fail ("#1");
166                         } catch (ArgumentNullException) {
167                         }
168
169                         try {
170                                 factory.FromAsync (result, null);
171                                 Assert.Fail ("#2");
172                         } catch (ArgumentNullException) {
173                         }
174
175                         try {
176                                 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
177                                 Assert.Fail ("#3");
178                         } catch (ArgumentOutOfRangeException) {
179                         }
180
181                         try {
182                                 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
183                                 Assert.Fail ("#4");
184                         } catch (ArgumentOutOfRangeException) {
185                         }
186
187                         try {
188                                 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
189                                 Assert.Fail ("#5");
190                         } catch (ArgumentNullException) {
191                         }
192
193                         try {
194                                 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
195                                 Assert.Fail ("#6");
196                         } catch (ArgumentNullException) {
197                         }
198                 }
199
200                 [Test]
201                 public void FromAsync_SimpleAsyncResult ()
202                 {
203                         var result = new TestAsyncResult ();
204
205                         var factory = new TaskFactory<int> ();
206                         var task = factory.FromAsync (result, l => 5);
207
208                         Assert.IsTrue (task.Wait (1000), "#1");
209                         Assert.AreEqual (5, task.Result, "#2");
210                 }
211
212                 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously (AsyncCallback cb, object obj)
213                 {
214                         return new TestAsyncResultCompletedSynchronously ();
215                 }
216
217                 string EndGetTestAsyncResultCompletedSynchronously (IAsyncResult res)
218                 {
219                         return "1";
220                 }
221
222                 [Test]
223                 public void FromAsync_CompletedSynchronously ()
224                 {
225                         var factory = new TaskFactory<string> ();
226                         var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously, EndGetTestAsyncResultCompletedSynchronously, null);
227
228                         Assert.IsTrue (task.Wait (1000), "#1");
229                         Assert.AreEqual ("1", task.Result, "#2");
230                 }
231
232                 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously2 (AsyncCallback cb, object obj)
233                 {
234                         var result = new TestAsyncResultCompletedSynchronously ();
235                         cb (result);
236                         return result;
237                 }
238                 
239                 string EndGetTestAsyncResultCompletedSynchronously2 (IAsyncResult res)
240                 {
241                         return "1";
242                 }
243                 
244                 [Test]
245                 public void FromAsync_CompletedSynchronously_with_Callback ()
246                 {
247                         var factory = new TaskFactory<string> ();
248                         var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously2, EndGetTestAsyncResultCompletedSynchronously2, null);
249                         
250                         Assert.IsTrue (task.Wait (1000), "#1");
251                         Assert.AreEqual ("1", task.Result, "#2");
252                 }
253
254                 [Test]
255                 public void StartNewCancelled ()
256                 {
257                         var ct = new CancellationToken (true);
258                         var factory = new TaskFactory<int> ();
259
260                         var task = factory.StartNew (() => { Assert.Fail ("Should never be called"); return 1; }, ct);
261                         try {
262                                 task.Start ();
263                                 Assert.Fail ("#1");
264                         } catch (InvalidOperationException) {
265                         }
266
267                         try {
268                                 task.Wait ();
269                                 Assert.Fail ("#2");
270                         } catch (AggregateException e) {
271                                 Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
272                         }
273
274                         Assert.IsTrue (task.IsCanceled, "#4");
275                 }
276         }
277 }
278