Merge pull request #5210 from alexrp/profiler-runtime-settings
[mono.git] / mono / tests / finalizer-wait.cs
index dde31cbc345b5c5bc1adff7db1d8959454b7234b..7bbfbe38545bd3b0ccef5a7488286416ecead59b 100644 (file)
@@ -2,28 +2,79 @@ using System;
 using System.Threading;
 using System.Runtime.ConstrainedExecution;
 
-class P {
+class P
+{
+       int index;
+       ManualResetEvent mre;
 
-       static public int count = 0;
-       ~P () {
-               // Console.WriteLine ("finalizer done");
-               count++;
+       public static int Count = 0;
+
+       public P (int index, ManualResetEvent mre)
+       {
+               this.index = index;
+               this.mre = mre;
+       }
+
+       ~P ()
+       {
+               mre.Set ();
+
+               Console.Write (String.Format ("[{0}] Finalize\n", index));
+               Count ++;
+               Console.Write (String.Format ("[{0}] Finalize -- end\n", index));
        }
 }
 
-class T {
-       static int Main () {
-               for (int i = 0; i < 1000; ++i) {
-                       var t = new Thread (() => {
-                                       P p = new P ();
-                               });
-                       t.Start ();
-                       t.Join ();
+class Driver
+{
+       static int Main ()
+       {
+               Thread thread;
+               ManualResetEvent mre;
+               int collected, total = 100;
+
+               for (int i = 1; i <= 1000; ++i) {
+                       P.Count = 0;
+
+                       mre = new ManualResetEvent (false);
+
+                       thread = new Thread (() => {
+                               for (int j = 0; j < total; ++j)
+                                       new P (i, mre);
+                       });
+                       thread.Start ();
+                       thread.Join ();
 
                        GC.Collect ();
+
+                       Console.Write (String.Format ("[{0}] Wait for pending finalizers\n", i));
                        GC.WaitForPendingFinalizers ();
-                       if (P.count != i + 1)
-                               return 1;
+                       Console.Write (String.Format ("[{0}] Wait for pending finalizers -- end\n", i));
+
+                       collected = P.Count;
+                       if (collected == 0) {
+                               if (!mre.WaitOne (5000)) {
+                                       Console.Write (String.Format ("[{0}] Finalizer never started\n", i));
+                                       return 1;
+                               }
+
+                               Console.Write (String.Format ("[{0}] Wait for pending finalizers (2)\n", i));
+                               GC.WaitForPendingFinalizers ();
+                               Console.Write (String.Format ("[{0}] Wait for pending finalizers (2) -- end\n", i));
+
+                               collected = P.Count;
+                               if (collected == 0) {
+                                       /* At least 1 finalizer started (as mre has been Set), but P.Count has not been incremented */
+                                       Console.Write (String.Format ("[{0}] Did not wait for finalizers to run\n", i));
+                                       return 2;
+                               }
+                       }
+
+                       if (collected != total) {
+                               /* Not all finalizer finished, before returning from WaitForPendingFinalizers. Or not all objects
+                                * have been garbage collected; this might be due to false pinning */
+                               Console.Write (String.Format ("[{0}] Finalized {1} of {2} objects\n", i, collected, total));
+                       }
                }
                return 0;
        }