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