5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Threading;
33 using System.Threading.Tasks;
34 using NUnit.Framework;
35 using System.Runtime.CompilerServices;
36 using System.Collections.Generic;
37 using System.Collections;
39 namespace MonoTests.System.Runtime.CompilerServices
42 public class TaskAwaiterTest
44 class Scheduler : TaskScheduler
48 public Scheduler (string name)
53 public int InlineCalls { get; set; }
54 public int QueueCalls { get; set; }
56 protected override IEnumerable<Task> GetScheduledTasks ()
58 throw new NotImplementedException ();
61 protected override void QueueTask (Task task)
64 ThreadPool.QueueUserWorkItem (o => {
65 TryExecuteTask (task);
69 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
76 class SingleThreadSynchronizationContext : SynchronizationContext
78 readonly Queue _queue = new Queue ();
80 public void RunOnCurrentThread ()
82 while (_queue.Count != 0) {
83 var workItem = (KeyValuePair<SendOrPostCallback, object>) _queue.Dequeue ();
84 workItem.Key (workItem.Value);
88 public override void Post (SendOrPostCallback d, object state)
91 throw new ArgumentNullException ("d");
94 _queue.Enqueue (new KeyValuePair<SendOrPostCallback, object> (d, state));
97 public override void Send (SendOrPostCallback d, object state)
99 throw new NotSupportedException ("Synchronously sending is not supported.");
104 SynchronizationContext sc;
109 sc = SynchronizationContext.Current;
113 public void TearDown ()
115 SynchronizationContext.SetSynchronizationContext (sc);
119 public void GetResultFaulted ()
123 var task = new Task (() => { throw new ApplicationException (); });
124 awaiter = task.GetAwaiter ();
125 task.RunSynchronously (TaskScheduler.Current);
128 Assert.IsTrue (awaiter.IsCompleted);
131 awaiter.GetResult ();
133 } catch (ApplicationException) {
138 public void GetResultCanceled ()
142 var token = new CancellationToken (true);
143 var task = new Task (() => { }, token);
144 awaiter = task.GetAwaiter ();
147 awaiter.GetResult ();
149 } catch (TaskCanceledException) {
154 public void GetResultWaitOnCompletion ()
158 var task = Task.Delay (30);
159 awaiter = task.GetAwaiter ();
161 awaiter.GetResult ();
162 Assert.AreEqual (TaskStatus.RanToCompletion, task.Status);
166 public void CustomScheduler ()
168 // some test runners (e.g. Touch.Unit) will execute this on the main thread and that would lock them
169 if (!Thread.CurrentThread.IsBackground)
170 Assert.Ignore ("Current thread is not running in the background.");
172 var a = new Scheduler ("a");
173 var b = new Scheduler ("b");
175 var t = TestCS (a, b);
176 Assert.IsTrue (t.Wait (3000), "#0");
177 Assert.AreEqual (0, t.Result, "#1");
178 Assert.AreEqual (0, b.InlineCalls, "#2b");
179 Assert.AreEqual (2, a.QueueCalls, "#3a");
180 Assert.AreEqual (1, b.QueueCalls, "#3b");
183 static async Task<int> TestCS (TaskScheduler schedulerA, TaskScheduler schedulerB)
185 var res = await Task.Factory.StartNew (async () => {
186 if (TaskScheduler.Current != schedulerA)
189 await Task.Factory.StartNew (
191 if (TaskScheduler.Current != schedulerB)
195 }, CancellationToken.None, TaskCreationOptions.None, schedulerB);
197 if (TaskScheduler.Current != schedulerA)
201 }, CancellationToken.None, TaskCreationOptions.None, schedulerA);
207 public void FinishedTaskOnCompleted ()
209 var mres = new ManualResetEvent (false);
210 var mres2 = new ManualResetEvent (false);
212 var tcs = new TaskCompletionSource<object> ();
213 tcs.SetResult (null);
216 var awaiter = task.GetAwaiter ();
217 Assert.IsTrue (awaiter.IsCompleted, "#1");
219 awaiter.OnCompleted(() => {
220 if (mres.WaitOne (1000))
225 // this will only terminate correctly if the test was not executed from the main thread
226 // e.g. Touch.Unit defaults to run tests on the main thread and this will return false
227 Assert.AreEqual (Thread.CurrentThread.IsBackground, mres2.WaitOne (2000), "#2");;
231 public void CompletionOnSameCustomSynchronizationContext ()
234 var syncContext = new SingleThreadSynchronizationContext ();
235 SynchronizationContext.SetSynchronizationContext (syncContext);
237 syncContext.Post (delegate {
241 // Custom message loop
242 var cts = new CancellationTokenSource ();
243 cts.CancelAfter (5000);
244 while (progress.Length != 3 && !cts.IsCancellationRequested) {
245 syncContext.RunOnCurrentThread ();
249 Assert.AreEqual ("123", progress);
252 async void Go (SynchronizationContext ctx)
259 async Task Wait (SynchronizationContext ctx)
261 await Task.Delay (10); // Force block suspend/return
263 ctx.Post (l => progress += "3", null);
267 // Exiting same context - no need to post continuation
271 public void CompletionOnDifferentCustomSynchronizationContext ()
274 var syncContext = new SingleThreadSynchronizationContext ();
275 SynchronizationContext.SetSynchronizationContext (syncContext);
277 syncContext.Post (delegate {
281 // Custom message loop
282 var cts = new CancellationTokenSource ();
283 cts.CancelAfter (5000);
284 while (progress.Length != 3 && !cts.IsCancellationRequested) {
285 syncContext.RunOnCurrentThread ();
289 Assert.AreEqual ("132", progress);
292 async void Go2 (SynchronizationContext ctx)
299 async Task Wait2 (SynchronizationContext ctx)
301 await Task.Delay (10); // Force block suspend/return
303 ctx.Post (l => progress += "3", null);
307 SynchronizationContext.SetSynchronizationContext (null);