Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / tests / test-async-07.cs
1 // Compiler options: -langversion:future
2
3 using System;
4 using System.Threading;
5 using System.Threading.Tasks;
6
7 class Program
8 {
9         public static int Main ()
10         {
11                 var mre_l = new ManualResetEvent (false);
12                 var mre = new ManualResetEvent (false);
13
14                 Func<string, Task<string>> f = async l =>
15                         await Task.Factory.StartNew (() => {
16                                 if (!mre_l.WaitOne (3000))
17                                         throw new ApplicationException ("3");
18
19                                 return l;
20                         });
21
22                 var r = f ("a");
23                 mre_l.Set ();
24                 if (!r.Wait (3000))
25                         return 1;
26
27                 if (r.Result != "a")
28                         return 11;
29
30                 mre_l.Reset ();
31
32                 Func<Task> ff = async () =>
33                         await Task.Factory.StartNew (() => {
34                                 if (!mre_l.WaitOne (3000))
35                                         throw new ApplicationException ("3");
36                         });
37
38                 var rr = ff ();
39                 mre_l.Set ();
40                 if (!rr.Wait (3000))
41                         return 2;
42
43                 Func<short, Task<short>> f2 = async l => l;
44
45                 var r2 = f2 (88);
46                 if (r2.Result != 88)
47                         return 3;
48                 
49                 mre.Reset ();
50                 mre_l.Reset ();
51                 Action a = async () => await Task.Factory.StartNew (() => {
52                                 if (!mre_l.WaitOne (3000))
53                                         throw new ApplicationException ("4");
54                                 mre.Set ();
55                         }, CancellationToken.None);
56                 
57                 a ();
58                 mre_l.Set ();
59                 if (!mre.WaitOne (3000))
60                         return 4;
61
62                 Console.WriteLine ("ok");
63                 return 0;
64         }
65 }