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 using NUnit.Framework.SyntaxHelpers;
40 namespace MonoTests.System.Threading.Tasks
43 public class TaskFactory_T_Tests
45 class CompletedAsyncResult : IAsyncResult
47 public object AsyncState
49 get { throw new NotImplementedException (); }
52 public WaitHandle AsyncWaitHandle
54 get { throw new NotImplementedException (); }
57 public bool CompletedSynchronously
59 get { throw new NotImplementedException (); }
62 public bool IsCompleted
68 class TestAsyncResult : IAsyncResult
70 WaitHandle wh = new ManualResetEvent (true);
72 public object AsyncState
74 get { throw new NotImplementedException (); }
77 public WaitHandle AsyncWaitHandle
85 public bool CompletedSynchronously
87 get { throw new NotImplementedException (); }
90 public bool IsCompleted
96 class TestAsyncResultCompletedSynchronously : IAsyncResult
98 public object AsyncState {
100 throw new NotImplementedException ();
104 public WaitHandle AsyncWaitHandle {
106 throw new NotImplementedException ();
110 public bool CompletedSynchronously {
116 public bool IsCompleted {
118 throw new NotImplementedException ();
130 public void ConstructorTest ()
133 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
135 } catch (ArgumentOutOfRangeException) {
139 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
141 } catch (ArgumentOutOfRangeException) {
145 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
147 } catch (ArgumentOutOfRangeException) {
152 public void NoDefaultScheduler ()
154 var tf = new TaskFactory<object> ();
155 Assert.IsNull (tf.Scheduler, "#1");
159 public void FromAsync_ArgumentsCheck ()
161 var factory = new TaskFactory<object> ();
163 var result = new CompletedAsyncResult ();
165 factory.FromAsync (null, l => 1);
167 } catch (ArgumentNullException) {
171 factory.FromAsync (result, null);
173 } catch (ArgumentNullException) {
177 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
179 } catch (ArgumentOutOfRangeException) {
183 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
185 } catch (ArgumentOutOfRangeException) {
189 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
191 } catch (ArgumentNullException) {
195 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
197 } catch (ArgumentNullException) {
202 public void FromAsync_SimpleAsyncResult ()
204 var result = new TestAsyncResult ();
206 var factory = new TaskFactory<int> ();
207 var task = factory.FromAsync (result, l => 5);
209 Assert.IsTrue (task.Wait (1000), "#1");
210 Assert.AreEqual (5, task.Result, "#2");
213 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously (AsyncCallback cb, object obj)
215 return new TestAsyncResultCompletedSynchronously ();
218 string EndGetTestAsyncResultCompletedSynchronously (IAsyncResult res)
224 public void FromAsync_CompletedSynchronously ()
226 var factory = new TaskFactory<string> ();
227 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously, EndGetTestAsyncResultCompletedSynchronously, null);
229 Assert.IsTrue (task.Wait (1000), "#1");
230 Assert.AreEqual ("1", task.Result, "#2");
233 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously2 (AsyncCallback cb, object obj)
235 var result = new TestAsyncResultCompletedSynchronously ();
240 string EndGetTestAsyncResultCompletedSynchronously2 (IAsyncResult res)
246 public void FromAsync_CompletedSynchronously_with_Callback ()
248 var factory = new TaskFactory<string> ();
249 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously2, EndGetTestAsyncResultCompletedSynchronously2, null);
251 Assert.IsTrue (task.Wait (1000), "#1");
252 Assert.AreEqual ("1", task.Result, "#2");
256 public void StartNewCancelled ()
258 var ct = new CancellationToken (true);
259 var factory = new TaskFactory<int> ();
261 var task = factory.StartNew (() => { Assert.Fail ("Should never be called"); return 1; }, ct);
265 } catch (InvalidOperationException) {
271 } catch (AggregateException e) {
272 Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
275 Assert.IsTrue (task.IsCanceled, "#4");