[MSBuild] Fix minor assembly resolution issue
[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
39 namespace MonoTests.System.Runtime.CompilerServices
40 {
41         [TestFixture]
42         public class TaskAwaiterTest
43         {
44                 class Scheduler : TaskScheduler
45                 {
46                         string name;
47                         int ic, qc;
48
49                         public Scheduler (string name)
50                         {
51                                 this.name = name;
52                         }
53
54                         public int InlineCalls { get { return ic; } }
55                         public int QueueCalls { get { return qc; } }
56
57                         protected override IEnumerable<Task> GetScheduledTasks ()
58                         {
59                                 throw new NotImplementedException ();
60                         }
61
62                         protected override void QueueTask (Task task)
63                         {
64                                 Interlocked.Increment (ref qc);
65                                 ThreadPool.QueueUserWorkItem (o => {
66                                         TryExecuteTask (task);
67                                 });
68                         }
69
70                         protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
71                         {
72                                 Interlocked.Increment (ref ic);
73                                 return false;
74                         }
75
76                         public override string ToString ()
77                         {
78                                 return "Scheduler-" + name;
79                         }
80                 }
81
82                 class SingleThreadSynchronizationContext : SynchronizationContext
83                 {
84                         readonly Queue _queue = new Queue ();
85
86                         public void RunOnCurrentThread ()
87                         {
88                                 while (_queue.Count != 0) {
89                                         var workItem = (KeyValuePair<SendOrPostCallback, object>) _queue.Dequeue ();
90                                         workItem.Key (workItem.Value);
91                                 }
92                         }
93                                 
94                         public override void Post (SendOrPostCallback d, object state)
95                         {
96                                 if (d == null) {
97                                         throw new ArgumentNullException ("d");
98                                 }
99
100                                 _queue.Enqueue (new KeyValuePair<SendOrPostCallback, object> (d, state));
101                         }
102
103                         public override void Send (SendOrPostCallback d, object state)
104                         {
105                                 throw new NotSupportedException ("Synchronously sending is not supported.");
106                         }
107                 }
108
109                 string progress;
110                 SynchronizationContext sc;
111                 ManualResetEvent mre;
112
113                 [SetUp]
114                 public void Setup ()
115                 {
116                         sc = SynchronizationContext.Current;
117                 }
118
119                 [TearDown]
120                 public void TearDown ()
121                 {
122                         SynchronizationContext.SetSynchronizationContext (sc);
123                 }
124
125                 [Test]
126                 public void GetResultFaulted ()
127                 {
128                         TaskAwaiter awaiter;
129
130                         var task = new Task (() => { throw new ApplicationException (); });
131                         awaiter = task.GetAwaiter ();
132                         task.RunSynchronously (TaskScheduler.Current);
133
134
135                         Assert.IsTrue (awaiter.IsCompleted);
136
137                         try {
138                                 awaiter.GetResult ();
139                                 Assert.Fail ();
140                         } catch (ApplicationException) {
141                         }
142                 }
143
144                 [Test]
145                 public void GetResultCanceled ()
146                 {
147                         TaskAwaiter awaiter;
148
149                         var token = new CancellationToken (true);
150                         var task = new Task (() => { }, token);
151                         awaiter = task.GetAwaiter ();
152
153                         try {
154                                 awaiter.GetResult ();
155                                 Assert.Fail ();
156                         } catch (TaskCanceledException) {
157                         }
158                 }
159
160                 [Test]
161                 public void GetResultWaitOnCompletion ()
162                 {
163                         TaskAwaiter awaiter;
164                                 
165                         var task = Task.Delay (30);
166                         awaiter = task.GetAwaiter ();
167                                 
168                         awaiter.GetResult ();
169                         Assert.AreEqual (TaskStatus.RanToCompletion, task.Status);
170                 }
171
172                 [Test]
173                 public void CustomScheduler ()
174                 {
175                         // some test runners (e.g. Touch.Unit) will execute this on the main thread and that would lock them
176                         if (!Thread.CurrentThread.IsBackground)
177                                 Assert.Ignore ("Current thread is not running in the background.");
178
179                         var a = new Scheduler ("a");
180                         var b = new Scheduler ("b");
181
182                         var t = TestCS (a, b);
183                         Assert.IsTrue (t.Wait (3000), "#0");
184                         Assert.AreEqual (0, t.Result, "#1");
185                         Assert.AreEqual (0, b.InlineCalls, "#2b");
186                         Assert.IsTrue (a.QueueCalls == 1 || a.QueueCalls == 2, "#3a");
187                         Assert.AreEqual (1, b.QueueCalls, "#3b");
188                 }
189
190                 static async Task<int> TestCS (TaskScheduler schedulerA, TaskScheduler schedulerB)
191                 {
192                         var res = await Task.Factory.StartNew (async () => {
193                                 if (TaskScheduler.Current != schedulerA)
194                                         return 1;
195
196                                 await Task.Factory.StartNew (
197                                         () => {
198                                                 if (TaskScheduler.Current != schedulerB)
199                                                         return 2;
200
201                                                 return 0;
202                                         }, CancellationToken.None, TaskCreationOptions.None, schedulerB);
203
204                                 if (TaskScheduler.Current != schedulerA)
205                                         return 3;
206
207                                 return 0;
208                         }, CancellationToken.None, TaskCreationOptions.None, schedulerA);
209
210                         return res.Result;
211                 }
212
213                 [Test]
214                 public void FinishedTaskOnCompleted ()
215                 {
216                         var mres = new ManualResetEvent (false);
217                         var mres2 = new ManualResetEvent (false);
218
219                         var tcs = new TaskCompletionSource<object> ();
220                         tcs.SetResult (null);
221                         var task = tcs.Task;
222
223                         var awaiter = task.GetAwaiter ();
224                         Assert.IsTrue (awaiter.IsCompleted, "#1");
225
226                         awaiter.OnCompleted(() => { 
227                                 if (mres.WaitOne (1000))
228                                         mres2.Set ();
229                         });
230
231                         mres.Set ();
232                         // this will only terminate correctly if the test was not executed from the main thread
233                         // e.g. Touch.Unit defaults to run tests on the main thread and this will return false
234                         Assert.AreEqual (Thread.CurrentThread.IsBackground, mres2.WaitOne (2000), "#2");;
235                 }
236
237                 [Test]
238                 public void CompletionOnSameCustomSynchronizationContext ()
239                 {
240                         progress = "";
241                         var syncContext = new SingleThreadSynchronizationContext ();
242                         SynchronizationContext.SetSynchronizationContext (syncContext);
243
244                         syncContext.Post (delegate {
245                                 Go (syncContext);
246                         }, null);
247
248                         // Custom message loop
249                         var cts = new CancellationTokenSource ();
250                         cts.CancelAfter (5000);
251                         while (progress.Length != 3 && !cts.IsCancellationRequested) {
252                                 syncContext.RunOnCurrentThread ();
253                                 Thread.Sleep (0);
254                         }
255
256                         Assert.AreEqual ("123", progress);
257                 }
258
259                 async void Go (SynchronizationContext ctx)
260                 {
261                         await Wait (ctx);
262
263                         progress += "2";
264                 }
265
266                 async Task Wait (SynchronizationContext ctx)
267                 {
268                         await Task.Delay (10); // Force block suspend/return
269
270                         ctx.Post (l => progress += "3", null);
271
272                         progress += "1";
273
274                         // Exiting same context - no need to post continuation
275                 }
276
277                 [Test]
278                 public void CompletionOnDifferentCustomSynchronizationContext ()
279                 {
280                         mre = new ManualResetEvent (false);
281                         progress = "";
282                         var syncContext = new SingleThreadSynchronizationContext ();
283                         SynchronizationContext.SetSynchronizationContext (syncContext);
284
285                         syncContext.Post (delegate {
286                                 Task t = new Task (delegate() { });
287                                 Go2 (syncContext, t);
288                                 t.Start ();
289                         }, null);
290
291                         // Custom message loop
292                         var cts = new CancellationTokenSource ();
293                         cts.CancelAfter (5000);
294                         while (progress.Length != 3 && !cts.IsCancellationRequested) {
295                                 syncContext.RunOnCurrentThread ();
296                                 Thread.Sleep (0);
297                         }
298
299                         Assert.AreEqual ("13xa2", progress);
300                 }
301
302                 async void Go2 (SynchronizationContext ctx, Task t)
303                 {
304                         await Wait2 (ctx, t);
305
306                         progress += "a";
307
308                         if (mre.WaitOne (5000))
309                                 progress += "2";
310                         else
311                                 progress += "b";
312                 }
313
314                 async Task Wait2 (SynchronizationContext ctx, Task t)
315                 {
316                         await t; // Force block suspend/return
317
318                         ctx.Post (l => {
319                                 progress += "3";
320                                 mre.Set ();
321                                 progress += "x";
322                         }, null);
323
324                         progress += "1";
325
326                         SynchronizationContext.SetSynchronizationContext (null);
327                 }
328         }
329 }
330
331 #endif