[Microsoft.Build] Ignore ThreadAbortException in BuildNodeManager
[mono-project.git] / mono / tests / abort-cctor.cs
blob34f87808da9186330451d8e629ac8ebf2ebce2b9
1 using System;
2 using System.Diagnostics;
3 using System.Threading;
4 using System.Runtime.CompilerServices;
6 class Driver
8 public static ManualResetEvent mre1 = new ManualResetEvent (false);
9 public static ManualResetEvent mre2 = new ManualResetEvent (false);
11 class StaticConstructor1
13 internal static bool gotToEnd, caughtException;
14 static StaticConstructor1 ()
16 try {
17 Console.WriteLine ("StaticConstructor1.StaticConstructor1 (1)");
18 Driver.mre1.Set ();
19 var sw = Stopwatch.StartNew ();
20 Thread.Sleep (1000);
21 sw.Stop ();
22 typeof (string).GetMethods ();
23 //XXX we assume that if we slept less than 900ms we got aborted
24 if (sw.ElapsedMilliseconds < 900)
25 throw new Exception ("Bad abort broke our sleep");
26 Console.WriteLine ("StaticConstructor1.StaticConstructor1 (2) waited {0}", sw.ElapsedMilliseconds);
27 gotToEnd = true;
28 } catch (Exception e) {
29 caughtException = true;
30 throw;
34 public static void Init ()
36 Console.WriteLine ("StaticConstructor1.Init");
40 [MethodImplAttribute (MethodImplOptions.NoInlining)]
41 static void IsStaticConstructor1Viable () {
42 new StaticConstructor1 ();
43 Console.WriteLine ("Did it get to the end? {0} Did it catch an exception {1}", StaticConstructor1.gotToEnd, StaticConstructor1.caughtException);
44 if (!StaticConstructor1.gotToEnd) /* the TAE must not land during a .cctor */
45 Environment.Exit (1);
46 if (StaticConstructor1.caughtException)
47 Environment.Exit (1);
51 static void Test1 ()
53 Console.WriteLine ("Test 1:");
55 Driver.mre1.Reset ();
56 Driver.mre2.Reset ();
58 Thread thread = new Thread (() => {
59 try {
60 StaticConstructor1.Init ();
61 } catch (Exception e) {
62 Console.WriteLine ("StaticConstructor1::init caught exception {0}", e);
64 if (!(e is ThreadAbortException))
65 throw;
67 });
69 thread.Start ();
71 Driver.mre1.WaitOne ();
73 // The ThreadAbortException should land while in
74 // the StaticConstructor1.cctor. The exception should
75 // be queued, and be rethrown when exiting the cctor.
76 thread.Abort ();
78 thread.Join ();
80 //is StaticConstructor1 viable?
81 try {
82 IsStaticConstructor1Viable ();
83 Console.WriteLine ("StaticConstructor1 is viable"); /* a TAE doesn't make a type unusable */
84 } catch (TypeInitializationException e) {
85 Console.WriteLine ("StaticConstructor1 not viable");
86 Environment.Exit (1);
90 class StaticConstructor2Exception : Exception {}
92 class StaticConstructor2
94 static StaticConstructor2 ()
96 Console.WriteLine ("StaticConstructor2.StaticConstructor2 (1)");
97 Driver.mre1.Set ();
98 throw new StaticConstructor2Exception ();
99 /* Unreachable */
100 Driver.mre2.Set ();
101 Console.WriteLine ("StaticConstructor2.StaticConstructor2 (2)");
104 public static void Init ()
106 Console.WriteLine ("StaticConstructor2.Init");
110 [MethodImplAttribute (MethodImplOptions.NoInlining)]
111 static void IsStaticConstructor2Viable () {
112 new StaticConstructor2 ();
116 static void Test2 ()
118 Console.WriteLine ("Test 2:");
120 Driver.mre1.Reset ();
121 Driver.mre2.Reset ();
123 Thread thread = new Thread (() => {
124 try {
125 StaticConstructor2.Init ();
126 } catch (TypeInitializationException e) {
127 Console.WriteLine (e);
129 if (!(e.InnerException is StaticConstructor2Exception))
130 throw;
134 thread.Start ();
136 Driver.mre1.WaitOne ();
138 // A InvalidOperationException should be thrown while in
139 // the StaticConstructor2.cctor. The exception should
140 // be wrapped in a TypeInitializationException.
142 if (Driver.mre2.WaitOne (500)) {
143 /* We shouldn't reach Driver.mre.Set () in StaticConstructor2.cctor */
144 Environment.Exit (1);
147 thread.Join ();
149 //is StaticConstructor2 viable?
150 try {
151 IsStaticConstructor2Viable ();
152 Console.WriteLine ("StaticConstructor2 is viable");
153 /* A regular exception escaping the .cctor makes the type not usable */
154 Environment.Exit (1);
155 } catch (TypeInitializationException e) {
156 Console.WriteLine ("StaticConstructor2 not viable");
161 class StaticConstructor3
163 static StaticConstructor3 ()
165 Console.WriteLine ("StaticConstructor3.StaticConstructor3 (1)");
166 Driver.mre1.Set ();
167 Thread.CurrentThread.Abort ();
168 /* Unreachable */
169 Driver.mre2.Set ();
170 Console.WriteLine ("StaticConstructor3.StaticConstructor3 (2)");
171 Environment.Exit (1);
174 public static void Init ()
176 Console.WriteLine ("StaticConstructor3.Init");
180 [MethodImplAttribute (MethodImplOptions.NoInlining)]
181 static void IsStaticConstructor3Viable () {
182 new StaticConstructor3 ();
185 static void Test3 ()
187 Console.WriteLine ("Test 3:");
189 Driver.mre1.Reset ();
190 Driver.mre2.Reset ();
192 Thread thread = new Thread (() => {
193 try {
194 StaticConstructor3.Init ();
195 Console.WriteLine ("cctor3 didn't throw?!?!");
196 /* StaticConstructor3 self aborted */
197 Environment.Exit (1);
198 } catch (ThreadAbortException e) {
199 Console.WriteLine ("TEST 3: aborted {0}", e);
203 thread.Start ();
205 Driver.mre1.WaitOne ();
207 // A InvalidOperationException should be thrown while in
208 // the StaticConstructor2.cctor. The exception should
209 // be wrapped in a TypeInitializationException.
211 thread.Join ();
213 //is StaticConstructor2 viable?
214 try {
215 IsStaticConstructor3Viable ();
216 Console.WriteLine ("StaticConstructor3 is viable");
217 /* A regular exception escaping the .cctor makes the type not usable */
218 Environment.Exit (1);
219 } catch (TypeInitializationException e) {
220 Console.WriteLine ("StaticConstructor3 not viable");
228 class StaticConstructor4
230 internal static bool gotToEnd, caughtException;
232 static StaticConstructor4 ()
234 try {
235 Console.WriteLine ("StaticConstructor4.StaticConstructor4 (1)");
236 Driver.mre1.Set ();
237 var sw = Stopwatch.StartNew ();
238 Thread.Sleep (1000);
239 sw.Stop ();
240 typeof (string).GetMethods ();
241 //XXX we assume that if we slept less than 900ms we got aborted
242 if (sw.ElapsedMilliseconds < 900)
243 throw new Exception ("Bad abort broke our sleep");
244 Console.WriteLine ("StaticConstructor4.StaticConstructor4 (2) waited {0}", sw.ElapsedMilliseconds);
245 gotToEnd = true;
246 } catch (Exception e) {
247 caughtException = true;
248 throw;
252 public static void Init ()
254 Console.WriteLine ("StaticConstructor4.Init");
258 static bool got_to_the_end_of_the_finally = false;
260 [MethodImplAttribute (MethodImplOptions.NoInlining)]
261 static void IsStaticConstructor4Viable () {
262 new StaticConstructor4 ();
263 Console.WriteLine ("IsStaticConstructor4Viable: Did it get to the end? {0} Did it catch an exception {1} and end of the finally block {2}", StaticConstructor4.gotToEnd, StaticConstructor4.caughtException, got_to_the_end_of_the_finally);
264 if (!StaticConstructor4.gotToEnd) /* the TAE must not land during a .cctor */
265 Environment.Exit (1);
266 if (StaticConstructor4.caughtException)
267 Environment.Exit (1);
270 static void Test4 ()
272 Console.WriteLine ("Test 4:");
274 Driver.mre1.Reset ();
275 Driver.mre2.Reset ();
277 Thread thread = new Thread (() => {
278 try {
280 try {
281 } finally {
282 StaticConstructor4.Init ();
283 Console.WriteLine ("Test 4: After the cctor");
284 got_to_the_end_of_the_finally = true;
286 } catch (Exception e) {
287 Console.WriteLine ("StaticConstructor4::init caught exception {0}", e);
288 if (!(e is ThreadAbortException))
289 throw;
290 if (!got_to_the_end_of_the_finally)
291 throw new Exception ("Test 4: did not get to the end of the cctor");
295 thread.Start ();
297 Driver.mre1.WaitOne ();
299 // The ThreadAbortException should land while in
300 // the StaticConstructor4.cctor. The exception should
301 // be queued, and be rethrown when exiting the cctor.
302 thread.Abort ();
304 thread.Join ();
306 if (!got_to_the_end_of_the_finally) {
307 Console.WriteLine ("Did not get to the end of test 4 cctor");
308 Environment.Exit (1);
311 //is StaticConstructor4viable?
312 try {
313 IsStaticConstructor4Viable ();
314 Console.WriteLine ("StaticConstructor4 is viable"); /* a TAE doesn't make a type unusable */
315 } catch (TypeInitializationException e) {
316 Console.WriteLine ("StaticConstructor4 not viable");
317 Environment.Exit (1);
323 public static int Main ()
325 Test1 ();
326 Test2 ();
327 Test3 ();
328 Test4 ();
329 Console.WriteLine ("done, all things good");
330 return 0;