1/*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
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
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4954921 8009259
27 * @library /test/lib
28 * @modules java.base/jdk.internal.ref
29 *          java.base/jdk.internal.misc
30 * @build jdk.test.lib.*
31 * @build jdk.test.lib.process.*
32 * @run main ExitOnThrow
33 * @summary Ensure that if a cleaner throws an exception then the VM exits
34 */
35import java.util.Arrays;
36
37import jdk.internal.ref.Cleaner;
38import jdk.test.lib.JDKToolLauncher;
39import jdk.test.lib.process.OutputAnalyzer;
40import jdk.test.lib.process.ProcessTools;
41
42public class ExitOnThrow {
43
44    static final String cp = System.getProperty("test.classes", ".");
45
46    public static void main(String[] args) throws Exception {
47        if (args.length == 0) {
48            String[] cmd = JDKToolLauncher.createUsingTestJDK("java")
49                                          .addToolArg("-cp")
50                                          .addToolArg(cp)
51                                          .addToolArg("ExitOnThrow")
52                                          .addToolArg("-executeCleaner")
53                                          .getCommand();
54            ProcessBuilder pb = new ProcessBuilder(cmd);
55            OutputAnalyzer out = ProcessTools.executeProcess(pb);
56            System.out.println("======================");
57            System.out.println(Arrays.toString(cmd));
58            String msg = " stdout: [" + out.getStdout() + "]\n" +
59                         " stderr: [" + out.getStderr() + "]\n" +
60                         " exitValue = " + out.getExitValue() + "\n";
61            System.out.println(msg);
62
63            if (out.getExitValue() != 1)
64                throw new RuntimeException("Unexpected exit code: " +
65                                           out.getExitValue());
66
67        } else {
68            Cleaner.create(new Object(),
69                           () -> { throw new RuntimeException("Foo!"); } );
70            while (true) {
71                System.gc();
72                Thread.sleep(100);
73            }
74        }
75    }
76
77}
78