Merge pull request #2003 from esdrubal/seq_test_fix2
[mono.git] / mono / tests / unhandled-exception.cs
1 using System;
2 using System.Diagnostics;
3 using System.Threading;
4 using System.Threading.Tasks;
5
6 class CustomException : Exception
7 {
8 }
9
10 class CustomException2 : Exception
11 {
12 }
13
14
15 class CrossDomain : MarshalByRefObject
16 {
17         public Action NewDelegateWithTarget ()
18         {
19                 return new Action (Bar);
20         }
21
22         public Action NewDelegateWithoutTarget ()
23         {
24                 return () => { throw new CustomException (); };
25         }
26
27         public void Bar ()
28         {
29                 throw new CustomException ();
30         }
31 }
32
33 class Driver {
34         static ManualResetEvent mre = new ManualResetEvent (false);
35
36         static void DoTest1 ()
37         {
38                 mre.Reset ();
39
40                 var t = new Thread (new ThreadStart (() => { try { throw new CustomException (); } finally { mre.Set (); } }));
41                 t.Start ();
42
43                 if (!mre.WaitOne (5000))
44                         Environment.Exit (2);
45
46                 t.Join ();
47         }
48
49         static void DoTest2 ()
50         {
51                 mre.Reset ();
52
53                 var a = new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } });
54                 var ares = a.BeginInvoke (null, null);
55
56                 if (!mre.WaitOne (5000))
57                         Environment.Exit (2);
58
59                 try {
60                         a.EndInvoke (ares);
61                         throw new Exception ();
62                 } catch (CustomException) {                     
63                 } catch (Exception) {
64                         Environment.Exit (3);
65                 }
66         }
67
68         static void DoTest3 ()
69         {
70                 mre.Reset ();
71
72                 ThreadPool.QueueUserWorkItem (_ => { try { throw new CustomException (); } finally { mre.Set (); } });
73
74                 if (!mre.WaitOne (5000))
75                         Environment.Exit (2);
76         }
77
78         static void DoTest4 ()
79         {
80                 mre.Reset ();
81
82                 var t = Task.Factory.StartNew (new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } }));
83
84                 if (!mre.WaitOne (5000))
85                         Environment.Exit (2);
86
87                 try {
88                         t.Wait ();
89                         throw new Exception ();
90                 } catch (AggregateException ae) {
91                         if (!(ae.InnerExceptions [0] is CustomException))
92                                 Environment.Exit (4);
93                 } catch (Exception) {
94                         Environment.Exit (3);
95                 }
96         }
97         
98         class FinalizedClass
99         {
100                 ~FinalizedClass ()
101                 {
102                         try {
103                                 throw new CustomException ();
104                         } finally {
105                                 mre.Set ();
106                         }
107                 }
108         }
109
110         static void DoTest5 ()
111         {
112                 mre.Reset ();
113
114                 new FinalizedClass();
115
116                 GC.Collect ();
117                 GC.WaitForPendingFinalizers ();
118
119                 if (!mre.WaitOne (5000))
120                         Environment.Exit (2);
121         }
122
123         static void DoTest6 ()
124         {
125                 ManualResetEvent mre2 = new ManualResetEvent (false);
126
127                 mre.Reset ();
128
129                 var a = new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } });
130                 var ares = a.BeginInvoke (_ => { mre2.Set (); throw new CustomException2 (); }, null);
131
132                 if (!mre.WaitOne (5000))
133                         Environment.Exit (2);
134                 if (!mre2.WaitOne (5000))
135                         Environment.Exit (22);
136
137                 try {
138                         a.EndInvoke (ares);
139                         throw new Exception ();
140                 } catch (CustomException) {
141                 } catch (Exception) {
142                         Environment.Exit (3);
143                 }
144         }
145
146         static void DoTest7 ()
147         {
148                 var cd = (CrossDomain) AppDomain.CreateDomain ("ad").CreateInstanceAndUnwrap (typeof(CrossDomain).Assembly.FullName, "CrossDomain");
149
150                 var a = cd.NewDelegateWithoutTarget ();
151                 var ares = a.BeginInvoke (delegate { throw new CustomException2 (); }, null);
152
153                 try {
154                         a.EndInvoke (ares);
155                         throw new Exception ();
156                 } catch (CustomException) {
157                 } catch (Exception) {
158                         Environment.Exit (3);
159                 }
160         }
161
162         static void DoTest8 ()
163         {
164                 var cd = (CrossDomain) AppDomain.CreateDomain ("ad").CreateInstanceAndUnwrap (typeof(CrossDomain).Assembly.FullName, "CrossDomain");
165
166                 var a = cd.NewDelegateWithTarget ();
167                 var ares = a.BeginInvoke (delegate { throw new CustomException2 (); }, null);
168
169                 try {
170                         a.EndInvoke (ares);
171                         throw new Exception ();
172                 } catch (CustomException) {
173                 } catch (Exception) {
174                         Environment.Exit (3);
175                 }
176         }
177
178         static void Main (string[] args)
179         {
180                 switch (int.Parse (args [0])) {
181                 case 1: DoTest1 (); break;
182                 case 2: DoTest2 (); break;
183                 case 3: DoTest3 (); break;
184                 case 4: DoTest4 (); break;
185                 case 5: DoTest5 (); break;
186                 case 6: DoTest6 (); break;
187                 case 7: DoTest7 (); break;
188                 case 8: DoTest8 (); break;
189                 default: throw new ArgumentOutOfRangeException ();
190                 }
191                 Environment.Exit (0);
192         }
193 }