[rx] add missing project file generator helper files.
[mono-project.git] / mono / tests / thread6.cs
blob9b0c5cf3f9952f92b51e3cb8ce9db61af7d0a8de
1 //
2 // thread6.cs: Thread abort tests
3 //
4 using System;
5 using System.Threading;
7 public class Tests {
9 public static int result = 0;
10 public static object started = new object ();
12 public static void ThreadStart1 () {
13 Console.WriteLine("{0} started",
14 Thread.CurrentThread.Name);
16 try {
17 try {
18 try {
19 lock (started) {
20 Monitor.Pulse (started);
22 int i = 0;
23 try {
24 while (true) {
25 Console.WriteLine ("Count: " + i++);
26 Thread.Sleep (100);
29 catch (ThreadAbortException e) {
30 Console.WriteLine ("cought exception level 3 ");
32 // Check that the exception is only rethrown in
33 // the appropriate catch clauses
35 // This doesn't work currently, see
36 // http://bugzilla.ximian.com/show_bug.cgi?id=68552
39 try {
41 catch {}
42 try {
43 throw new DivideByZeroException ();
45 catch (Exception) {
48 result |= 32;
50 // Check that the exception is properly rethrown
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;
59 Thread.ResetAbort ();
60 throw e;
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;
68 } catch (Exception e) {
69 Console.WriteLine ("cought exception level 0")
70 ; Console.WriteLine (e);
71 Console.WriteLine (e.StackTrace);
72 result |= 4;
75 try {
76 Thread.ResetAbort ();
77 } catch (System.Threading.ThreadStateException e) {
78 result |= 8;
81 Console.WriteLine ("end");
82 result |= 16;
85 static string regress_78024 ()
87 try {
88 Thread.CurrentThread.Abort ();
89 } catch (Exception e) {
90 return "Got exception: " + e.Message;
91 } finally {
93 return "";
96 public static int Main() {
97 return TestDriver.RunTests (typeof (Tests));
100 public static int test_0_abort_current () {
101 // Check aborting the current thread
102 bool aborted = false;
103 try {
104 Thread.CurrentThread.Abort ();
106 catch {
107 aborted = true;
108 Thread.ResetAbort ();
110 if (!aborted)
111 return 2;
113 return 0;
116 public static int test_0_test_1 () {
117 Thread t1 = null;
119 lock (started) {
120 t1 = new Thread(new ThreadStart
121 (Tests.ThreadStart1));
122 t1.Name = "Thread 1";
124 Thread.Sleep (100);
126 t1.Start();
128 Monitor.Wait (started);
131 Thread.Sleep (100);
133 t1.Abort ("STATETEST");
135 t1.Join ();
137 if (result != 59) {
138 Console.WriteLine ("Result: " + result);
139 return 1;
142 return 0;
145 public static int test_0_regress_68552 () {
146 try {
147 try {
148 Run ();
149 } catch (Exception ex) {
152 return 2;
154 catch (ThreadAbortException ex) {
155 Thread.ResetAbort ();
158 return 0;
161 public static int test_0_regress_78024 () {
162 try {
163 regress_78024 ();
164 return 3;
166 catch (ThreadAbortException ex) {
167 Thread.ResetAbort ();
170 return 0;
173 public class CBO : ContextBoundObject {
174 public void Run () {
175 Thread.CurrentThread.Abort ("FOO");
179 public static int test_0_regress_539394 () {
180 // Check that a ThreadAbortException thrown through remoting retains its
181 // abort state
182 AppDomain d = AppDomain.CreateDomain ("test");
183 CBO obj = (CBO)d.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Tests/CBO");
184 bool success = false;
186 Thread t = new Thread (delegate () {
187 try {
188 obj.Run ();
189 } catch (ThreadAbortException ex) {
190 if ((string)ex.ExceptionState == "FOO")
191 success = true;
195 t.Start ();
196 t.Join ();
198 return success ? 0 : 1;
201 public static int test_0_regress_4413 () {
202 // Check that thread abort exceptions originating in another thread are not automatically rethrown
203 object o = new object ();
204 Thread t = null;
205 bool waiting = false;
206 Action a = delegate () {
207 t = Thread.CurrentThread;
208 while (true) {
209 lock (o) {
210 if (waiting) {
211 Monitor.Pulse (o);
212 break;
216 Thread.Sleep (10);
218 while (true) {
219 Thread.Sleep (1000);
222 var ar = a.BeginInvoke (null, null);
223 lock (o) {
224 waiting = true;
225 Monitor.Wait (o);
228 t.Abort ();
230 try {
231 try {
232 a.EndInvoke (ar);
233 } catch (ThreadAbortException) {
235 } catch (ThreadAbortException) {
236 // This will fail
237 Thread.ResetAbort ();
238 return 1;
241 return 0;
244 public static void Run ()
246 try {
247 Thread.CurrentThread.Abort ();
248 } catch (Exception ex) {
249 throw new Exception ("other");