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