[w32file] Move MonoIO.Find{First,Next,Close} to managed
[mono.git] / mcs / tests / test-async-06.cs
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
4
5 class Program
6 {
7         public static int Main ()
8         {
9                 var mre = new ManualResetEvent (false);
10                 var mre_l = new ManualResetEvent (false);
11
12                 Action a = async () => {
13                         await Task.Factory.StartNew (() => {
14                                 if (!mre_l.WaitOne (3000))
15                                         throw new ApplicationException ("1");
16                         }).ConfigureAwait (false);
17
18                         if (mre_l.WaitOne ())
19                                 mre.Set ();
20                 };
21
22                 a ();
23                 mre_l.Set ();
24                 if (!mre.WaitOne (3000))
25                         return 1;
26
27                 mre.Reset ();
28                 mre_l.Reset ();
29
30                 Action a2 = async delegate {
31                         await Task.Factory.StartNew (() => {
32                                 if (!mre_l.WaitOne (3000))
33                                         throw new ApplicationException ("2");
34                         }).ConfigureAwait (false);
35
36                         if (mre_l.WaitOne ())
37                                 mre.Set ();
38                 };
39
40                 a2 ();
41                 mre_l.Set ();
42                 if (!mre.WaitOne (3000))
43                         return 2;
44
45                 mre_l.Reset ();
46
47                 Func<string, Task<string>> f = async l => {
48                         var t = await Task.Factory.StartNew (() => {
49                                 if (!mre_l.WaitOne (3000))
50                                         throw new ApplicationException ("3");
51
52                                 return l;
53                         }).ConfigureAwait (false);
54
55                         return t;
56                 };
57
58                 var r = f ("a");
59                 mre_l.Set ();
60                 if (!r.Wait (3000))
61                         return 3;
62
63                 if (r.Result != "a")
64                         return 31;
65
66                 mre_l.Reset ();
67
68                 Func<decimal, Task<decimal>> f2 = async delegate (decimal l) {
69                         var t = await Task.Factory.StartNew (() => {
70                                 if (!mre_l.WaitOne (3000))
71                                         throw new ApplicationException ("4");
72
73                                 return l;
74                         }).ConfigureAwait (false);
75
76                         return t;
77                 };
78
79                 var r2 = f2 (decimal.MaxValue);
80                 mre_l.Set ();
81                 if (!r2.Wait (3000))
82                         return 4;
83
84                 if (r2.Result != decimal.MaxValue)
85                         return 41;
86
87                 f2 = async delegate (decimal l) {
88                         return l;
89                 };
90
91                 r2 = f2 (88);
92                 if (r2.Result != 88)
93                         return 5;
94
95                 return 0;
96         }
97 }