Update compiler tests
[mono.git] / mcs / tests / test-async-17.cs
1 // Compiler options: -langversion:future
2
3 using System;
4 using System.Threading.Tasks;
5 using System.Threading;
6 using System.Reflection;
7 using System.Linq;
8
9 class Tester
10 {
11         async Task<int> TestException_1 ()
12         {
13                 await Task.Factory.StartNew (() => { throw new ApplicationException (); }).ConfigureAwait (false);
14                 return 1;
15         }
16
17         async Task TestException_2 ()
18         {
19                 await Task.Factory.StartNew (() => { throw new ApplicationException (); }).ConfigureAwait (false);
20         }
21
22         async Task TestException_3 ()
23         {
24                 Func<Task> a = async () => await Task.Factory.StartNew (() => { throw new ApplicationException (); }).ConfigureAwait (false);
25                 await a ().ConfigureAwait (false);
26         }
27         
28         async Task<int> TestException_4 ()
29         {
30                 try {
31                         await Task.Factory.StartNew (() => 5).ConfigureAwait (false);
32                 } finally {
33                         throw new ApplicationException ();
34                 }
35         }
36         
37         async Task<int> TestException_5 ()
38         {
39                 int state = 0;
40                 try {
41                         await Task.Factory.StartNew (() => { throw new ArgumentException (); }).ConfigureAwait (false);
42                         state = 1;
43                 } catch (ArgumentException) {
44                         state = 2;
45                 } finally {
46                         if (state == 2)
47                                 throw new ApplicationException ();      
48                 }
49                 
50                 return 1;
51         }
52         
53         async Task<int> TestException_6 ()
54         {
55                 try {
56                         await Task.Factory.StartNew (() => { throw new ArgumentException (); }).ConfigureAwait (false);
57                 } catch (ArgumentException) {
58                         throw new ApplicationException ();      
59                 }
60                 
61                 return 1;
62         }
63         
64         static bool RunTest (MethodInfo test)
65         {
66                 Console.Write ("Running test {0, -25}", test.Name);
67                 try {
68                         Task t = test.Invoke (new Tester (), null) as Task;
69                         try {
70                                 if (!Task.WaitAll (new[] { t }, 1000)) {
71                                         Console.WriteLine ("FAILED (Timeout)");
72                                         return false;
73                                 }
74                         } catch (AggregateException) {
75                         }
76                         
77                         if (t.Status != TaskStatus.Faulted) {
78                                 Console.WriteLine ("FAILED (Status={0})", t.Status);
79                                 return false;
80                         }
81                         
82                         if (!(t.Exception.InnerException is ApplicationException)) {
83                                 Console.WriteLine ("FAILED with wrong exception");
84                                 return false;
85                         }
86                         
87                         Console.WriteLine ("OK");
88                         return true;
89                 } catch (Exception e) {
90                         Console.WriteLine ("FAILED");
91                         Console.WriteLine (e.ToString ());
92                         return false;
93                 }
94         }
95
96         public static int Main ()
97         {
98                 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
99                                         where test.GetParameters ().Length == 0
100                                         orderby test.Name
101                                         select RunTest (test);
102
103                 int failures = tests.Count (a => !a);
104                 Console.WriteLine (failures + " tests failed");
105                 return failures;
106         }       
107 }