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
99 public void ConstructorTest ()
102 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
104 } catch (ArgumentOutOfRangeException) {
108 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
110 } catch (ArgumentOutOfRangeException) {
114 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
116 } catch (ArgumentOutOfRangeException) {
121 public void NoDefaultScheduler ()
123 var tf = new TaskFactory<object> ();
124 Assert.IsNull (tf.Scheduler, "#1");
128 public void FromAsync_ArgumentsCheck ()
130 var factory = new TaskFactory<object> ();
132 var result = new CompletedAsyncResult ();
134 factory.FromAsync (null, l => 1);
136 } catch (ArgumentNullException) {
140 factory.FromAsync (result, null);
142 } catch (ArgumentNullException) {
146 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
148 } catch (ArgumentOutOfRangeException) {
152 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
154 } catch (ArgumentOutOfRangeException) {
158 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
160 } catch (ArgumentNullException) {
164 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
166 } catch (ArgumentNullException) {
171 public void FromAsync_SimpleAsyncResult ()
173 var result = new TestAsyncResult ();
175 var factory = new TaskFactory<int> ();
176 var task = factory.FromAsync (result, l => 5);
178 Assert.IsTrue (task.Wait (1000), "#1");
179 Assert.AreEqual (5, task.Result, "#2");