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.
31 using System.Threading;
32 using System.Threading.Tasks;
33 using NUnit.Framework;
34 using System.Runtime.CompilerServices;
35 using System.Collections.Generic;
36 using System.Collections;
37 using System.Collections.Concurrent;
39 namespace MonoTests.System.Runtime.CompilerServices
42 public class TaskAwaiterTest
44 class Scheduler : TaskScheduler
49 public Scheduler (string name)
54 public int InlineCalls { get { return ic; } }
55 public int QueueCalls { get { return qc; } }
57 protected override IEnumerable<Task> GetScheduledTasks ()
59 throw new NotImplementedException ();
62 protected override void QueueTask (Task task)
64 Interlocked.Increment (ref qc);
65 ThreadPool.QueueUserWorkItem (o => {
66 TryExecuteTask (task);
70 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
72 Interlocked.Increment (ref ic);
76 public override string ToString ()
78 return "Scheduler-" + name;
82 class SingleThreadSynchronizationContext : SynchronizationContext
84 readonly Queue _queue = new Queue ();
86 public void RunOnCurrentThread ()
88 while (_queue.Count != 0) {
89 var workItem = (KeyValuePair<SendOrPostCallback, object>) _queue.Dequeue ();
90 workItem.Key (workItem.Value);
94 public override void Post (SendOrPostCallback d, object state)
97 throw new ArgumentNullException ("d");
100 _queue.Enqueue (new KeyValuePair<SendOrPostCallback, object> (d, state));
103 public override void Send (SendOrPostCallback d, object state)
105 throw new NotSupportedException ("Synchronously sending is not supported.");
109 class NestedSynchronizationContext : SynchronizationContext
112 readonly ConcurrentQueue<Tuple<SendOrPostCallback, object, ExecutionContext>> workQueue = new ConcurrentQueue<Tuple<SendOrPostCallback, object, ExecutionContext>> ();
113 readonly AutoResetEvent workReady = new AutoResetEvent (false);
115 public NestedSynchronizationContext ()
117 thread = new Thread (WorkerThreadProc) { IsBackground = true };
121 public override void Post (SendOrPostCallback d, object state)
123 var context = ExecutionContext.Capture ();
124 workQueue.Enqueue (Tuple.Create (d, state, context));
128 void WorkerThreadProc ()
130 if (!workReady.WaitOne (10000))
133 Tuple<SendOrPostCallback, object, ExecutionContext> work;
135 while (workQueue.TryDequeue (out work)) {
136 ExecutionContext.Run (work.Item3, _ => {
137 var oldSyncContext = SynchronizationContext.Current;
138 SynchronizationContext.SetSynchronizationContext (this);
140 SynchronizationContext.SetSynchronizationContext (oldSyncContext);
147 SynchronizationContext sc;
148 ManualResetEvent mre;
153 sc = SynchronizationContext.Current;
157 public void TearDown ()
159 SynchronizationContext.SetSynchronizationContext (sc);
163 public void GetResultFaulted ()
167 var task = new Task (() => { throw new ApplicationException (); });
168 awaiter = task.GetAwaiter ();
169 task.RunSynchronously (TaskScheduler.Current);
172 Assert.IsTrue (awaiter.IsCompleted);
175 awaiter.GetResult ();
177 } catch (ApplicationException) {
182 public void GetResultCanceled ()
186 var token = new CancellationToken (true);
187 var task = new Task (() => { }, token);
188 awaiter = task.GetAwaiter ();
191 awaiter.GetResult ();
193 } catch (TaskCanceledException) {
198 public void GetResultWaitOnCompletion ()
202 var task = Task.Delay (30);
203 awaiter = task.GetAwaiter ();
205 awaiter.GetResult ();
206 Assert.AreEqual (TaskStatus.RanToCompletion, task.Status);
210 public void CustomScheduler ()
212 // some test runners (e.g. Touch.Unit) will execute this on the main thread and that would lock them
213 if (!Thread.CurrentThread.IsBackground)
214 Assert.Ignore ("Current thread is not running in the background.");
216 var a = new Scheduler ("a");
217 var b = new Scheduler ("b");
219 var t = TestCS (a, b);
220 Assert.IsTrue (t.Wait (3000), "#0");
221 Assert.AreEqual (0, t.Result, "#1");
222 Assert.AreEqual (0, b.InlineCalls, "#2b");
223 Assert.IsTrue (a.QueueCalls == 1 || a.QueueCalls == 2, "#3a");
224 Assert.AreEqual (1, b.QueueCalls, "#3b");
227 static async Task<int> TestCS (TaskScheduler schedulerA, TaskScheduler schedulerB)
229 var res = await Task.Factory.StartNew (async () => {
230 if (TaskScheduler.Current != schedulerA)
233 await Task.Factory.StartNew (
235 if (TaskScheduler.Current != schedulerB)
239 }, CancellationToken.None, TaskCreationOptions.None, schedulerB);
241 if (TaskScheduler.Current != schedulerA)
245 }, CancellationToken.None, TaskCreationOptions.None, schedulerA);
252 public void FinishedTaskOnCompleted ()
254 var mres = new ManualResetEvent (false);
255 var mres2 = new ManualResetEvent (false);
257 var tcs = new TaskCompletionSource<object> ();
258 tcs.SetResult (null);
261 var awaiter = task.GetAwaiter ();
262 Assert.IsTrue (awaiter.IsCompleted, "#1");
264 awaiter.OnCompleted(() => {
265 if (mres.WaitOne (1000))
270 // this will only terminate correctly if the test was not executed from the main thread
271 // e.g. Touch.Unit defaults to run tests on the main thread and this will return false
272 Assert.AreEqual (Thread.CurrentThread.IsBackground, mres2.WaitOne (2000), "#2");;
278 public void CompletionOnSameCustomSynchronizationContext ()
281 var syncContext = new SingleThreadSynchronizationContext ();
282 SynchronizationContext.SetSynchronizationContext (syncContext);
284 syncContext.Post (delegate {
288 // Custom message loop
289 var cts = new CancellationTokenSource ();
290 cts.CancelAfter (5000);
291 while (progress.Length != 3 && !cts.IsCancellationRequested) {
292 syncContext.RunOnCurrentThread ();
296 Assert.AreEqual ("123", progress);
299 async void Go (SynchronizationContext ctx)
306 async Task Wait (SynchronizationContext ctx)
308 await Task.Delay (10); // Force block suspend/return
310 ctx.Post (l => progress += "3", null);
314 // Exiting same context - no need to post continuation
318 public void CompletionOnDifferentCustomSynchronizationContext ()
320 mre = new ManualResetEvent (false);
322 var syncContext = new SingleThreadSynchronizationContext ();
323 SynchronizationContext.SetSynchronizationContext (syncContext);
325 syncContext.Post (delegate {
326 Task t = new Task (delegate() { });
327 Go2 (syncContext, t);
331 // Custom message loop
332 var cts = new CancellationTokenSource ();
333 cts.CancelAfter (5000);
334 while (progress.Length != 3 && !cts.IsCancellationRequested) {
335 syncContext.RunOnCurrentThread ();
339 Assert.AreEqual ("13xa2", progress);
342 async void Go2 (SynchronizationContext ctx, Task t)
344 await Wait2 (ctx, t);
348 if (mre.WaitOne (5000))
354 async Task Wait2 (SynchronizationContext ctx, Task t)
356 await t; // Force block suspend/return
366 SynchronizationContext.SetSynchronizationContext (null);
370 public void NestedLeakingSynchronizationContext ()
372 var sc = SynchronizationContext.Current;
374 Assert.IsTrue (NestedLeakingSynchronizationContext_MainAsync (sc).Wait (5000), "#1");
376 Assert.Ignore ("NestedSynchronizationContext may never complete on custom context");
379 static async Task NestedLeakingSynchronizationContext_MainAsync (SynchronizationContext sc)
381 Assert.AreSame (sc, SynchronizationContext.Current, "#1");
382 await NestedLeakingSynchronizationContext_DoWorkAsync ();
383 Assert.AreSame (sc, SynchronizationContext.Current, "#2");
386 static async Task NestedLeakingSynchronizationContext_DoWorkAsync ()
388 var sc = new NestedSynchronizationContext ();
389 SynchronizationContext.SetSynchronizationContext (sc);
391 Assert.AreSame (sc, SynchronizationContext.Current);
393 Assert.AreSame (sc, SynchronizationContext.Current);