2009-06-10 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / thread6.cs
1 using System;
2 using System.Threading;
3
4 public class MultiThreadExceptionTest {
5
6         public static int result = 0;
7         public static object started = new object ();
8         
9         public static void ThreadStart1 () {
10                 Console.WriteLine("{0} started", 
11                                   Thread.CurrentThread.Name);
12
13                 try {
14                         try {
15                                 try {
16                                         lock (started) {
17                                                 Monitor.Pulse (started);
18                                         }
19                                         int i = 0;
20                                         try {
21                                                 while (true) {
22                                                         Console.WriteLine ("Count: " + i++);
23                                                         Thread.Sleep (100);
24                                                 }
25                                         }
26                                         catch (ThreadAbortException e) {
27                                                 Console.WriteLine ("cought exception level 3 ");
28
29                                                 // Check that the exception is only rethrown in
30                                                 // the appropriate catch clauses
31
32                                                 // This doesn't work currently, see
33                                                 // http://bugzilla.ximian.com/show_bug.cgi?id=68552
34
35                                                 /*
36                                                 try {
37                                                 }
38                                                 catch {}
39                                                 try {
40                                                         throw new DivideByZeroException ();
41                                                 }
42                                                 catch (Exception) {
43                                                 }
44                                                 */
45                                                 result |= 32;
46
47                                                 // Check that the exception is properly rethrown
48                                         }
49                                         result = 255;
50                                 } catch (ThreadAbortException e) {
51                                         Console.WriteLine ("cought exception level 2 " + e.ExceptionState);
52                                         Console.WriteLine (e);
53                                         if ((string)e.ExceptionState == "STATETEST")
54                                                 result |= 1;
55
56                                         Thread.ResetAbort ();
57                                         throw e;
58                                 }
59                         } catch (ThreadAbortException e) {
60                                 Console.WriteLine ("cought exception level 1 " + e.ExceptionState);
61                                 Console.WriteLine (e);
62                                 if (e.ExceptionState == null)
63                                         result |= 2;
64                         }
65                 } catch (Exception e) {
66                         Console.WriteLine ("cought exception level 0")
67 ;                       Console.WriteLine (e);
68                         Console.WriteLine (e.StackTrace);
69                         result |= 4;
70                 }
71
72                 try {
73                         Thread.ResetAbort ();
74                 } catch (System.Threading.ThreadStateException e) {
75                         result |= 8;                    
76                 }
77                 
78                 Console.WriteLine ("end");
79                 result |= 16;
80         }
81
82         static string regress_78024 ()
83         {
84                 try {
85                         Thread.CurrentThread.Abort ();
86                 } catch (Exception e) {
87                         return "Got exception: " + e.Message;
88                 } finally {
89                 }
90                 return "";
91         }
92         
93         public static int Main() {
94                 // Check aborting the current thread
95                 bool aborted = false;
96                 try {
97                         Thread.CurrentThread.Abort ();
98                 }
99                 catch {
100                         aborted = true;
101                         Thread.ResetAbort ();
102                 }
103                 if (!aborted)
104                         return 2;
105
106                 Thread t1 = null;
107
108                 lock (started) {
109                         t1 = new Thread(new ThreadStart
110                                                         (MultiThreadExceptionTest.ThreadStart1));
111                         t1.Name = "Thread 1";
112
113                         Thread.Sleep (100);
114                 
115                         t1.Start();
116
117                         Monitor.Wait (started);
118                 }
119
120                 Thread.Sleep (100);
121
122                 t1.Abort ("STATETEST");
123
124                 t1.Join ();
125
126                 if (result != 59) {
127                         Console.WriteLine ("Result: " + result);
128                         return 1;
129                 }
130
131                 // Test from #68552
132                 try {
133                         try {
134                                 Run ();
135                         } catch (Exception ex) {
136                         }
137
138                         return 2;
139                 }
140                 catch (ThreadAbortException ex) {
141                         Thread.ResetAbort ();
142                 }
143
144                 // Test from #78024
145                 try {
146                         regress_78024 ();
147                         return 3;
148                 }
149                 catch (ThreadAbortException ex) {
150                         Thread.ResetAbort ();
151                 }
152
153                 return 0;
154         }
155
156         public static void Run ()
157         {
158                 try {
159                         Thread.CurrentThread.Abort ();
160                 } catch (Exception ex) {
161                         throw new Exception ("other");
162                 }
163         }
164 }
165