|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +import static jdk.jpackage.test.HelloApp.configureAndExecute; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.time.Duration; |
| 30 | +import jdk.jpackage.test.AdditionalLauncher; |
| 31 | +import jdk.jpackage.test.Annotations.Test; |
| 32 | +import jdk.jpackage.test.CfgFile; |
| 33 | +import jdk.jpackage.test.Executor; |
| 34 | +import jdk.jpackage.test.JPackageCommand; |
| 35 | +import jdk.jpackage.test.TKit; |
| 36 | + |
| 37 | +/** |
| 38 | + * Test the child process has a chance to handle Ctrl+C signal. |
| 39 | + */ |
| 40 | + |
| 41 | +/* |
| 42 | + * @test |
| 43 | + * @summary Test case for JDK-8365790 |
| 44 | + * @library /test/jdk/tools/jpackage/helpers |
| 45 | + * @build jdk.jpackage.test.* |
| 46 | + * @build Win8365790Test |
| 47 | + * @requires (os.family == "windows") |
| 48 | + * @run main/othervm/timeout=100 -Xmx512m jdk.jpackage.test.Main |
| 49 | + * --jpt-run=Win8365790Test |
| 50 | + */ |
| 51 | +public class Win8365790Test { |
| 52 | + |
| 53 | + @Test |
| 54 | + public void test() throws InterruptedException, IOException { |
| 55 | + |
| 56 | + var outputDir = TKit.createTempDirectory("response-dir"); |
| 57 | + |
| 58 | + var mainOutputFile = outputDir.resolve("output.txt"); |
| 59 | + var mainTraceFile = outputDir.resolve("trace.txt"); |
| 60 | + |
| 61 | + var probeOutputFile = outputDir.resolve("probe-output.txt"); |
| 62 | + var probeTraceFile = outputDir.resolve("probe-trace.txt"); |
| 63 | + |
| 64 | + var cmd = JPackageCommand |
| 65 | + .helloAppImage(TEST_APP_JAVA + "*UseShutdownHook") |
| 66 | + .ignoreFakeRuntime() |
| 67 | + .addArguments("--java-options", "-Djpackage.test.trace-file=" + mainTraceFile.toString()) |
| 68 | + .addArguments("--arguments", mainOutputFile.toString()) |
| 69 | + .addArguments("--arguments", Long.toString(Duration.ofSeconds(TETS_APP_AUTOCLOSE_TIMEOUT_SECONDS).getSeconds())); |
| 70 | + |
| 71 | + new AdditionalLauncher("probe") { |
| 72 | + @Override |
| 73 | + protected void verify(JPackageCommand cmd) { |
| 74 | + } |
| 75 | + }.addJavaOptions("-Djpackage.test.trace-file=" + probeTraceFile.toString()) |
| 76 | + .addDefaultArguments(probeOutputFile.toString(), Long.toString(Duration.ofSeconds(TETS_APP_AUTOCLOSE_TIMEOUT_SECONDS).getSeconds())) |
| 77 | + .applyTo(cmd); |
| 78 | + |
| 79 | + cmd.executeAndAssertImageCreated(); |
| 80 | + |
| 81 | + cmd.readLauncherCfgFile("probe") |
| 82 | + .add(new CfgFile().addValue("Application", "win.norestart", Boolean.TRUE.toString())) |
| 83 | + .save(cmd.appLauncherCfgPath("probe")); |
| 84 | + |
| 85 | + // Try Ctrl+C signal on a launcher with disabled restart functionality. |
| 86 | + // It will create a single launcher process instead of the parent and the child processes. |
| 87 | + // Ctrl+C always worked for launcher with disabled restart functionality. |
| 88 | + var probeOutput = runLauncher(cmd, "probe", probeTraceFile, probeOutputFile); |
| 89 | + |
| 90 | + if (!probeOutput.equals("shutdown hook executed")) { |
| 91 | + // Ctrl+C signal didn't make it. Test environment doesn't support Ctrl+C signal |
| 92 | + // delivery from the prowershell process to a child process, don't run the main |
| 93 | + // test. |
| 94 | + TKit.throwSkippedException( |
| 95 | + "The environment does NOT support Ctrl+C signal delivery from the prowershell process to a child process"); |
| 96 | + } |
| 97 | + |
| 98 | + var mainOutput = runLauncher(cmd, null, mainTraceFile, mainOutputFile); |
| 99 | + |
| 100 | + TKit.assertEquals("shutdown hook executed", mainOutput, "Check shutdown hook executed"); |
| 101 | + } |
| 102 | + |
| 103 | + private static String runLauncher(JPackageCommand cmd, String launcherName, Path traceFile, Path outputFile) throws IOException { |
| 104 | + // Launch the specified launcher and send Ctrl+C signal to it. |
| 105 | + Thread.ofVirtual().start(() -> { |
| 106 | + configureAndExecute(0, Executor.of("powershell", "-NonInteractive", "-NoLogo", "-NoProfile", "-ExecutionPolicy", "Unrestricted") |
| 107 | + .addArgument("-File").addArgument(TEST_PS1) |
| 108 | + .addArguments("-TimeoutSeconds", Long.toString(Duration.ofSeconds(5).getSeconds())) |
| 109 | + .addArgument("-Executable").addArgument(cmd.appLauncherPath(launcherName)) |
| 110 | + .dumpOutput()); |
| 111 | + }); |
| 112 | + |
| 113 | + TKit.waitForFileCreated(traceFile, Duration.ofSeconds(20), Duration.ofSeconds(2)); |
| 114 | + |
| 115 | + try { |
| 116 | + TKit.waitForFileCreated(outputFile, Duration.ofSeconds(TETS_APP_AUTOCLOSE_TIMEOUT_SECONDS * 2), Duration.ofSeconds(2)); |
| 117 | + } finally { |
| 118 | + TKit.traceFileContents(traceFile, "Test app trace"); |
| 119 | + } |
| 120 | + |
| 121 | + TKit.assertFileExists(outputFile); |
| 122 | + return Files.readString(outputFile); |
| 123 | + } |
| 124 | + |
| 125 | + private static final long TETS_APP_AUTOCLOSE_TIMEOUT_SECONDS = 30; |
| 126 | + |
| 127 | + private static final Path TEST_APP_JAVA = TKit.TEST_SRC_ROOT.resolve("apps/UseShutdownHook.java"); |
| 128 | + private static final Path TEST_PS1 = TKit.TEST_SRC_ROOT.resolve(Path.of("resources/Win8365790Test.ps1")).normalize(); |
| 129 | +} |
0 commit comments