[jit] Fix the saving of the 'cfg->ret_var_set' flag when inlining, it was set to...
[mono.git] / mono / tests / thread6.cs
1 //
2 // thread6.cs: Thread abort tests
3 //
4 using System;
5 using System.Threading;
6
7 public class Tests {
8
9         public static int result = 0;
10         public static object started = new object ();
11         
12         public static void ThreadStart1 () {
13                 Console.WriteLine("{0} started", 
14                                   Thread.CurrentThread.Name);
15
16                 try {
17                         try {
18                                 try {
19                                         lock (started) {
20                                                 Monitor.Pulse (started);
21                                         }
22                                         int i = 0;
23                                         try {
24                                                 while (true) {
25                                                         Console.WriteLine ("Count: " + i++);
26                                                         Thread.Sleep (100);
27                                                 }
28                                         }
29                                         catch (ThreadAbortException e) {
30                                                 Console.WriteLine ("cought exception level 3 ");
31
32                                                 // Check that the exception is only rethrown in
33                                                 // the appropriate catch clauses
34
35                                                 // This doesn't work currently, see
36                                                 // http://bugzilla.ximian.com/show_bug.cgi?id=68552
37
38                                                 /*
39                                                 try {
40                                                 }
41                                                 catch {}
42                                                 try {
43                                                         throw new DivideByZeroException ();
44                                                 }
45                                                 catch (Exception) {
46                                                 }
47                                                 */
48                                                 result |= 32;
49
50                                                 // Check that the exception is properly rethrown
51                                         }
52                                         result = 255;
53                                 } catch (ThreadAbortException e) {
54                                         Console.WriteLine ("cought exception level 2 " + e.ExceptionState);
55                                         Console.WriteLine (e);
56                                         if ((string)e.ExceptionState == "STATETEST")
57                                                 result |= 1;
58
59                                         Thread.ResetAbort ();
60                                         throw e;
61                                 }
62                         } catch (ThreadAbortException e) {
63                                 Console.WriteLine ("cought exception level 1 " + e.ExceptionState);
64                                 Console.WriteLine (e);
65                                 if (e.ExceptionState == null)
66                                         result |= 2;
67                         }
68                 } catch (Exception e) {
69                         Console.WriteLine ("cought exception level 0")
70 ;                       Console.WriteLine (e);
71                         Console.WriteLine (e.StackTrace);
72                         result |= 4;
73                 }
74
75                 try {
76                         Thread.ResetAbort ();
77                 } catch (System.Threading.ThreadStateException e) {
78                         result |= 8;                    
79                 }
80                 
81                 Console.WriteLine ("end");
82                 result |= 16;
83         }
84
85         static string regress_78024 ()
86         {
87                 try {
88                         Thread.CurrentThread.Abort ();
89                 } catch (Exception e) {
90                         return "Got exception: " + e.Message;
91                 } finally {
92                 }
93                 return "";
94         }
95         
96         public static int Main() {
97                 return TestDriver.RunTests (typeof (Tests));
98         }
99
100         public static int test_0_abort_current () {
101                 // Check aborting the current thread
102                 bool aborted = false;
103                 try {
104                         Thread.CurrentThread.Abort ();
105                 }
106                 catch {
107                         aborted = true;
108                         Thread.ResetAbort ();
109                 }
110                 if (!aborted)
111                         return 2;
112
113                 return 0;
114         }
115
116         public static int test_0_test_1 () {
117                 Thread t1 = null;
118
119                 lock (started) {
120                         t1 = new Thread(new ThreadStart
121                                                         (Tests.ThreadStart1));
122                         t1.Name = "Thread 1";
123
124                         Thread.Sleep (100);
125                 
126                         t1.Start();
127
128                         Monitor.Wait (started);
129                 }
130
131                 Thread.Sleep (100);
132
133                 t1.Abort ("STATETEST");
134
135                 t1.Join ();
136
137                 if (result != 59) {
138                         Console.WriteLine ("Result: " + result);
139                         return 1;
140                 }
141
142                 return 0;
143         }
144
145         public static int test_0_regress_68552 () {
146                 try {
147                         try {
148                                 Run ();
149                         } catch (Exception ex) {
150                         }
151
152                         return 2;
153                 }
154                 catch (ThreadAbortException ex) {
155                         Thread.ResetAbort ();
156                 }
157
158                 return 0;
159         }
160
161         public static int test_0_regress_78024 () {
162                 try {
163                         regress_78024 ();
164                         return 3;
165                 }
166                 catch (ThreadAbortException ex) {
167                         Thread.ResetAbort ();
168                 }
169
170                 return 0;
171         }
172
173         public static void HasTry ()
174         {
175                 try {
176                         throw new Exception ("boop");
177                 } catch (Exception e) {
178                         // See if we re-throw the thread abort exception here
179                 }
180         }
181
182         public static int test_0_thread_abort_water_mark () 
183         {
184                 Boolean failed = true;
185
186                 try {
187                         Thread.CurrentThread.Abort ("test_0_thread_abort_water_mark");
188                 } catch (ThreadAbortException e) {
189                         HasTry ();
190                         Thread.ResetAbort ();
191                         failed = false;
192                 } finally {
193                         if (failed) {
194                                 Thread.ResetAbort ();
195                                 throw new Exception ("Threw pending ThreadAbort exception under stack threshold");
196                         }
197                         Console.WriteLine ("Working thread abort");
198                 }
199
200                 return 0;
201         }
202
203         public static int test_0_thread_abort_water_mark_other_exc () 
204         {
205                 Boolean failed = true;
206
207                 try {
208                         try {
209                                 try {
210                                         Thread.CurrentThread.Abort ("TestKeepAbort");
211                                 } catch (ThreadAbortException ta_ex) {
212                                         throw new ArgumentNullException("SpecificDummyException");
213                                 }
214                         } catch (ArgumentNullException ex){
215                                 // Throw ThreadAbortException here
216                         }
217                 } catch (ThreadAbortException ex) {
218                         Console.WriteLine ("Retained thread abort exception");
219                         failed = false;
220                         Thread.ResetAbort ();
221                 } catch (Exception e) {
222                         failed = true;
223                 } finally {
224                         if (failed)
225                                 throw new Exception ("Lost the thread abort due to another exception running.");
226                 }
227
228                 return 0;
229         }
230
231         public class CBO : ContextBoundObject {
232                 public void Run () {
233                         Thread.CurrentThread.Abort ("FOO");
234                 }
235         }
236
237         public static int test_0_regress_539394 () {
238                 // Check that a ThreadAbortException thrown through remoting retains its
239                 // abort state
240                 AppDomain d = AppDomain.CreateDomain ("test");
241                 CBO obj = (CBO)d.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Tests/CBO");
242                 bool success = false;
243
244                 Thread t = new Thread (delegate () {
245                                 try {
246                                         obj.Run ();
247                                 } catch (ThreadAbortException ex) {
248                                         if ((string)ex.ExceptionState == "FOO")
249                                                 success = true;
250                                 }
251                         });
252
253                 t.Start ();
254                 t.Join ();
255
256                 return success ? 0 : 1;
257         }
258
259         public static int test_0_regress_4413 () {
260                 // Check that thread abort exceptions originating in another thread are not automatically rethrown
261                 object o = new object ();
262                 Thread t = null;
263                 bool waiting = false;
264                 Action a = delegate () {
265                         t = Thread.CurrentThread;
266                         while (true) {
267                                 lock (o) {
268                                         if (waiting) {
269                                                 Monitor.Pulse (o);
270                                                 break;
271                                         }
272                                 }
273
274                                 Thread.Sleep (10);
275                         }
276                         while (true) {
277                                 Thread.Sleep (1000);
278                         }
279                 };
280                 var ar = a.BeginInvoke (null, null);
281                 lock (o) {
282                         waiting = true;
283                         Monitor.Wait (o);
284                 }
285
286                 t.Abort ();
287
288                 try {
289                         try {
290                                 a.EndInvoke (ar);
291                         } catch (ThreadAbortException) {
292                         }
293                 } catch (ThreadAbortException) {
294                         // This will fail
295                         Thread.ResetAbort ();
296                         return 1;
297                 }
298
299                 return 0;
300         }
301
302         public static void Run ()
303         {
304                 try {
305                         Thread.CurrentThread.Abort ();
306                 } catch (Exception ex) {
307                         throw new Exception ("other");
308                 }
309         }
310 }
311