[interp] Remove unreachable code (#12411)
[mono-project.git] / mono / tests / appdomain-thread-abort.cs
blobf60daf1e6b2690b146cc0b05fe60277704a46266
1 using System;
2 using System.Threading;
3 using System.Runtime.Remoting;
4 using System.Reflection;
6 public class JustSomeClass {
9 public class Test2 : ContextBoundObject
11 public void Run () {
12 Thread.CurrentThread.Abort ();
16 public class Test1 : MarshalByRefObject
18 public bool Run () {
19 AppDomain d = AppDomain.CreateDomain ("foo2");
21 var t2 = (Test2)d.CreateInstanceAndUnwrap (Assembly.GetExecutingAssembly().FullName, "Test2");
22 try {
23 t2.Run ();
24 } catch (ThreadAbortException ex) {
25 Thread.ResetAbort ();
26 return true;
29 return false;
33 public class Test : MarshalByRefObject {
34 ThreadAbortException exc;
35 public JustSomeClass other;
37 public void doThrow (int n, object state) {
38 if (n <= 0)
39 Thread.CurrentThread.Abort (state);
40 else
41 doThrow (n - 1, state);
44 public void abortProxy () {
45 doThrow (10, this);
48 public void abortOther () {
49 other = new JustSomeClass ();
50 doThrow (10, other);
53 public void abortString () {
54 try {
55 doThrow (10, "bla");
56 } catch (ThreadAbortException e) {
57 exc = e;
61 public void abortOtherIndirect (Test test) {
62 test.abortOther ();
65 public object getState () {
66 return exc.ExceptionState;
69 public int getInt () {
70 return 123;
74 public static class Tests
76 static AppDomain domain = AppDomain.CreateDomain ("newdomain");
78 public static int test_0_abort_other_indirect ()
80 Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
81 Test testHere = new Test ();
83 if (!RemotingServices.IsTransparentProxy (test)) {
84 Console.WriteLine ("test is no proxy");
85 return 1;
88 try {
89 test.abortOtherIndirect (testHere);
90 } catch (ThreadAbortException e) {
91 object state = e.ExceptionState;
92 Thread.ResetAbort ();
93 if ((JustSomeClass)state != testHere.other) {
94 Console.WriteLine ("other class not preserved in state");
95 return 2;
99 return 0;
102 public static int test_0_abort_string ()
104 Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
106 if (!RemotingServices.IsTransparentProxy (test)) {
107 Console.WriteLine ("test is no proxy");
108 return 1;
111 try {
112 test.abortString ();
113 Console.WriteLine ("no abort");
114 return 2;
115 } catch (ThreadAbortException e) {
116 object state;
117 state = e.ExceptionState;
118 Thread.ResetAbort ();
119 if (state == null) {
120 Console.WriteLine ("state is null");
121 return 3;
122 } else {
123 if (RemotingServices.IsTransparentProxy (state)) {
124 Console.WriteLine ("state is proxy");
125 return 4;
127 if (!((string)state).Equals ("bla")) {
128 Console.WriteLine ("state is wrong: " + (string)state);
129 return 5;
132 if (RemotingServices.IsTransparentProxy (e)) {
133 Console.WriteLine ("exception is proxy");
134 return 6;
136 if (test.getState () != null) {
137 Console.WriteLine ("have state");
138 return 7;
142 return 0;
145 public static int test_0_abort_proxy ()
147 Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
149 if (!RemotingServices.IsTransparentProxy (test)) {
150 Console.WriteLine ("test is no proxy");
151 return 1;
154 try {
155 test.abortProxy ();
156 Console.WriteLine ("no abort");
157 return 2;
158 } catch (ThreadAbortException e) {
159 object state;
160 state = e.ExceptionState;
161 Thread.ResetAbort ();
162 if (state == null) {
163 Console.WriteLine ("state is null");
164 return 3;
165 } else {
166 if (!RemotingServices.IsTransparentProxy (state)) {
167 Console.WriteLine ("state is not proxy");
168 return 4;
170 if (((Test)state).getInt () != 123) {
171 Console.WriteLine ("state doesn't work");
172 return 5;
175 if (RemotingServices.IsTransparentProxy (e)) {
176 Console.WriteLine ("exception is proxy");
177 return 6;
181 return 0;
184 public static int test_0_abort_other ()
186 Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
188 if (!RemotingServices.IsTransparentProxy (test)) {
189 Console.WriteLine ("test is no proxy");
190 return 1;
193 try {
194 test.abortOther ();
195 Console.WriteLine ("no abort");
196 return 2;
197 } catch (ThreadAbortException e) {
198 object state = null;
199 bool stateExc = false;
201 try {
202 state = e.ExceptionState;
203 Console.WriteLine ("have state");
204 } catch (Exception) {
205 stateExc = true;
206 /* FIXME: if we put this after the try/catch, mono
207 quietly quits */
208 Thread.ResetAbort ();
210 if (!stateExc) {
211 Console.WriteLine ("no state exception");
212 return 3;
215 if (RemotingServices.IsTransparentProxy (e)) {
216 Console.WriteLine ("exception is proxy");
217 return 4;
221 return 0;
224 public static int test_0_abort_in_thread ()
226 // #539394
227 // Calling Thread.Abort () from a remoting call throws a ThreadAbortException which
228 // cannot be caught because the exception handling code is confused by the domain
229 // transitions
230 bool res = false;
232 Thread thread = new Thread (delegate () {
233 AppDomain d = AppDomain.CreateDomain ("foo");
235 var t = (Test1)d.CreateInstanceAndUnwrap (Assembly.GetExecutingAssembly().FullName, "Test1");
236 res = t.Run ();
239 thread.Start ();
240 thread.Join ();
242 if (!res)
243 return 1;
245 return 0;
248 public static int Main (string [] args)
250 return TestDriver.RunTests (typeof (Tests), args);