Initial support for handling exceptions inside async
[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 (); });
14                 return 1;
15         }
16
17         async Task TestException_2 ()
18         {
19                 await Task.Factory.StartNew (() => { throw new ApplicationException (); });
20         }
21
22         async Task TestException_3 ()
23         {
24                 Func<Task> a = async () => await Task.Factory.StartNew (() => { throw new ApplicationException (); });
25                 await a ();
26         }
27         
28         async Task<int> TestException_4 ()
29         {
30                 try {
31                         await Task.Factory.StartNew (() => 5);
32                 } finally {
33                         throw new ApplicationException ();
34                 }
35         }
36         
37         static bool RunTest (MethodInfo test)
38         {
39                 Console.Write ("Running test {0, -25}", test.Name);
40                 try {
41                         Task t = test.Invoke (new Tester (), null) as Task;
42                         try {
43                                 if (!Task.WaitAll (new[] { t }, 1000)) {
44                                         Console.WriteLine ("FAILED (Timeout)");
45                                         return false;
46                                 }
47                         } catch (AggregateException) {
48                         }
49                         
50                         if (t.Status != TaskStatus.Faulted) {
51                                 Console.WriteLine ("FAILED (Status={0})", t.Status);
52                                 return false;
53                         }
54                         /*
55                         if (!(t.Exception.InnerException is ApplicationException)) {
56                                 Console.WriteLine ("FAILED with wrong exception");
57                                 return false;
58                         }
59                         */
60                         Console.WriteLine ("OK");
61                         return true;
62                 } catch (Exception e) {
63                         Console.WriteLine ("FAILED");
64                         Console.WriteLine (e.ToString ());
65                         return false;
66                 }
67         }
68
69         public static int Main ()
70         {
71                 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
72                                         where test.GetParameters ().Length == 0
73                                         orderby test.Name
74                                         select RunTest (test);
75
76                 int failures = tests.Count (a => !a);
77                 Console.WriteLine (failures + " tests failed");
78                 return failures;
79         }       
80 }