2004-02-14 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / tests / appdomain-unload.cs
1 using System;
2 using System.Threading;
3
4 [Serializable]
5 public class Foo {
6
7         ~Foo () {
8                 Console.WriteLine ("FINALIZING IN DOMAIN " + AppDomain.CurrentDomain.FriendlyName + ": " + AppDomain.CurrentDomain.IsFinalizingForUnload ());
9         }
10 }
11
12 [Serializable]
13 public class SlowFinalize {
14
15         ~SlowFinalize () {
16                 Console.WriteLine ("FINALIZE1.");
17                 try {
18                         Thread.Sleep (500);
19                 }
20                 catch (Exception ex) {
21                         Console.WriteLine ("A: " + ex);
22                 }
23                 Console.WriteLine ("FINALIZE2.");
24         }
25 }
26
27 [Serializable]
28 public class AThread {
29
30         public AThread () {
31                 new Thread (new ThreadStart (Run)).Start ();
32         }
33
34         public void Run () {
35                 try {
36                         while (true)
37                                 Thread.Sleep (100);
38                 }
39                 catch (ThreadAbortException ex) {
40                         Console.WriteLine ("Thread aborted correctly.");
41                 }
42         }
43 }
44
45 [Serializable]
46 // A Thread which refuses to die
47 public class BThread {
48
49         public BThread () {
50                 new Thread (new ThreadStart (Run)).Start ();
51         }
52
53         public void Run () {
54                 try {
55                         while (true)
56                                 Thread.Sleep (100);
57                 }
58                 catch (ThreadAbortException ex) {
59                         Thread.Sleep (1000000000);
60                 }
61         }
62 }
63
64 public class UnloadThread {
65
66         AppDomain domain;
67
68         public UnloadThread (AppDomain domain) {
69                 this.domain = domain;
70         }
71
72         public void Run () {
73                 Console.WriteLine ("UNLOAD1");
74                 AppDomain.Unload (domain);
75                 Console.WriteLine ("UNLOAD2");
76         }
77 }
78
79 public class Tests
80 {
81         public static int Main() {
82                 return TestDriver.RunTests (typeof (Tests));
83         }
84
85         public static int test_0_unload () {
86                 for (int i = 0; i < 10; ++i) {
87                         AppDomain appDomain = AppDomain.CreateDomain("Test-unload" + i);
88                         AppDomain.Unload(appDomain);
89                 }
90
91                 return 0;
92         }
93
94         public static int test_0_unload_default () {
95                 try {
96                         AppDomain.Unload (Thread.GetDomain ());
97                 }
98                 catch (CannotUnloadAppDomainException) {
99                         return 0;
100                 }
101                 return 1;
102         }
103
104         public static int test_0_unload_after_unload () {
105                 AppDomain domain = AppDomain.CreateDomain ("Test2");
106                 AppDomain.Unload (domain);
107
108                 try {
109                         AppDomain.Unload (domain);
110                 }
111                 catch (Exception) {
112                         return 0;
113                 }
114
115                 return 1;
116         }
117
118         public static int test_0_is_finalizing () {
119                 AppDomain domain = AppDomain.CreateDomain ("Test-is-finalizing");
120                 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Foo");
121
122                 if (domain.IsFinalizingForUnload ())
123                         return 1;
124
125                 AppDomain.Unload (domain);
126
127                 return 0;
128         }
129
130         public static int test_0_unload_with_active_threads () {
131                 AppDomain domain = AppDomain.CreateDomain ("Test3");
132                 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "AThread");
133                 Thread.Sleep (100);
134
135                 AppDomain.Unload (domain);
136
137                 return 0;
138         }
139
140         public static int test_0_unload_with_active_threads_timeout () {
141                 AppDomain domain = AppDomain.CreateDomain ("Test4");
142                 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
143                 Thread.Sleep (100);
144
145                 try {
146                         AppDomain.Unload (domain);
147                 }
148                 catch (Exception) {
149                         return 0;
150                 }
151
152                 return 1;
153         }
154
155         // FIXME: This does not work yet, because the thread is finalized too
156         // early
157         /*
158         public static int test_0_unload_during_unload () {
159                 AppDomain domain = AppDomain.CreateDomain ("Test3");
160                 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "SlowFinalize");
161
162                 UnloadThread t = new UnloadThread (domain);
163
164                 // Start unloading in a separate thread
165                 new Thread (new ThreadStart (t.Run)).Start ();
166
167                 Thread.Sleep (100);
168
169                 try {
170                         AppDomain.Unload (domain);
171                 }
172                 catch (Exception) {
173                         Console.WriteLine ("OK");
174                 }
175
176                 return 0;
177         }       
178 */
179 }
180