[runtime] Synthesize IList and IReadOnlyList for the element type of enum errays...
[mono.git] / mono / tests / appdomain-unload.cs
index 4777b2b2bde154e6112dca3914b0036201319380..07cdf833e8cfafba1993b6111038c77c0da84ba8 100644 (file)
@@ -1,6 +1,7 @@
 using System;
 using System.Threading;
 using System.Reflection;
+using System.Runtime.Remoting;
 
 [Serializable]
 public class Foo {
@@ -10,6 +11,20 @@ public class Foo {
        }
 }
 
+public class Bar : MarshalByRefObject {
+       public int test (int x) {
+               Console.WriteLine ("in " + Thread.GetDomain ().FriendlyName);
+               return x + 1;
+       }
+
+       public void start_wait () {
+               Action a = delegate () {
+                       Thread.Sleep (10000);
+               };
+               a.BeginInvoke (null, null);
+       }
+}
+
 [Serializable]
 public class SlowFinalize {
 
@@ -68,6 +83,40 @@ public class BThread : MarshalByRefObject {
        }
 }
 
+public interface IRunnable {
+       void Run ();
+}
+
+public class MBRObject : MarshalByRefObject, IRunnable {
+       /* XDomain wrappers for invocation */
+       public void Run () {
+               while (true) {
+                       try {
+                               while (true)
+                                       Thread.Sleep (100);
+                       }
+                       catch (ThreadAbortException ex) {
+                               Thread.ResetAbort ();
+                       }
+               }
+       }
+}
+
+public class CBObject : ContextBoundObject, IRunnable {
+       /* Slow corlib path for invocation */
+       public void Run () {
+               while (true) {
+                       try {
+                               while (true)
+                                       Thread.Sleep (100);
+                       }
+                       catch (ThreadAbortException ex) {
+                               Thread.ResetAbort ();
+                       }
+               }
+       }
+}
+
 public class UnloadThread {
 
        AppDomain domain;
@@ -89,8 +138,11 @@ class CrossDomainTester : MarshalByRefObject
 
 public class Tests
 {
-       public static int Main() {
-               return TestDriver.RunTests (typeof (Tests));
+       public static int Main(string[] args) {
+               if (args.Length == 0)
+                       return TestDriver.RunTests (typeof (Tests), new String[] { "-v" });
+               else
+                       return TestDriver.RunTests (typeof (Tests), args);
        }
 
        public static int test_0_unload () {
@@ -152,6 +204,8 @@ public class Tests
                return 0;
        }
 
+       /* In recent mono versions, there is no unload timeout */
+       /*
        public static int test_0_unload_with_active_threads_timeout () {
                AppDomain domain = AppDomain.CreateDomain ("Test4");
                BThread o = (BThread)domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
@@ -169,6 +223,33 @@ public class Tests
 
                return 1;
        }
+       */
+
+       public static void ThreadStart (object obj)
+       {
+               IRunnable runnable = (IRunnable)obj;
+
+               try {
+                       runnable.Run ();
+               } catch (AppDomainUnloadedException) {
+                       Console.WriteLine ("OK");
+               } catch (ThreadAbortException) {
+                       throw new Exception ();
+               }
+       }
+
+       public static int test_0_unload_reset_abort () {
+               AppDomain domain = AppDomain.CreateDomain ("test_0_unload_reset_abort");
+               MBRObject mbro = (MBRObject) domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "MBRObject");
+               CBObject cbo = (CBObject) domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "CBObject");
+
+               new Thread (ThreadStart).Start (mbro);
+               new Thread (ThreadStart).Start (cbo);
+               Thread.Sleep (100);
+
+               AppDomain.Unload (domain);
+               return 0;
+       }
 
        static void Worker (object x) {
                Thread.Sleep (100000);
@@ -219,15 +300,48 @@ public class Tests
 
        public static int test_0_unload_inside_appdomain_sync () {
                AppDomain domain = AppDomain.CreateDomain ("Test3");
+               bool caught = false;
 
                try {
                        domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
                }
-               catch (Exception ex) {
-                       /* Should throw a ThreadAbortException */
-                       Thread.ResetAbort ();
+               catch (AppDomainUnloadedException ex) {
+                       caught = true;
+               }
+
+               if (!caught)
+                       return 1;
+
+               return 0;
+       }
+
+       public static int test_0_invoke_after_unload () {
+               AppDomain domain = AppDomain.CreateDomain ("DeadInvokeTest");
+               Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
+               int x;
+
+               if (!RemotingServices.IsTransparentProxy(bar))
+                       return 3;
+
+               AppDomain.Unload (domain);
+
+               try {
+                       x = bar.test (123);
+                       if (x == 124)
+                               return 1;
+                       return 2;
+               } catch (Exception e) {
+                       return 0;
                }
+       }
+
+       public static int test_0_abort_wait () {
+               AppDomain domain = AppDomain.CreateDomain ("AbortWait");
+               Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
+               int x;
 
+               bar.start_wait ();
+               AppDomain.Unload (domain);
                return 0;
        }