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 using NUnit.Framework.SyntaxHelpers;
39 namespace MonoTests.System.Threading.Tasks
42 public class TaskFactory_T_Tests
44 class CompletedAsyncResult : IAsyncResult
46 public object AsyncState
48 get { throw new NotImplementedException (); }
51 public WaitHandle AsyncWaitHandle
53 get { throw new NotImplementedException (); }
56 public bool CompletedSynchronously
58 get { throw new NotImplementedException (); }
61 public bool IsCompleted
67 class TestAsyncResult : IAsyncResult
69 WaitHandle wh = new ManualResetEvent (true);
71 public object AsyncState
73 get { throw new NotImplementedException (); }
76 public WaitHandle AsyncWaitHandle
84 public bool CompletedSynchronously
86 get { throw new NotImplementedException (); }
89 public bool IsCompleted
95 class TestAsyncResultCompletedSynchronously : IAsyncResult
97 public object AsyncState {
99 throw new NotImplementedException ();
103 public WaitHandle AsyncWaitHandle {
105 throw new NotImplementedException ();
109 public bool CompletedSynchronously {
115 public bool IsCompleted {
117 throw new NotImplementedException ();
129 public void ConstructorTest ()
132 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning);
134 } catch (ArgumentOutOfRangeException) {
138 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.OnlyOnRanToCompletion);
140 } catch (ArgumentOutOfRangeException) {
144 new TaskFactory<int> (TaskCreationOptions.None, TaskContinuationOptions.NotOnRanToCompletion);
146 } catch (ArgumentOutOfRangeException) {
151 public void NoDefaultScheduler ()
153 var tf = new TaskFactory<object> ();
154 Assert.IsNull (tf.Scheduler, "#1");
158 public void FromAsync_ArgumentsCheck ()
160 var factory = new TaskFactory<object> ();
162 var result = new CompletedAsyncResult ();
164 factory.FromAsync (null, l => 1);
166 } catch (ArgumentNullException) {
170 factory.FromAsync (result, null);
172 } catch (ArgumentNullException) {
176 factory.FromAsync (result, l => 1, TaskCreationOptions.LongRunning);
178 } catch (ArgumentOutOfRangeException) {
182 factory.FromAsync (result, l => 1, TaskCreationOptions.PreferFairness);
184 } catch (ArgumentOutOfRangeException) {
188 factory.FromAsync (result, l => 1, TaskCreationOptions.None, null);
190 } catch (ArgumentNullException) {
194 factory.FromAsync (null, l => 1, null, TaskCreationOptions.None);
196 } catch (ArgumentNullException) {
201 public void FromAsync_SimpleAsyncResult ()
203 var result = new TestAsyncResult ();
205 var factory = new TaskFactory<int> ();
206 var task = factory.FromAsync (result, l => 5);
208 Assert.IsTrue (task.Wait (1000), "#1");
209 Assert.AreEqual (5, task.Result, "#2");
212 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously (AsyncCallback cb, object obj)
214 return new TestAsyncResultCompletedSynchronously ();
217 string EndGetTestAsyncResultCompletedSynchronously (IAsyncResult res)
223 public void FromAsync_CompletedSynchronously ()
225 var factory = new TaskFactory<string> ();
226 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously, EndGetTestAsyncResultCompletedSynchronously, null);
228 Assert.IsTrue (task.Wait (1000), "#1");
229 Assert.AreEqual ("1", task.Result, "#2");
232 IAsyncResult BeginGetTestAsyncResultCompletedSynchronously2 (AsyncCallback cb, object obj)
234 var result = new TestAsyncResultCompletedSynchronously ();
239 string EndGetTestAsyncResultCompletedSynchronously2 (IAsyncResult res)
245 public void FromAsync_CompletedSynchronously_with_Callback ()
247 var factory = new TaskFactory<string> ();
248 var task = factory.FromAsync (BeginGetTestAsyncResultCompletedSynchronously2, EndGetTestAsyncResultCompletedSynchronously2, null);
250 Assert.IsTrue (task.Wait (1000), "#1");
251 Assert.AreEqual ("1", task.Result, "#2");
255 public void StartNewCancelled ()
257 var ct = new CancellationToken (true);
258 var factory = new TaskFactory<int> ();
260 var task = factory.StartNew (() => { Assert.Fail ("Should never be called"); return 1; }, ct);
264 } catch (InvalidOperationException) {
270 } catch (AggregateException e) {
271 Assert.That (e.InnerException, Is.TypeOf (typeof (TaskCanceledException)), "#3");
274 Assert.IsTrue (task.IsCanceled, "#4");