2005-10-07 Martin Baulig <martin@ximian.com>
[mono.git] / mono / tests / appdomain-unload.cs
index 8da67e960373d18841d3b3cbad248429b59422cb..f0778f0f7abde2b068a040a65836b6c16d10c628 100644 (file)
@@ -42,21 +42,27 @@ public class AThread {
        }
 }
 
-[Serializable]
 // A Thread which refuses to die
-public class BThread {
+public class BThread : MarshalByRefObject {
+
+       bool stop;
 
        public BThread () {
                new Thread (new ThreadStart (Run)).Start ();
        }
 
+       public void Stop () {
+               stop = true;
+       }
+
        public void Run () {
                try {
                        while (true)
                                Thread.Sleep (100);
                }
                catch (ThreadAbortException ex) {
-                       Thread.Sleep (1000000000);
+                       while (!stop)
+                               Thread.Sleep (100);
                }
        }
 }
@@ -139,19 +145,83 @@ public class Tests
 
        public static int test_0_unload_with_active_threads_timeout () {
                AppDomain domain = AppDomain.CreateDomain ("Test4");
-               object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
+               BThread o = (BThread)domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
                Thread.Sleep (100);
 
                try {
                        AppDomain.Unload (domain);
                }
                catch (Exception) {
+                       // Try again
+                       o.Stop ();
+                       AppDomain.Unload (domain);
                        return 0;
                }
 
                return 1;
        }
 
+       static void Worker (object x) {
+               Thread.Sleep (100000);
+       }
+
+       public static void invoke_workers () {
+               for (int i = 0; i < 1; i ++)
+                       ThreadPool.QueueUserWorkItem (Worker);
+       }
+
+       public static int test_0_unload_with_threadpool () {
+               AppDomain domain = AppDomain.CreateDomain ("test_0_unload_with_threadpool");
+
+               domain.DoCallBack (new CrossAppDomainDelegate (invoke_workers));
+               AppDomain.Unload (domain);
+
+               return 0;
+       }
+
+       /*
+        * This test is not very deterministic since the thread which enqueues
+        * the work item might or might not be inside the domain when the unload
+        * happens. So disable this for now.
+        */
+       /*
+       public static void DoUnload (object state) {
+               AppDomain.Unload (AppDomain.CurrentDomain);
+       }
+
+       public static void Callback () {
+               Console.WriteLine (AppDomain.CurrentDomain);
+               WaitCallback unloadDomainCallback = new WaitCallback (DoUnload);
+               ThreadPool.QueueUserWorkItem (unloadDomainCallback);
+       }               
+
+       public static int test_0_unload_inside_appdomain_async () {
+               AppDomain domain = AppDomain.CreateDomain ("Test3");
+
+               domain.DoCallBack (new CrossAppDomainDelegate (Callback));
+
+               return 0;
+       }
+       */
+
+       public static void SyncCallback () {
+               AppDomain.Unload (AppDomain.CurrentDomain);
+       }               
+
+       public static int test_0_unload_inside_appdomain_sync () {
+               AppDomain domain = AppDomain.CreateDomain ("Test3");
+
+               try {
+                       domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
+               }
+               catch (Exception ex) {
+                       /* Should throw a ThreadAbortException */
+                       Thread.ResetAbort ();
+               }
+
+               return 0;
+       }
+
        // FIXME: This does not work yet, because the thread is finalized too
        // early
        /*