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 class UnloadThread {
90 public UnloadThread (AppDomain domain) {
95 Console.WriteLine ("UNLOAD1");
96 AppDomain.Unload (domain);
97 Console.WriteLine ("UNLOAD2");
101 class CrossDomainTester : MarshalByRefObject
107 public static int Main(string[] args) {
108 if (args.Length == 0)
109 return TestDriver.RunTests (typeof (Tests), new String[] { "-v" });
111 return TestDriver.RunTests (typeof (Tests), args);
114 public static int test_0_unload () {
115 for (int i = 0; i < 10; ++i) {
116 AppDomain appDomain = AppDomain.CreateDomain("Test-unload" + i);
118 appDomain.CreateInstanceAndUnwrap (
119 typeof (CrossDomainTester).Assembly.FullName, "CrossDomainTester");
121 AppDomain.Unload(appDomain);
127 public static int test_0_unload_default () {
129 AppDomain.Unload (Thread.GetDomain ());
131 catch (CannotUnloadAppDomainException) {
137 public static int test_0_unload_after_unload () {
138 AppDomain domain = AppDomain.CreateDomain ("Test2");
139 AppDomain.Unload (domain);
142 AppDomain.Unload (domain);
151 public static int test_0_is_finalizing () {
152 AppDomain domain = AppDomain.CreateDomain ("Test-is-finalizing");
153 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Foo");
155 if (domain.IsFinalizingForUnload ())
158 AppDomain.Unload (domain);
163 public static int test_0_unload_with_active_threads () {
164 AppDomain domain = AppDomain.CreateDomain ("Test3");
165 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "AThread");
168 AppDomain.Unload (domain);
173 /* In recent mono versions, there is no unload timeout */
175 public static int test_0_unload_with_active_threads_timeout () {
176 AppDomain domain = AppDomain.CreateDomain ("Test4");
177 BThread o = (BThread)domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
181 AppDomain.Unload (domain);
186 AppDomain.Unload (domain);
194 static void Worker (object x) {
195 Thread.Sleep (100000);
198 public static void invoke_workers () {
199 for (int i = 0; i < 1; i ++)
200 ThreadPool.QueueUserWorkItem (Worker);
203 public static int test_0_unload_with_threadpool () {
204 AppDomain domain = AppDomain.CreateDomain ("test_0_unload_with_threadpool");
206 domain.DoCallBack (new CrossAppDomainDelegate (invoke_workers));
207 AppDomain.Unload (domain);
213 * This test is not very deterministic since the thread which enqueues
214 * the work item might or might not be inside the domain when the unload
215 * happens. So disable this for now.
218 public static void DoUnload (object state) {
219 AppDomain.Unload (AppDomain.CurrentDomain);
222 public static void Callback () {
223 Console.WriteLine (AppDomain.CurrentDomain);
224 WaitCallback unloadDomainCallback = new WaitCallback (DoUnload);
225 ThreadPool.QueueUserWorkItem (unloadDomainCallback);
228 public static int test_0_unload_inside_appdomain_async () {
229 AppDomain domain = AppDomain.CreateDomain ("Test3");
231 domain.DoCallBack (new CrossAppDomainDelegate (Callback));
237 public static void SyncCallback () {
238 AppDomain.Unload (AppDomain.CurrentDomain);
241 public static int test_0_unload_inside_appdomain_sync () {
242 AppDomain domain = AppDomain.CreateDomain ("Test3");
245 domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
247 catch (Exception ex) {
248 /* Should throw a ThreadAbortException */
249 Thread.ResetAbort ();
255 public static int test_0_invoke_after_unload () {
256 AppDomain domain = AppDomain.CreateDomain ("DeadInvokeTest");
257 Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
260 if (!RemotingServices.IsTransparentProxy(bar))
263 AppDomain.Unload (domain);
270 } catch (Exception e) {
275 public static int test_0_abort_wait () {
276 AppDomain domain = AppDomain.CreateDomain ("AbortWait");
277 Bar bar = (Bar)domain.CreateInstanceAndUnwrap (typeof (Tests).Assembly.FullName, "Bar");
281 AppDomain.Unload (domain);
285 // FIXME: This does not work yet, because the thread is finalized too
288 public static int test_0_unload_during_unload () {
289 AppDomain domain = AppDomain.CreateDomain ("Test3");
290 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "SlowFinalize");
292 UnloadThread t = new UnloadThread (domain);
294 // Start unloading in a separate thread
295 new Thread (new ThreadStart (t.Run)).Start ();
300 AppDomain.Unload (domain);
303 Console.WriteLine ("OK");