[rx] add missing project file generator helper files.
[mono-project.git] / mono / tests / appdomain-thread-abort.cs
blob77015fb7b49aabd7042ddcab855c3dee3227b3b4
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,
22 "Test2");
23 try {
24 t2.Run ();
25 } catch (ThreadAbortException ex) {
26 Thread.ResetAbort ();
27 return true;
30 return false;
34 public class Test : MarshalByRefObject {
35 ThreadAbortException exc;
36 public JustSomeClass other;
38 public void doThrow (int n, object state) {
39 if (n <= 0)
40 Thread.CurrentThread.Abort (state);
41 else
42 doThrow (n - 1, state);
45 public void abortProxy () {
46 doThrow (10, this);
49 public void abortOther () {
50 other = new JustSomeClass ();
51 doThrow (10, other);
54 public void abortString () {
55 try {
56 doThrow (10, "bla");
57 } catch (ThreadAbortException e) {
58 exc = e;
62 public void abortOtherIndirect (Test test) {
63 test.abortOther ();
66 public object getState () {
67 return exc.ExceptionState;
70 public int getInt () {
71 return 123;
75 public class main {
76 public static int Main (string [] args) {
77 AppDomain domain = AppDomain.CreateDomain ("newdomain");
78 Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
79 bool didAbort;
80 Test testHere = new Test ();
82 if (!RemotingServices.IsTransparentProxy (test)) {
83 Console.WriteLine ("test is no proxy");
84 return 5;
87 try {
88 test.abortOtherIndirect (testHere);
89 } catch (ThreadAbortException e) {
90 object state = e.ExceptionState;
91 Thread.ResetAbort ();
92 if ((JustSomeClass)state != testHere.other) {
93 Console.WriteLine ("other class not preserved in state");
94 return 16;
98 try {
99 didAbort = false;
100 test.abortString ();
101 } catch (ThreadAbortException e) {
102 object state;
103 state = e.ExceptionState;
104 Thread.ResetAbort ();
105 didAbort = true;
106 if (state == null) {
107 Console.WriteLine ("state is null");
108 return 13;
109 } else {
110 if (RemotingServices.IsTransparentProxy (state)) {
111 Console.WriteLine ("state is proxy");
112 return 1;
114 if (!((string)state).Equals ("bla")) {
115 Console.WriteLine ("state is wrong: " + (string)state);
116 return 2;
119 if (RemotingServices.IsTransparentProxy (e)) {
120 Console.WriteLine ("exception is proxy");
121 return 3;
123 if (test.getState () != null) {
124 Console.WriteLine ("have state");
125 return 12;
128 if (!didAbort) {
129 Console.WriteLine ("no abort");
130 return 4;
133 try {
134 didAbort = false;
135 test.abortProxy ();
136 } catch (ThreadAbortException e) {
137 object state;
138 state = e.ExceptionState;
139 Thread.ResetAbort ();
140 didAbort = true;
141 if (state == null) {
142 Console.WriteLine ("state is null");
143 return 14;
144 } else {
145 if (!RemotingServices.IsTransparentProxy (state)) {
146 Console.WriteLine ("state is not proxy");
147 return 6;
149 if (((Test)state).getInt () != 123) {
150 Console.WriteLine ("state doesn't work");
151 return 15;
154 if (RemotingServices.IsTransparentProxy (e)) {
155 Console.WriteLine ("exception is proxy");
156 return 7;
159 if (!didAbort) {
160 Console.WriteLine ("no abort");
161 return 8;
164 try {
165 didAbort = false;
166 test.abortOther ();
167 } catch (ThreadAbortException e) {
168 object state = null;
169 bool stateExc = false;
171 didAbort = true;
173 try {
174 state = e.ExceptionState;
175 Console.WriteLine ("have state");
176 } catch (Exception) {
177 stateExc = true;
178 /* FIXME: if we put this after the try/catch, mono
179 quietly quits */
180 Thread.ResetAbort ();
182 if (!stateExc) {
183 Console.WriteLine ("no state exception");
184 return 9;
187 if (RemotingServices.IsTransparentProxy (e)) {
188 Console.WriteLine ("exception is proxy");
189 return 10;
192 if (!didAbort) {
193 Console.WriteLine ("no abort");
194 return 11;
197 // #539394
198 // Calling Thread.Abort () from a remoting call throws a ThreadAbortException which
199 // cannot be caught because the exception handling code is confused by the domain
200 // transitions
201 bool res = false;
203 Thread thread = new Thread (delegate () {
204 AppDomain d = AppDomain.CreateDomain ("foo");
206 var t = (Test1)d.CreateInstanceAndUnwrap (Assembly.GetExecutingAssembly().FullName,
207 "Test1");
208 res = t.Run ();
211 thread.Start ();
212 thread.Join ();
214 if (!res)
215 return 12;
217 Console.WriteLine ("done");
219 return 0;