Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / tests / test-async-32.cs
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
4
5 class Program
6 {
7         static async Task<int> TestCanceled()
8         {
9                 await Task.FromResult(1);
10                 throw new OperationCanceledException();
11         }
12
13         static async Task TestCanceled_2()
14         {
15                 await Task.FromResult(1);
16                 throw new OperationCanceledException();
17         }
18
19         static async Task<int> TestException()
20         {
21                 await Task.FromResult(1);
22                 throw new ApplicationException();
23         }
24
25         public static int Main()
26         {
27                 bool canceled = false;
28                 var t = TestCanceled().ContinueWith(l =>
29                 {
30                         canceled = l.IsCanceled;
31                 }, TaskContinuationOptions.ExecuteSynchronously);
32
33                 t.Wait();
34
35                 if (!canceled)
36                         return 1;
37
38                 if (t.Exception != null)
39                         return 2;
40
41                 t = TestCanceled_2().ContinueWith(l =>
42                 {
43                         canceled = l.IsCanceled;
44                 }, TaskContinuationOptions.ExecuteSynchronously);
45
46                 t.Wait();
47
48                 if (!canceled)
49                         return 11;
50
51                 if (t.Exception != null)
52                         return 12;
53
54                 bool faulted = false;
55                 bool has_exception = false;
56                 t = TestException().ContinueWith(l =>
57                 {
58                         faulted = l.IsFaulted;
59                         has_exception = l.Exception != null; // Has to observe it or will throw on shutdown
60                 }, TaskContinuationOptions.ExecuteSynchronously);
61
62                 if (!faulted)
63                         return 21;
64                         
65                 if (!has_exception)
66                         return 22;
67
68                 if (t.Exception != null)
69                         return 23;
70
71                 Console.WriteLine("ok");
72                 return 0;
73         }
74 }