Merge pull request #301 from directhex/master
[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 || MOBILE
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                 [SetUp]
94                 public void Setup ()
95                 {
96                 }
97                 
98                 [Test]
99                 public void ConstructorTest ()
100                 {
101                         try {
102                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
103                                 Assert.Fail ("#1");
104                         } catch (ArgumentOutOfRangeException) {
105                         }
106
107                         try {
108                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
109                                 Assert.Fail ("#2");
110                         } catch (ArgumentOutOfRangeException) {
111                         }
112
113                         try {
114                                 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
115                                 Assert.Fail ("#3");
116                         } catch (ArgumentOutOfRangeException) {
117                         }
118                 }
119
120                 [Test]
121                 public void NoDefaultScheduler ()
122                 {
123                         var tf = new TaskFactory<object> ();
124                         Assert.IsNull (tf.Scheduler, "#1");
125                 }
126
127                 [Test]
128                 public void FromAsync_ArgumentsCheck ()
129                 {
130                         var factory = new TaskFactory<object> ();
131
132                         var result = new CompletedAsyncResult ();
133                         try {
134                                 factory.FromAsync (null, l => 1);
135                                 Assert.Fail ("#1");
136                         } catch (ArgumentNullException) {
137                         }
138
139                         try {
140                                 factory.FromAsync (result, null);
141                                 Assert.Fail ("#2");
142                         } catch (ArgumentNullException) {
143                         }
144
145                         try {
146                                 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
147                                 Assert.Fail ("#3");
148                         } catch (ArgumentOutOfRangeException) {
149                         }
150
151                         try {
152                                 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
153                                 Assert.Fail ("#4");
154                         } catch (ArgumentOutOfRangeException) {
155                         }
156
157                         try {
158                                 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
159                                 Assert.Fail ("#5");
160                         } catch (ArgumentNullException) {
161                         }
162
163                         try {
164                                 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
165                                 Assert.Fail ("#6");
166                         } catch (ArgumentNullException) {
167                         }
168                 }
169
170                 [Test]
171                 public void FromAsync_SimpleAsyncResult ()
172                 {
173                         var result = new TestAsyncResult ();
174
175                         var factory = new TaskFactory<int> ();
176                         var task = factory.FromAsync (result, l => 5);
177
178                         Assert.IsTrue (task.Wait (1000), "#1");
179                         Assert.AreEqual (5, task.Result, "#2");
180                 }
181         }
182 }
183
184 #endif