2009-04-15 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / thread6.cs
index 24f02118a1cc6dce43199d795d0cdceb5f7f3b2d..d1b6d81baff95692067048fcb5f93975f484634a 100644 (file)
@@ -4,6 +4,7 @@ using System.Threading;
 public class MultiThreadExceptionTest {
 
        public static int result = 0;
+       public static object started = new object ();
        
        public static void ThreadStart1 () {
                Console.WriteLine("{0} started", 
@@ -12,6 +13,9 @@ public class MultiThreadExceptionTest {
                try {
                        try {
                                try {
+                                       lock (started) {
+                                               Monitor.Pulse (started);
+                                       }
                                        int i = 0;
                                        try {
                                                while (true) {
@@ -25,6 +29,10 @@ public class MultiThreadExceptionTest {
                                                // Check that the exception is only rethrown in
                                                // the appropriate catch clauses
 
+                                               // This doesn't work currently, see
+                                               // http://bugzilla.ximian.com/show_bug.cgi?id=68552
+
+                                               /*
                                                try {
                                                }
                                                catch {}
@@ -33,6 +41,7 @@ public class MultiThreadExceptionTest {
                                                }
                                                catch (Exception) {
                                                }
+                                               */
                                                result |= 32;
 
                                                // Check that the exception is properly rethrown
@@ -69,9 +78,19 @@ public class MultiThreadExceptionTest {
                Console.WriteLine ("end");
                result |= 16;
        }
+
+       static string regress_78024 ()
+       {
+               try {
+                       Thread.CurrentThread.Abort ();
+               } catch (Exception e) {
+                       return "Got exception: " + e.Message;
+               } finally {
+               }
+               return "";
+       }
        
        public static int Main() {
-
                // Check aborting the current thread
                bool aborted = false;
                try {
@@ -84,24 +103,63 @@ public class MultiThreadExceptionTest {
                if (!aborted)
                        return 2;
 
-               Thread t1 = new Thread(new ThreadStart
-                       (MultiThreadExceptionTest.ThreadStart1));
-               t1.Name = "Thread 1";
+               Thread t1 = null;
 
-               Thread.Sleep (100);
-               
-               t1.Start();
+               lock (started) {
+                       t1 = new Thread(new ThreadStart
+                                                       (MultiThreadExceptionTest.ThreadStart1));
+                       t1.Name = "Thread 1";
+
+                       Thread.Sleep (100);
                
-               Thread.Sleep (200);
+                       t1.Start();
+
+                       Monitor.Wait (started);
+               }
+
+               Thread.Sleep (100);
+
                t1.Abort ("STATETEST");
 
                t1.Join ();
-               Console.WriteLine ("Result: " + result);
 
-               if (result != 59)
+               if (result != 59) {
+                       Console.WriteLine ("Result: " + result);
                        return 1;
+               }
+
+               // Test from #68552
+               try {
+                       try {
+                               Run ();
+                       } catch (Exception ex) {
+                       }
+
+                       return 2;
+               }
+               catch (ThreadAbortException ex) {
+                       Thread.ResetAbort ();
+               }
+
+               // Test from #78024
+               try {
+                       regress_78024 ();
+                       return 3;
+               }
+               catch (ThreadAbortException ex) {
+                       Thread.ResetAbort ();
+               }
 
                return 0;
        }
+
+       public static void Run ()
+       {
+               try {
+                       Thread.CurrentThread.Abort ();
+               } catch (Exception ex) {
+                       throw new Exception ("other");
+               }
+       }
 }