Merge pull request #2523 from akoeplinger/fix-profiler-test-reporting
[mono.git] / mcs / class / corlib / Test / System.Runtime.CompilerServices / TaskAwaiterTest.cs
1 //
2 // TaskAwaiterTest.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 #if NET_4_5
30
31 using System;
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;
38 using System.Collections.Concurrent;
39
40 namespace MonoTests.System.Runtime.CompilerServices
41 {
42         [TestFixture]
43         public class TaskAwaiterTest
44         {
45                 class Scheduler : TaskScheduler
46                 {
47                         string name;
48                         int ic, qc;
49
50                         public Scheduler (string name)
51                         {
52                                 this.name = name;
53                         }
54
55                         public int InlineCalls { get { return ic; } }
56                         public int QueueCalls { get { return qc; } }
57
58                         protected override IEnumerable<Task> GetScheduledTasks ()
59                         {
60                                 throw new NotImplementedException ();
61                         }
62
63                         protected override void QueueTask (Task task)
64                         {
65                                 Interlocked.Increment (ref qc);
66                                 ThreadPool.QueueUserWorkItem (o => {
67                                         TryExecuteTask (task);
68                                 });
69                         }
70
71                         protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
72                         {
73                                 Interlocked.Increment (ref ic);
74                                 return false;
75                         }
76
77                         public override string ToString ()
78                         {
79                                 return "Scheduler-" + name;
80                         }
81                 }
82
83                 class SingleThreadSynchronizationContext : SynchronizationContext
84                 {
85                         readonly Queue _queue = new Queue ();
86
87                         public void RunOnCurrentThread ()
88                         {
89                                 while (_queue.Count != 0) {
90                                         var workItem = (KeyValuePair<SendOrPostCallback, object>) _queue.Dequeue ();
91                                         workItem.Key (workItem.Value);
92                                 }
93                         }
94                                 
95                         public override void Post (SendOrPostCallback d, object state)
96                         {
97                                 if (d == null) {
98                                         throw new ArgumentNullException ("d");
99                                 }
100
101                                 _queue.Enqueue (new KeyValuePair<SendOrPostCallback, object> (d, state));
102                         }
103
104                         public override void Send (SendOrPostCallback d, object state)
105                         {
106                                 throw new NotSupportedException ("Synchronously sending is not supported.");
107                         }
108                 }
109
110                 class NestedSynchronizationContext : SynchronizationContext
111                 {
112                         Thread thread;
113                         readonly ConcurrentQueue<Tuple<SendOrPostCallback, object, ExecutionContext>> workQueue = new ConcurrentQueue<Tuple<SendOrPostCallback, object, ExecutionContext>> ();
114                         readonly AutoResetEvent workReady = new AutoResetEvent (false);
115
116                         public NestedSynchronizationContext ()
117                         {
118                                 thread = new Thread (WorkerThreadProc) { IsBackground = true };
119                                 thread.Start ();
120                         }
121
122                         public override void Post (SendOrPostCallback d, object state)
123                         {
124                                 var context = ExecutionContext.Capture ();
125                                 workQueue.Enqueue (Tuple.Create (d, state, context));
126                                 workReady.Set ();
127                         }
128
129                         void WorkerThreadProc ()
130                         {
131                                 if (!workReady.WaitOne (10000))
132                                         return;
133
134                                 Tuple<SendOrPostCallback, object, ExecutionContext> work;
135
136                                 while (workQueue.TryDequeue (out work)) {
137                                         ExecutionContext.Run (work.Item3, _ => {
138                                                 var oldSyncContext = SynchronizationContext.Current;
139                                                 SynchronizationContext.SetSynchronizationContext (this);
140                                                 work.Item1 (_);
141                                                 SynchronizationContext.SetSynchronizationContext (oldSyncContext);
142                                         }, work.Item2); 
143                                 }
144                         }
145                 }
146
147                 string progress;
148                 SynchronizationContext sc;
149                 ManualResetEvent mre;
150
151                 [SetUp]
152                 public void Setup ()
153                 {
154                         sc = SynchronizationContext.Current;
155                 }
156
157                 [TearDown]
158                 public void TearDown ()
159                 {
160                         SynchronizationContext.SetSynchronizationContext (sc);
161                 }
162
163                 [Test]
164                 public void GetResultFaulted ()
165                 {
166                         TaskAwaiter awaiter;
167
168                         var task = new Task (() => { throw new ApplicationException (); });
169                         awaiter = task.GetAwaiter ();
170                         task.RunSynchronously (TaskScheduler.Current);
171
172
173                         Assert.IsTrue (awaiter.IsCompleted);
174
175                         try {
176                                 awaiter.GetResult ();
177                                 Assert.Fail ();
178                         } catch (ApplicationException) {
179                         }
180                 }
181
182                 [Test]
183                 public void GetResultCanceled ()
184                 {
185                         TaskAwaiter awaiter;
186
187                         var token = new CancellationToken (true);
188                         var task = new Task (() => { }, token);
189                         awaiter = task.GetAwaiter ();
190
191                         try {
192                                 awaiter.GetResult ();
193                                 Assert.Fail ();
194                         } catch (TaskCanceledException) {
195                         }
196                 }
197
198                 [Test]
199                 public void GetResultWaitOnCompletion ()
200                 {
201                         TaskAwaiter awaiter;
202                                 
203                         var task = Task.Delay (30);
204                         awaiter = task.GetAwaiter ();
205                                 
206                         awaiter.GetResult ();
207                         Assert.AreEqual (TaskStatus.RanToCompletion, task.Status);
208                 }
209
210                 [Test]
211                 public void CustomScheduler ()
212                 {
213                         // some test runners (e.g. Touch.Unit) will execute this on the main thread and that would lock them
214                         if (!Thread.CurrentThread.IsBackground)
215                                 Assert.Ignore ("Current thread is not running in the background.");
216
217                         var a = new Scheduler ("a");
218                         var b = new Scheduler ("b");
219
220                         var t = TestCS (a, b);
221                         Assert.IsTrue (t.Wait (3000), "#0");
222                         Assert.AreEqual (0, t.Result, "#1");
223                         Assert.AreEqual (0, b.InlineCalls, "#2b");
224                         Assert.IsTrue (a.QueueCalls == 1 || a.QueueCalls == 2, "#3a");
225                         Assert.AreEqual (1, b.QueueCalls, "#3b");
226                 }
227
228                 static async Task<int> TestCS (TaskScheduler schedulerA, TaskScheduler schedulerB)
229                 {
230                         var res = await Task.Factory.StartNew (async () => {
231                                 if (TaskScheduler.Current != schedulerA)
232                                         return 1;
233
234                                 await Task.Factory.StartNew (
235                                         () => {
236                                                 if (TaskScheduler.Current != schedulerB)
237                                                         return 2;
238
239                                                 return 0;
240                                         }, CancellationToken.None, TaskCreationOptions.None, schedulerB);
241
242                                 if (TaskScheduler.Current != schedulerA)
243                                         return 3;
244
245                                 return 0;
246                         }, CancellationToken.None, TaskCreationOptions.None, schedulerA);
247
248                         return res.Result;
249                 }
250
251 #if !MOBILE_STATIC
252                 [Test]
253                 public void FinishedTaskOnCompleted ()
254                 {
255                         var mres = new ManualResetEvent (false);
256                         var mres2 = new ManualResetEvent (false);
257
258                         var tcs = new TaskCompletionSource<object> ();
259                         tcs.SetResult (null);
260                         var task = tcs.Task;
261
262                         var awaiter = task.GetAwaiter ();
263                         Assert.IsTrue (awaiter.IsCompleted, "#1");
264
265                         awaiter.OnCompleted(() => { 
266                                 if (mres.WaitOne (1000))
267                                         mres2.Set ();
268                         });
269
270                         mres.Set ();
271                         // this will only terminate correctly if the test was not executed from the main thread
272                         // e.g. Touch.Unit defaults to run tests on the main thread and this will return false
273                         Assert.AreEqual (Thread.CurrentThread.IsBackground, mres2.WaitOne (2000), "#2");;
274                 }
275
276 #endif
277
278                 [Test]
279                 public void CompletionOnSameCustomSynchronizationContext ()
280                 {
281                         progress = "";
282                         var syncContext = new SingleThreadSynchronizationContext ();
283                         SynchronizationContext.SetSynchronizationContext (syncContext);
284
285                         syncContext.Post (delegate {
286                                 Go (syncContext);
287                         }, null);
288
289                         // Custom message loop
290                         var cts = new CancellationTokenSource ();
291                         cts.CancelAfter (5000);
292                         while (progress.Length != 3 && !cts.IsCancellationRequested) {
293                                 syncContext.RunOnCurrentThread ();
294                                 Thread.Sleep (0);
295                         }
296
297                         Assert.AreEqual ("123", progress);
298                 }
299
300                 async void Go (SynchronizationContext ctx)
301                 {
302                         await Wait (ctx);
303
304                         progress += "2";
305                 }
306
307                 async Task Wait (SynchronizationContext ctx)
308                 {
309                         await Task.Delay (10); // Force block suspend/return
310
311                         ctx.Post (l => progress += "3", null);
312
313                         progress += "1";
314
315                         // Exiting same context - no need to post continuation
316                 }
317
318                 [Test]
319                 public void CompletionOnDifferentCustomSynchronizationContext ()
320                 {
321                         mre = new ManualResetEvent (false);
322                         progress = "";
323                         var syncContext = new SingleThreadSynchronizationContext ();
324                         SynchronizationContext.SetSynchronizationContext (syncContext);
325
326                         syncContext.Post (delegate {
327                                 Task t = new Task (delegate() { });
328                                 Go2 (syncContext, t);
329                                 t.Start ();
330                         }, null);
331
332                         // Custom message loop
333                         var cts = new CancellationTokenSource ();
334                         cts.CancelAfter (5000);
335                         while (progress.Length != 3 && !cts.IsCancellationRequested) {
336                                 syncContext.RunOnCurrentThread ();
337                                 Thread.Sleep (0);
338                         }
339
340                         Assert.AreEqual ("13xa2", progress);
341                 }
342
343                 async void Go2 (SynchronizationContext ctx, Task t)
344                 {
345                         await Wait2 (ctx, t);
346
347                         progress += "a";
348
349                         if (mre.WaitOne (5000))
350                                 progress += "2";
351                         else
352                                 progress += "b";
353                 }
354
355                 async Task Wait2 (SynchronizationContext ctx, Task t)
356                 {
357                         await t; // Force block suspend/return
358
359                         ctx.Post (l => {
360                                 progress += "3";
361                                 mre.Set ();
362                                 progress += "x";
363                         }, null);
364
365                         progress += "1";
366
367                         SynchronizationContext.SetSynchronizationContext (null);
368                 }
369
370                 [Test]
371                 public void NestedLeakingSynchronizationContext ()
372                 {
373                         var sc = SynchronizationContext.Current;
374                         if (sc == null)
375                                 Assert.IsTrue (NestedLeakingSynchronizationContext_MainAsync (sc).Wait (5000), "#1");
376                         else
377                                 Assert.Ignore ("NestedSynchronizationContext may never complete on custom context");
378                 }
379
380                 static async Task NestedLeakingSynchronizationContext_MainAsync (SynchronizationContext sc)
381                 {
382                         Assert.AreSame (sc, SynchronizationContext.Current, "#1");
383                         await NestedLeakingSynchronizationContext_DoWorkAsync ();
384                         Assert.AreSame (sc, SynchronizationContext.Current, "#2");
385                 }
386
387                 static async Task NestedLeakingSynchronizationContext_DoWorkAsync ()
388                 {
389                         var sc = new NestedSynchronizationContext ();
390                         SynchronizationContext.SetSynchronizationContext (sc);
391
392                         Assert.AreSame (sc, SynchronizationContext.Current);
393                         await Task.Yield ();
394                         Assert.AreSame (sc, SynchronizationContext.Current);
395                 }
396         }
397 }
398
399 #endif