2010-05-11 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / gchandles.cs
blob14e9d983e46006ba089159f89d455bce829660a6
1 using System;
2 using System.Runtime.InteropServices;
4 public class Test1
6 public GCHandle self;
7 public static bool fail;
8 public static Test1 instance;
10 ~Test1 () {
11 if (self.Target == null)
12 fail = true;
16 public class Test2
18 public GCHandle self;
19 public static bool fail;
20 public static Test2 instance;
22 ~Test2 () {
23 if (self.Target == null)
24 fail = true;
28 public class Tests
30 public static int Main (String[] args) {
31 return TestDriver.RunTests (typeof (Tests), args);
34 static void create1 () {
35 Test1.instance = new Test1 ();
36 Test1.instance.self = GCHandle.Alloc (Test1.instance, GCHandleType.WeakTrackResurrection);
37 Test1.instance = null;
40 public static unsafe int test_0_track_resurrection () {
41 /* The GCHandle should not be cleared before calling the finalizers */
42 create1 ();
43 GC.Collect ();
44 GC.WaitForPendingFinalizers ();
46 // WaitForPendingFinalizers doesn't seem to work ?
47 System.Threading.Thread.Sleep (100);
48 /* If the finalizers do not get ran by this time, the test will still succeed */
49 return Test1.fail ? 1 : 0;
52 static void create2 () {
53 Test2.instance = new Test2 ();
54 object o = new object ();
55 Test2.instance.self = GCHandle.Alloc (o, GCHandleType.WeakTrackResurrection);
56 Test2.instance.self.Target = Test2.instance;
57 Test2.instance = null;
60 public static int test_0_track_resurrection_set_target () {
61 /* Same test but the handle target is set dynamically after it is created */
62 create2 ();
63 GC.Collect ();
64 GC.WaitForPendingFinalizers ();
66 System.Threading.Thread.Sleep (100);
67 return Test2.fail ? 1 : 0;