[corlib] Update failing test
[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                 [Test]
252                 public void FinishedTaskOnCompleted ()
253                 {
254                         var mres = new ManualResetEvent (false);
255                         var mres2 = new ManualResetEvent (false);
256
257                         var tcs = new TaskCompletionSource<object> ();
258                         tcs.SetResult (null);
259                         var task = tcs.Task;
260
261                         var awaiter = task.GetAwaiter ();
262                         Assert.IsTrue (awaiter.IsCompleted, "#1");
263
264                         awaiter.OnCompleted(() => { 
265                                 if (mres.WaitOne (1000))
266                                         mres2.Set ();
267                         });
268
269                         mres.Set ();
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");;
273                 }
274
275                 [Test]
276                 public void CompletionOnSameCustomSynchronizationContext ()
277                 {
278                         progress = "";
279                         var syncContext = new SingleThreadSynchronizationContext ();
280                         SynchronizationContext.SetSynchronizationContext (syncContext);
281
282                         syncContext.Post (delegate {
283                                 Go (syncContext);
284                         }, null);
285
286                         // Custom message loop
287                         var cts = new CancellationTokenSource ();
288                         cts.CancelAfter (5000);
289                         while (progress.Length != 3 && !cts.IsCancellationRequested) {
290                                 syncContext.RunOnCurrentThread ();
291                                 Thread.Sleep (0);
292                         }
293
294                         Assert.AreEqual ("123", progress);
295                 }
296
297                 async void Go (SynchronizationContext ctx)
298                 {
299                         await Wait (ctx);
300
301                         progress += "2";
302                 }
303
304                 async Task Wait (SynchronizationContext ctx)
305                 {
306                         await Task.Delay (10); // Force block suspend/return
307
308                         ctx.Post (l => progress += "3", null);
309
310                         progress += "1";
311
312                         // Exiting same context - no need to post continuation
313                 }
314
315                 [Test]
316                 public void CompletionOnDifferentCustomSynchronizationContext ()
317                 {
318                         mre = new ManualResetEvent (false);
319                         progress = "";
320                         var syncContext = new SingleThreadSynchronizationContext ();
321                         SynchronizationContext.SetSynchronizationContext (syncContext);
322
323                         syncContext.Post (delegate {
324                                 Task t = new Task (delegate() { });
325                                 Go2 (syncContext, t);
326                                 t.Start ();
327                         }, null);
328
329                         // Custom message loop
330                         var cts = new CancellationTokenSource ();
331                         cts.CancelAfter (5000);
332                         while (progress.Length != 3 && !cts.IsCancellationRequested) {
333                                 syncContext.RunOnCurrentThread ();
334                                 Thread.Sleep (0);
335                         }
336
337                         Assert.AreEqual ("13xa2", progress);
338                 }
339
340                 async void Go2 (SynchronizationContext ctx, Task t)
341                 {
342                         await Wait2 (ctx, t);
343
344                         progress += "a";
345
346                         if (mre.WaitOne (5000))
347                                 progress += "2";
348                         else
349                                 progress += "b";
350                 }
351
352                 async Task Wait2 (SynchronizationContext ctx, Task t)
353                 {
354                         await t; // Force block suspend/return
355
356                         ctx.Post (l => {
357                                 progress += "3";
358                                 mre.Set ();
359                                 progress += "x";
360                         }, null);
361
362                         progress += "1";
363
364                         SynchronizationContext.SetSynchronizationContext (null);
365                 }
366
367                 [Test]
368                 public void NestedLeakingSynchronizationContext ()
369                 {
370                         var sc = SynchronizationContext.Current;
371                         if (sc == null)
372                                 Assert.IsTrue (NestedLeakingSynchronizationContext_MainAsync (sc).Wait (5000), "#1");
373                         else
374                                 Assert.Ignore ("NestedSynchronizationContext may never complete on custom context");
375                 }
376
377                 static async Task NestedLeakingSynchronizationContext_MainAsync (SynchronizationContext sc)
378                 {
379                         Assert.AreSame (sc, SynchronizationContext.Current, "#1");
380                         await NestedLeakingSynchronizationContext_DoWorkAsync ();
381                         Assert.AreSame (sc, SynchronizationContext.Current, "#2");
382                 }
383
384                 static async Task NestedLeakingSynchronizationContext_DoWorkAsync ()
385                 {
386                         var sc = new NestedSynchronizationContext ();
387                         SynchronizationContext.SetSynchronizationContext (sc);
388
389                         Assert.AreSame (sc, SynchronizationContext.Current);
390                         await Task.Yield ();
391                         Assert.AreSame (sc, SynchronizationContext.Current);
392                 }
393         }
394 }
395
396 #endif