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() {
101 return TestDriver.RunTests (typeof (Tests));
104 public static int test_0_unload () {
105 for (int i = 0; i < 10; ++i) {
106 AppDomain appDomain = AppDomain.CreateDomain("Test-unload" + i);
108 appDomain.CreateInstanceAndUnwrap (
109 typeof (CrossDomainTester).Assembly.FullName, "CrossDomainTester");
111 AppDomain.Unload(appDomain);
117 public static int test_0_unload_default () {
119 AppDomain.Unload (Thread.GetDomain ());
121 catch (CannotUnloadAppDomainException) {
127 public static int test_0_unload_after_unload () {
128 AppDomain domain = AppDomain.CreateDomain ("Test2");
129 AppDomain.Unload (domain);
132 AppDomain.Unload (domain);
141 public static int test_0_is_finalizing () {
142 AppDomain domain = AppDomain.CreateDomain ("Test-is-finalizing");
143 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Foo");
145 if (domain.IsFinalizingForUnload ())
148 AppDomain.Unload (domain);
153 public static int test_0_unload_with_active_threads () {
154 AppDomain domain = AppDomain.CreateDomain ("Test3");
155 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "AThread");
158 AppDomain.Unload (domain);
163 /* In recent mono versions, there is no unload timeout */
165 public static int test_0_unload_with_active_threads_timeout () {
166 AppDomain domain = AppDomain.CreateDomain ("Test4");
167 BThread o = (BThread)domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
171 AppDomain.Unload (domain);
176 AppDomain.Unload (domain);
184 static void Worker (object x) {
185 Thread.Sleep (100000);
188 public static void invoke_workers () {
189 for (int i = 0; i < 1; i ++)
190 ThreadPool.QueueUserWorkItem (Worker);
193 public static int test_0_unload_with_threadpool () {
194 AppDomain domain = AppDomain.CreateDomain ("test_0_unload_with_threadpool");
196 domain.DoCallBack (new CrossAppDomainDelegate (invoke_workers));
197 AppDomain.Unload (domain);
203 * This test is not very deterministic since the thread which enqueues
204 * the work item might or might not be inside the domain when the unload
205 * happens. So disable this for now.
208 public static void DoUnload (object state) {
209 AppDomain.Unload (AppDomain.CurrentDomain);
212 public static void Callback () {
213 Console.WriteLine (AppDomain.CurrentDomain);
214 WaitCallback unloadDomainCallback = new WaitCallback (DoUnload);
215 ThreadPool.QueueUserWorkItem (unloadDomainCallback);
218 public static int test_0_unload_inside_appdomain_async () {
219 AppDomain domain = AppDomain.CreateDomain ("Test3");
221 domain.DoCallBack (new CrossAppDomainDelegate (Callback));
227 public static void SyncCallback () {
228 AppDomain.Unload (AppDomain.CurrentDomain);
231 public static int test_0_unload_inside_appdomain_sync () {
232 AppDomain domain = AppDomain.CreateDomain ("Test3");
235 domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
237 catch (Exception ex) {
238 /* Should throw a ThreadAbortException */
239 Thread.ResetAbort ();
245 public static int test_0_invoke_after_unload () {
246 AppDomain domain = AppDomain.CreateDomain ("DeadInvokeTest");
247 Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
250 if (!RemotingServices.IsTransparentProxy(bar))
253 AppDomain.Unload (domain);
260 } catch (Exception e) {
265 // FIXME: This does not work yet, because the thread is finalized too
268 public static int test_0_unload_during_unload () {
269 AppDomain domain = AppDomain.CreateDomain ("Test3");
270 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "SlowFinalize");
272 UnloadThread t = new UnloadThread (domain);
274 // Start unloading in a separate thread
275 new Thread (new ThreadStart (t.Run)).Start ();
280 AppDomain.Unload (domain);
283 Console.WriteLine ("OK");