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