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);
22 public class SlowFinalize {
25 Console.WriteLine ("FINALIZE1.");
29 catch (Exception ex) {
30 Console.WriteLine ("A: " + ex);
32 Console.WriteLine ("FINALIZE2.");
37 public class AThread {
40 new Thread (new ThreadStart (Run)).Start ();
48 catch (ThreadAbortException ex) {
49 Console.WriteLine ("Thread aborted correctly.");
54 // A Thread which refuses to die
55 public class BThread : MarshalByRefObject {
60 new Thread (new ThreadStart (Run)).Start ();
72 catch (ThreadAbortException ex) {
79 public class UnloadThread {
83 public UnloadThread (AppDomain domain) {
88 Console.WriteLine ("UNLOAD1");
89 AppDomain.Unload (domain);
90 Console.WriteLine ("UNLOAD2");
94 class CrossDomainTester : MarshalByRefObject
100 public static int Main(string[] args) {
101 if (args.Length == 0)
102 return TestDriver.RunTests (typeof (Tests), new String[] { "-v" });
104 return TestDriver.RunTests (typeof (Tests), args);
107 public static int test_0_unload () {
108 for (int i = 0; i < 10; ++i) {
109 AppDomain appDomain = AppDomain.CreateDomain("Test-unload" + i);
111 appDomain.CreateInstanceAndUnwrap (
112 typeof (CrossDomainTester).Assembly.FullName, "CrossDomainTester");
114 AppDomain.Unload(appDomain);
120 public static int test_0_unload_default () {
122 AppDomain.Unload (Thread.GetDomain ());
124 catch (CannotUnloadAppDomainException) {
130 public static int test_0_unload_after_unload () {
131 AppDomain domain = AppDomain.CreateDomain ("Test2");
132 AppDomain.Unload (domain);
135 AppDomain.Unload (domain);
144 public static int test_0_is_finalizing () {
145 AppDomain domain = AppDomain.CreateDomain ("Test-is-finalizing");
146 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Foo");
148 if (domain.IsFinalizingForUnload ())
151 AppDomain.Unload (domain);
156 public static int test_0_unload_with_active_threads () {
157 AppDomain domain = AppDomain.CreateDomain ("Test3");
158 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "AThread");
161 AppDomain.Unload (domain);
166 /* In recent mono versions, there is no unload timeout */
168 public static int test_0_unload_with_active_threads_timeout () {
169 AppDomain domain = AppDomain.CreateDomain ("Test4");
170 BThread o = (BThread)domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
174 AppDomain.Unload (domain);
179 AppDomain.Unload (domain);
187 static void Worker (object x) {
188 Thread.Sleep (100000);
191 public static void invoke_workers () {
192 for (int i = 0; i < 1; i ++)
193 ThreadPool.QueueUserWorkItem (Worker);
196 public static int test_0_unload_with_threadpool () {
197 AppDomain domain = AppDomain.CreateDomain ("test_0_unload_with_threadpool");
199 domain.DoCallBack (new CrossAppDomainDelegate (invoke_workers));
200 AppDomain.Unload (domain);
206 * This test is not very deterministic since the thread which enqueues
207 * the work item might or might not be inside the domain when the unload
208 * happens. So disable this for now.
211 public static void DoUnload (object state) {
212 AppDomain.Unload (AppDomain.CurrentDomain);
215 public static void Callback () {
216 Console.WriteLine (AppDomain.CurrentDomain);
217 WaitCallback unloadDomainCallback = new WaitCallback (DoUnload);
218 ThreadPool.QueueUserWorkItem (unloadDomainCallback);
221 public static int test_0_unload_inside_appdomain_async () {
222 AppDomain domain = AppDomain.CreateDomain ("Test3");
224 domain.DoCallBack (new CrossAppDomainDelegate (Callback));
230 public static void SyncCallback () {
231 AppDomain.Unload (AppDomain.CurrentDomain);
234 public static int test_0_unload_inside_appdomain_sync () {
235 AppDomain domain = AppDomain.CreateDomain ("Test3");
238 domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
240 catch (Exception ex) {
241 /* Should throw a ThreadAbortException */
242 Thread.ResetAbort ();
248 public static int test_0_invoke_after_unload () {
249 AppDomain domain = AppDomain.CreateDomain ("DeadInvokeTest");
250 Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
253 if (!RemotingServices.IsTransparentProxy(bar))
256 AppDomain.Unload (domain);
263 } catch (Exception e) {
268 // FIXME: This does not work yet, because the thread is finalized too
271 public static int test_0_unload_during_unload () {
272 AppDomain domain = AppDomain.CreateDomain ("Test3");
273 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "SlowFinalize");
275 UnloadThread t = new UnloadThread (domain);
277 // Start unloading in a separate thread
278 new Thread (new ThreadStart (t.Run)).Start ();
283 AppDomain.Unload (domain);
286 Console.WriteLine ("OK");