[System] Tweak socket test
[mono-project.git] / mono / tests / appdomain-unload.cs
blobe5ce63689219231bba99f26ef95eb35baafc8c1f
1 using System;
2 using System.Threading;
3 using System.Reflection;
4 using System.Runtime.Remoting;
6 [Serializable]
7 public class Foo {
9 ~Foo () {
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);
17 return x + 1;
20 public void start_wait () {
21 Action a = delegate () {
22 Thread.Sleep (10000);
24 a.BeginInvoke (null, null);
28 [Serializable]
29 public class SlowFinalize {
31 ~SlowFinalize () {
32 Console.WriteLine ("FINALIZE1.");
33 try {
34 Thread.Sleep (500);
36 catch (Exception ex) {
37 Console.WriteLine ("A: " + ex);
39 Console.WriteLine ("FINALIZE2.");
43 [Serializable]
44 public class AThread {
46 public AThread () {
47 new Thread (new ThreadStart (Run)).Start ();
50 public void Run () {
51 try {
52 while (true)
53 Thread.Sleep (100);
55 catch (ThreadAbortException ex) {
56 Console.WriteLine ("Thread aborted correctly.");
61 // A Thread which refuses to die
62 public class BThread : MarshalByRefObject {
64 bool stop;
66 public BThread () {
67 new Thread (new ThreadStart (Run)).Start ();
70 public void Stop () {
71 stop = true;
74 public void Run () {
75 try {
76 while (true)
77 Thread.Sleep (100);
79 catch (ThreadAbortException ex) {
80 while (!stop)
81 Thread.Sleep (100);
86 public class UnloadThread {
88 AppDomain domain;
90 public UnloadThread (AppDomain domain) {
91 this.domain = domain;
94 public void Run () {
95 Console.WriteLine ("UNLOAD1");
96 AppDomain.Unload (domain);
97 Console.WriteLine ("UNLOAD2");
101 class CrossDomainTester : MarshalByRefObject
105 public class Tests
107 public static int Main(string[] args) {
108 if (args.Length == 0)
109 return TestDriver.RunTests (typeof (Tests), new String[] { "-v" });
110 else
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);
124 return 0;
127 public static int test_0_unload_default () {
128 try {
129 AppDomain.Unload (Thread.GetDomain ());
131 catch (CannotUnloadAppDomainException) {
132 return 0;
134 return 1;
137 public static int test_0_unload_after_unload () {
138 AppDomain domain = AppDomain.CreateDomain ("Test2");
139 AppDomain.Unload (domain);
141 try {
142 AppDomain.Unload (domain);
144 catch (Exception) {
145 return 0;
148 return 1;
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 ())
156 return 1;
158 AppDomain.Unload (domain);
160 return 0;
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");
166 Thread.Sleep (100);
168 AppDomain.Unload (domain);
170 return 0;
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");
178 Thread.Sleep (100);
180 try {
181 AppDomain.Unload (domain);
183 catch (Exception) {
184 // Try again
185 o.Stop ();
186 AppDomain.Unload (domain);
187 return 0;
190 return 1;
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);
209 return 0;
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));
233 return 0;
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");
244 try {
245 domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
247 catch (Exception ex) {
248 /* Should throw a ThreadAbortException */
249 Thread.ResetAbort ();
252 return 0;
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");
258 int x;
260 if (!RemotingServices.IsTransparentProxy(bar))
261 return 3;
263 AppDomain.Unload (domain);
265 try {
266 x = bar.test (123);
267 if (x == 124)
268 return 1;
269 return 2;
270 } catch (Exception e) {
271 return 0;
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");
278 int x;
280 bar.start_wait ();
281 AppDomain.Unload (domain);
282 return 0;
285 // FIXME: This does not work yet, because the thread is finalized too
286 // early
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 ();
297 Thread.Sleep (100);
299 try {
300 AppDomain.Unload (domain);
302 catch (Exception) {
303 Console.WriteLine ("OK");
306 return 0;