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
31 using System.Threading;
32 using System.Threading.Tasks;
34 using NUnit.Framework;
36 namespace MonoTests.System.Threading.Tasks
39 public class TaskFactory_T_Tests
41 class CompletedAsyncResult : IAsyncResult
43 public object AsyncState
45 get { throw new NotImplementedException (); }
48 public WaitHandle AsyncWaitHandle
50 get { throw new NotImplementedException (); }
53 public bool CompletedSynchronously
55 get { throw new NotImplementedException (); }
58 public bool IsCompleted
64 class TestAsyncResult : IAsyncResult
66 WaitHandle wh = new ManualResetEvent (true);
68 public object AsyncState
70 get { throw new NotImplementedException (); }
73 public WaitHandle AsyncWaitHandle
81 public bool CompletedSynchronously
83 get { throw new NotImplementedException (); }
86 public bool IsCompleted
92 class TestAsyncResultCompletedSynchronously : IAsyncResult
94 public object AsyncState {
96 throw new NotImplementedException ();
100 public WaitHandle AsyncWaitHandle {
102 throw new NotImplementedException ();
106 public bool CompletedSynchronously {
112 public bool IsCompleted {
114 throw new NotImplementedException ();
126 public void ConstructorTest ()
129 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
131 } catch (ArgumentOutOfRangeException) {
135 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
137 } catch (ArgumentOutOfRangeException) {
141 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
143 } catch (ArgumentOutOfRangeException) {
148 public void NoDefaultScheduler ()
150 var tf = new TaskFactory<object> ();
151 Assert.IsNull (tf.Scheduler, "#1");
155 public void FromAsync_ArgumentsCheck ()
157 var factory = new TaskFactory<object> ();
159 var result = new CompletedAsyncResult ();
161 factory.FromAsync (null, l => 1);
163 } catch (ArgumentNullException) {
167 factory.FromAsync (result, null);
169 } catch (ArgumentNullException) {
173 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
175 } catch (ArgumentOutOfRangeException) {
179 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
181 } catch (ArgumentOutOfRangeException) {
185 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
187 } catch (ArgumentNullException) {
191 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
193 } catch (ArgumentNullException) {
198 public void FromAsync_SimpleAsyncResult ()
200 var result = new TestAsyncResult ();
202 var factory = new TaskFactory<int> ();
203 var task = factory.FromAsync (result, l => 5);
205 Assert.IsTrue (task.Wait (1000), "#1");
206 Assert.AreEqual (5, task.Result, "#2");
209 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously (AsyncCallback cb, object obj)
211 return new TestAsyncResultCompletedSynchronously ();
214 string EndGetTestAsyncResultCompletedSynchronously (IAsyncResult res)
220 public void FromAsync_CompletedSynchronously ()
222 var factory = new TaskFactory<string> ();
223 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously, EndGetTestAsyncResultCompletedSynchronously, null);
225 Assert.IsTrue (task.Wait (1000), "#1");
226 Assert.AreEqual ("1", task.Result, "#2");
229 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously2 (AsyncCallback cb, object obj)
231 var result = new TestAsyncResultCompletedSynchronously ();
236 string EndGetTestAsyncResultCompletedSynchronously2 (IAsyncResult res)
242 public void FromAsync_CompletedSynchronously_with_Callback ()
244 var factory = new TaskFactory<string> ();
245 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously2, EndGetTestAsyncResultCompletedSynchronously2, null);
247 Assert.IsTrue (task.Wait (1000), "#1");
248 Assert.AreEqual ("1", task.Result, "#2");
252 public void StartNewCancelled ()
254 var ct = new CancellationToken (true);
255 var factory = new TaskFactory<int> ();
257 var task = factory.StartNew (() => { Assert.Fail ("Should never be called"); return 1; }, ct);
261 } catch (InvalidOperationException) {
267 } catch (AggregateException e) {
268 Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
271 Assert.IsTrue (task.IsCanceled, "#4");