2 // TaskFactory_T_Test.cs
5 // Marek Safar <marek.safargmail.com>
7 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
32 using System.Threading;
33 using System.Threading.Tasks;
35 using NUnit.Framework;
37 namespace MonoTests.System.Threading.Tasks
40 public class TaskFactory_T_Tests
42 class CompletedAsyncResult : IAsyncResult
44 public object AsyncState
46 get { throw new NotImplementedException (); }
49 public WaitHandle AsyncWaitHandle
51 get { throw new NotImplementedException (); }
54 public bool CompletedSynchronously
56 get { throw new NotImplementedException (); }
59 public bool IsCompleted
65 class TestAsyncResult : IAsyncResult
67 WaitHandle wh = new ManualResetEvent (true);
69 public object AsyncState
71 get { throw new NotImplementedException (); }
74 public WaitHandle AsyncWaitHandle
82 public bool CompletedSynchronously
84 get { throw new NotImplementedException (); }
87 public bool IsCompleted
93 class TestAsyncResultCompletedSynchronously : IAsyncResult
95 public object AsyncState {
97 throw new NotImplementedException ();
101 public WaitHandle AsyncWaitHandle {
103 throw new NotImplementedException ();
107 public bool CompletedSynchronously {
113 public bool IsCompleted {
115 throw new NotImplementedException ();
127 public void ConstructorTest ()
130 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
132 } catch (ArgumentOutOfRangeException) {
136 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
138 } catch (ArgumentOutOfRangeException) {
142 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
144 } catch (ArgumentOutOfRangeException) {
149 public void NoDefaultScheduler ()
151 var tf = new TaskFactory<object> ();
152 Assert.IsNull (tf.Scheduler, "#1");
156 public void FromAsync_ArgumentsCheck ()
158 var factory = new TaskFactory<object> ();
160 var result = new CompletedAsyncResult ();
162 factory.FromAsync (null, l => 1);
164 } catch (ArgumentNullException) {
168 factory.FromAsync (result, null);
170 } catch (ArgumentNullException) {
174 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
176 } catch (ArgumentOutOfRangeException) {
180 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
182 } catch (ArgumentOutOfRangeException) {
186 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
188 } catch (ArgumentNullException) {
192 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
194 } catch (ArgumentNullException) {
199 public void FromAsync_SimpleAsyncResult ()
201 var result = new TestAsyncResult ();
203 var factory = new TaskFactory<int> ();
204 var task = factory.FromAsync (result, l => 5);
206 Assert.IsTrue (task.Wait (1000), "#1");
207 Assert.AreEqual (5, task.Result, "#2");
210 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously (AsyncCallback cb, object obj)
212 return new TestAsyncResultCompletedSynchronously ();
215 string EndGetTestAsyncResultCompletedSynchronously (IAsyncResult res)
221 public void FromAsync_CompletedSynchronously ()
223 var factory = new TaskFactory<string> ();
224 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously, EndGetTestAsyncResultCompletedSynchronously, null);
226 Assert.IsTrue (task.Wait (1000), "#1");
227 Assert.AreEqual ("1", task.Result, "#2");
230 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously2 (AsyncCallback cb, object obj)
232 var result = new TestAsyncResultCompletedSynchronously ();
237 string EndGetTestAsyncResultCompletedSynchronously2 (IAsyncResult res)
243 public void FromAsync_CompletedSynchronously_with_Callback ()
245 var factory = new TaskFactory<string> ();
246 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously2, EndGetTestAsyncResultCompletedSynchronously2, null);
248 Assert.IsTrue (task.Wait (1000), "#1");
249 Assert.AreEqual ("1", task.Result, "#2");