[runtime] Fix detecting abort at end of abort protected block
[mono.git] / mono / tests / abort-cctor.cs
index 34f87808da9186330451d8e629ac8ebf2ebce2b9..7ff7a3b28e5143b073080de5dcf530737239a235 100644 (file)
@@ -44,7 +44,7 @@ class Driver
                if (!StaticConstructor1.gotToEnd) /* the TAE must not land during a .cctor */
                        Environment.Exit (1);
                if (StaticConstructor1.caughtException)
-                       Environment.Exit (1);
+                       Environment.Exit (2);
                        
        }
 
@@ -83,7 +83,7 @@ class Driver
                        Console.WriteLine ("StaticConstructor1 is viable"); /* a TAE doesn't make a type unusable */
                } catch (TypeInitializationException  e) {
                        Console.WriteLine ("StaticConstructor1 not viable");
-                       Environment.Exit (1);
+                       Environment.Exit (3);
                }
        }
 
@@ -141,7 +141,7 @@ class Driver
 
                if (Driver.mre2.WaitOne (500)) {
                        /* We shouldn't reach Driver.mre.Set () in StaticConstructor2.cctor */
-                       Environment.Exit (1);
+                       Environment.Exit (4);
                }
 
                thread.Join ();
@@ -151,7 +151,7 @@ class Driver
                        IsStaticConstructor2Viable ();
                        Console.WriteLine ("StaticConstructor2 is viable");
                        /* A regular exception escaping the .cctor makes the type not usable */
-                       Environment.Exit (1);
+                       Environment.Exit (5);
                } catch (TypeInitializationException e) {
                        Console.WriteLine ("StaticConstructor2 not viable");
                }
@@ -168,7 +168,7 @@ class Driver
                        /* Unreachable */
                        Driver.mre2.Set ();
                        Console.WriteLine ("StaticConstructor3.StaticConstructor3 (2)");
-                       Environment.Exit (1);
+                       Environment.Exit (6);
                }
 
                public static void Init ()
@@ -185,6 +185,7 @@ class Driver
        static void Test3 ()
        {
                Console.WriteLine ("Test 3:");
+               bool catched_abort = false;
 
                Driver.mre1.Reset ();
                Driver.mre2.Reset ();
@@ -194,9 +195,10 @@ class Driver
                                StaticConstructor3.Init ();
                                Console.WriteLine ("cctor3 didn't throw?!?!");
                                /* StaticConstructor3 self aborted */
-                               Environment.Exit (1);
+                               Environment.Exit (7);
                        } catch (ThreadAbortException e) {
                                Console.WriteLine ("TEST 3: aborted {0}", e);
+                               catched_abort = true;
                        }
                });
 
@@ -210,12 +212,16 @@ class Driver
 
                thread.Join ();
 
+               // Did we catch the abort
+               if (!catched_abort)
+                       Environment.Exit (8);
+
                //is StaticConstructor2 viable?
                try {
                        IsStaticConstructor3Viable ();
                        Console.WriteLine ("StaticConstructor3 is viable");
                        /* A regular exception escaping the .cctor makes the type not usable */
-                       Environment.Exit (1);
+                       Environment.Exit (9);
                } catch (TypeInitializationException e) {
                        Console.WriteLine ("StaticConstructor3 not viable");
                }
@@ -262,9 +268,9 @@ class Driver
                new StaticConstructor4 ();
                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);
                if (!StaticConstructor4.gotToEnd) /* the TAE must not land during a .cctor */
-                       Environment.Exit (1);
+                       Environment.Exit (10);
                if (StaticConstructor4.caughtException)
-                       Environment.Exit (1);
+                       Environment.Exit (11);
        }
 
        static void Test4 ()
@@ -305,7 +311,7 @@ class Driver
 
                if (!got_to_the_end_of_the_finally) { 
                        Console.WriteLine ("Did not get to the end of test 4 cctor");
-                       Environment.Exit (1);
+                       Environment.Exit (12);
                }
 
                //is StaticConstructor4viable?
@@ -314,11 +320,52 @@ class Driver
                        Console.WriteLine ("StaticConstructor4 is viable"); /* a TAE doesn't make a type unusable */
                } catch (TypeInitializationException  e) {
                        Console.WriteLine ("StaticConstructor4 not viable");
-                       Environment.Exit (1);
+                       Environment.Exit (13);
+               }
+       }
+
+
+       class StaticConstructor5 {
+               public static bool catched_exception = false;
+               static StaticConstructor5 ()
+               {
+                       Driver.mre1.Set ();
+                       Driver.mre2.WaitOne ();
+                       try {
+                               throw new Exception ();
+                       } catch (Exception) {
+                               Console.WriteLine ("Catched exception in cctor");
+                               catched_exception = true;
+                       }
                }
        }
 
+       static void Test5 ()
+       {
+               bool catched_abort = false;
+               Driver.mre1.Reset ();
+               Driver.mre2.Reset ();
+               Thread thread = new Thread (() => {
+                                       try {
+                                               new StaticConstructor5 ();
+                                       } catch (ThreadAbortException) {
+                                               Console.WriteLine ("Catched thread abort");
+                                               catched_abort = true;
+                                       }
+                               });
+               thread.Start ();
+
+               Driver.mre1.WaitOne ();
+               thread.Abort ();
+               Driver.mre2.Set ();
 
+               thread.Join ();
+
+               if (!StaticConstructor5.catched_exception)
+                       Environment.Exit (14);
+               if (!catched_abort)
+                       Environment.Exit (15);
+       }
 
        public static int Main ()
        {
@@ -326,7 +373,8 @@ class Driver
                Test2 ();
                Test3 ();
                Test4 ();
+               Test5 ();
                Console.WriteLine ("done, all things good");
                return 0;
        }
-}
\ No newline at end of file
+}