19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.util.concurrent.Phaser; |
24 import java.util.ArrayList; |
25 import java.util.concurrent.TimeUnit; |
25 import java.util.List; |
26 import java.util.concurrent.TimeoutException; |
|
27 |
26 |
28 import jdk.test.lib.dcmd.CommandExecutor; |
27 import jdk.test.lib.OutputAnalyzer; |
29 import jdk.test.lib.dcmd.JMXExecutor; |
28 import jdk.test.lib.ProcessTools; |
30 import jdk.test.lib.Utils; |
|
31 |
29 |
32 /* |
30 /* |
33 * @test |
31 * @test |
34 * @summary Test of diagnostic command GC.run_finalization |
32 * @summary Test of diagnostic command GC.run_finalization |
35 * @library /testlibrary |
33 * @library /testlibrary |
37 * java.compiler |
35 * java.compiler |
38 * java.management |
36 * java.management |
39 * jdk.jvmstat/sun.jvmstat.monitor |
37 * jdk.jvmstat/sun.jvmstat.monitor |
40 * @build jdk.test.lib.* |
38 * @build jdk.test.lib.* |
41 * @build jdk.test.lib.dcmd.* |
39 * @build jdk.test.lib.dcmd.* |
42 * @run main/othervm RunFinalizationTest |
40 * @build RunFinalizationTest FinalizationRunner |
|
41 * @run main RunFinalizationTest |
43 */ |
42 */ |
44 public class RunFinalizationTest { |
43 public class RunFinalizationTest { |
45 private static final long TIMEOUT = Utils.adjustTimeout(15000); // 15s |
44 private final static String TEST_APP_NAME = "FinalizationRunner"; |
46 private static final Phaser ph = new Phaser(3); |
|
47 static volatile boolean wasFinalized = false; |
|
48 static volatile boolean wasInitialized = false; |
|
49 |
45 |
50 static class MyObject { |
46 public static void main(String ... args) throws Exception { |
51 public MyObject() { |
47 List<String> javaArgs = new ArrayList<>(); |
52 /* Make sure object allocation/deallocation is not optimized out */ |
48 javaArgs.add("-cp"); |
53 wasInitialized = true; |
49 javaArgs.add(System.getProperty("test.class.path")); |
54 } |
50 javaArgs.add(TEST_APP_NAME); |
|
51 ProcessBuilder testAppPb = ProcessTools.createJavaProcessBuilder(javaArgs.toArray(new String[javaArgs.size()])); |
55 |
52 |
56 protected void finalize() { |
53 OutputAnalyzer out = ProcessTools.executeProcess(testAppPb); |
57 if (!Thread.currentThread().getName().equals("Finalizer")) { |
54 out.stderrShouldNotMatch("^" + FinalizationRunner.FAILED + ".*") |
58 wasFinalized = true; |
55 .stdoutShouldMatch("^" + FinalizationRunner.PASSED + ".*"); |
59 ph.arrive(); |
|
60 } else { |
|
61 ph.arriveAndAwaitAdvance(); |
|
62 } |
|
63 } |
|
64 } |
|
65 |
|
66 public static MyObject o; |
|
67 |
|
68 private static void run(CommandExecutor executor) { |
|
69 o = new MyObject(); |
|
70 o = null; |
|
71 System.gc(); |
|
72 executor.execute("GC.run_finalization"); |
|
73 |
|
74 System.out.println("Waiting for signal from finalizer"); |
|
75 |
|
76 long targetTime = System.currentTimeMillis() + TIMEOUT; |
|
77 while (System.currentTimeMillis() < targetTime) { |
|
78 try { |
|
79 ph.awaitAdvanceInterruptibly(ph.arrive(), 200, TimeUnit.MILLISECONDS); |
|
80 System.out.println("Received signal"); |
|
81 break; |
|
82 } catch (InterruptedException e) { |
|
83 fail("Test error: Interrupted while waiting for signal from finalizer", e); |
|
84 } catch (TimeoutException e) { |
|
85 System.out.println("Haven't received signal in 200ms. Retrying ..."); |
|
86 } |
|
87 } |
|
88 |
|
89 if (!wasFinalized) { |
|
90 fail("Test failure: Object was not finalized"); |
|
91 } |
|
92 } |
|
93 |
|
94 public static void main(String ... args) { |
|
95 MyObject o = new MyObject(); |
|
96 o = null; |
|
97 Runtime.getRuntime().addShutdownHook(new Thread(()->{ |
|
98 run(new JMXExecutor()); |
|
99 })); |
|
100 } |
|
101 |
|
102 private static void fail(String msg, Exception e) { |
|
103 throw new Error(msg, e); |
|
104 } |
|
105 |
|
106 private static void fail(String msg) { |
|
107 throw new Error(msg); |
|
108 } |
56 } |
109 } |
57 } |