2 using System.Threading;
3 using System.Reflection;
4 using System.Runtime.Remoting;
10 Console.WriteLine ("FINALIZING IN DOMAIN " + AppDomain.CurrentDomain.FriendlyName + ": " + AppDomain.CurrentDomain.IsFinalizingForUnload ());
14 public class Bar : MarshalByRefObject {
15 public int test (int x) {
16 Console.WriteLine ("in " + Thread.GetDomain ().FriendlyName);
20 public void start_wait () {
21 Action a = delegate () {
24 a.BeginInvoke (null, null);
29 public class SlowFinalize {
32 Console.WriteLine ("FINALIZE1.");
36 catch (Exception ex) {
37 Console.WriteLine ("A: " + ex);
39 Console.WriteLine ("FINALIZE2.");
44 public class AThread {
47 new Thread (new ThreadStart (Run)).Start ();
55 catch (ThreadAbortException ex) {
56 Console.WriteLine ("Thread aborted correctly.");
61 // A Thread which refuses to die
62 public class BThread : MarshalByRefObject {
67 new Thread (new ThreadStart (Run)).Start ();
79 catch (ThreadAbortException ex) {
86 public interface IRunnable {
90 public class MBRObject : MarshalByRefObject, IRunnable {
91 /* XDomain wrappers for invocation */
98 catch (ThreadAbortException ex) {
105 public class CBObject : ContextBoundObject, IRunnable {
106 /* Slow corlib path for invocation */
113 catch (ThreadAbortException ex) {
114 Thread.ResetAbort ();
120 public class UnloadThread {
124 public UnloadThread (AppDomain domain) {
125 this.domain = domain;
129 Console.WriteLine ("UNLOAD1");
130 AppDomain.Unload (domain);
131 Console.WriteLine ("UNLOAD2");
135 class CrossDomainTester : MarshalByRefObject
141 public static int Main(string[] args) {
142 if (args.Length == 0)
143 return TestDriver.RunTests (typeof (Tests), new String[] { "-v" });
145 return TestDriver.RunTests (typeof (Tests), args);
148 public static int test_0_unload () {
149 for (int i = 0; i < 10; ++i) {
150 AppDomain appDomain = AppDomain.CreateDomain("Test-unload" + i);
152 appDomain.CreateInstanceAndUnwrap (
153 typeof (CrossDomainTester).Assembly.FullName, "CrossDomainTester");
155 AppDomain.Unload(appDomain);
161 public static int test_0_unload_default () {
163 AppDomain.Unload (Thread.GetDomain ());
165 catch (CannotUnloadAppDomainException) {
171 public static int test_0_unload_after_unload () {
172 AppDomain domain = AppDomain.CreateDomain ("Test2");
173 AppDomain.Unload (domain);
176 AppDomain.Unload (domain);
185 public static int test_0_is_finalizing () {
186 AppDomain domain = AppDomain.CreateDomain ("Test-is-finalizing");
187 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Foo");
189 if (domain.IsFinalizingForUnload ())
192 AppDomain.Unload (domain);
197 public static int test_0_unload_with_active_threads () {
198 AppDomain domain = AppDomain.CreateDomain ("Test3");
199 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "AThread");
202 AppDomain.Unload (domain);
207 /* In recent mono versions, there is no unload timeout */
209 public static int test_0_unload_with_active_threads_timeout () {
210 AppDomain domain = AppDomain.CreateDomain ("Test4");
211 BThread o = (BThread)domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
215 AppDomain.Unload (domain);
220 AppDomain.Unload (domain);
228 public static void ThreadStart (object obj)
230 IRunnable runnable = (IRunnable)obj;
234 } catch (AppDomainUnloadedException) {
235 Console.WriteLine ("OK");
236 } catch (ThreadAbortException) {
237 throw new Exception ();
241 public static int test_0_unload_reset_abort () {
242 AppDomain domain = AppDomain.CreateDomain ("test_0_unload_reset_abort");
243 MBRObject mbro = (MBRObject) domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "MBRObject");
244 CBObject cbo = (CBObject) domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "CBObject");
246 new Thread (ThreadStart).Start (mbro);
247 new Thread (ThreadStart).Start (cbo);
250 AppDomain.Unload (domain);
254 static void Worker (object x) {
255 Thread.Sleep (100000);
258 public static void invoke_workers () {
259 for (int i = 0; i < 1; i ++)
260 ThreadPool.QueueUserWorkItem (Worker);
263 public static int test_0_unload_with_threadpool () {
264 AppDomain domain = AppDomain.CreateDomain ("test_0_unload_with_threadpool");
266 domain.DoCallBack (new CrossAppDomainDelegate (invoke_workers));
267 AppDomain.Unload (domain);
273 * This test is not very deterministic since the thread which enqueues
274 * the work item might or might not be inside the domain when the unload
275 * happens. So disable this for now.
278 public static void DoUnload (object state) {
279 AppDomain.Unload (AppDomain.CurrentDomain);
282 public static void Callback () {
283 Console.WriteLine (AppDomain.CurrentDomain);
284 WaitCallback unloadDomainCallback = new WaitCallback (DoUnload);
285 ThreadPool.QueueUserWorkItem (unloadDomainCallback);
288 public static int test_0_unload_inside_appdomain_async () {
289 AppDomain domain = AppDomain.CreateDomain ("Test3");
291 domain.DoCallBack (new CrossAppDomainDelegate (Callback));
297 public static void SyncCallback () {
298 AppDomain.Unload (AppDomain.CurrentDomain);
301 public static int test_0_unload_inside_appdomain_sync () {
302 AppDomain domain = AppDomain.CreateDomain ("Test3");
306 domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
308 catch (AppDomainUnloadedException ex) {
318 public static int test_0_invoke_after_unload () {
319 AppDomain domain = AppDomain.CreateDomain ("DeadInvokeTest");
320 Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
323 if (!RemotingServices.IsTransparentProxy(bar))
326 AppDomain.Unload (domain);
333 } catch (Exception e) {
338 public static int test_0_abort_wait () {
339 AppDomain domain = AppDomain.CreateDomain ("AbortWait");
340 Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
344 AppDomain.Unload (domain);
348 // FIXME: This does not work yet, because the thread is finalized too
351 public static int test_0_unload_during_unload () {
352 AppDomain domain = AppDomain.CreateDomain ("Test3");
353 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "SlowFinalize");
355 UnloadThread t = new UnloadThread (domain);
357 // Start unloading in a separate thread
358 new Thread (new ThreadStart (t.Run)).Start ();
363 AppDomain.Unload (domain);
366 Console.WriteLine ("OK");