From 27401641178f35420d0fbceae6ba61a3763b4dc9 Mon Sep 17 00:00:00 2001 From: Robert Stupp Date: Tue, 14 Feb 2023 19:26:47 +0100 Subject: [PATCH] Test-only: little cleanup of some Gradle ITs Also adds a shutdown hook for launched processes to kill those when tests are aborted w/ ctrl-c. A process, like the ones spawned by for example `FastJarFormatWorksTest`, can otherwise survive, still "hold" port 8080 and cause subsequent test failures. --- .../gradle/FastJarFormatWorksTest.java | 21 ++++--------------- .../java/io/quarkus/gradle/LaunchUtils.java | 4 +++- .../gradle/LegacyJarFormatWorksTest.java | 17 +-------------- .../gradle/MultiModuleUberJarTest.java | 14 +------------ .../MutableJarFormatBootsInDevModeTest.java | 17 +-------------- .../gradle/QuarkusGradleWrapperTestBase.java | 2 ++ .../gradle/UberJarFormatWorksTest.java | 19 +---------------- 7 files changed, 13 insertions(+), 81 deletions(-) diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/FastJarFormatWorksTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/FastJarFormatWorksTest.java index 16bb935fbc802..1241c56d18c9f 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/FastJarFormatWorksTest.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/FastJarFormatWorksTest.java @@ -7,7 +7,6 @@ import java.io.File; import java.nio.file.Path; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; @@ -17,14 +16,15 @@ import io.quarkus.test.devmode.util.DevModeTestUtils; public class FastJarFormatWorksTest extends QuarkusGradleWrapperTestBase { - private static Future jarRun; @Test public void testFastJarFormatWorks() throws Exception { final File projectDir = getProjectDir("test-that-fast-jar-format-works"); - runGradleWrapper(projectDir, "clean", "build"); + BuildResult result = runGradleWrapper(projectDir, "clean", "build"); + result.getTasks().forEach((k, v) -> System.err.println(" " + k + " --> " + v)); + System.err.println(result.getOutput()); final Path quarkusApp = projectDir.toPath().resolve("build").resolve("quarkus-app"); assertThat(quarkusApp).exists(); @@ -46,22 +46,9 @@ public void testFastJarFormatWorks() throws Exception { String logs = FileUtils.readFileToString(output, "UTF-8"); - assertThatOutputWorksCorrectly(logs); - - // test that the application name and version are properly set - assertThat(DevModeTestUtils.getHttpResponse("/hello", () -> { - return jarRun == null ? null : jarRun.isDone() ? "jar run mode has terminated" : null; - }).equals("hello")); + assertThat(logs).contains("INFO").contains("cdi, resteasy"); } finally { process.destroy(); } } - - private void assertThatOutputWorksCorrectly(String logs) { - assertThat(logs.isEmpty()).isFalse(); - String infoLogLevel = "INFO"; - assertThat(logs.contains(infoLogLevel)).isTrue(); - assertThat(logs.contains("cdi, resteasy")).isTrue(); - } - } diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/LaunchUtils.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/LaunchUtils.java index c6f74375fcd8e..2a27c5df04766 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/LaunchUtils.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/LaunchUtils.java @@ -30,7 +30,9 @@ protected static Process launch(Path jar, File output, Map env) if (env != null) { processBuilder.environment().putAll(env); } - return processBuilder.start(); + Process process = processBuilder.start(); + Runtime.getRuntime().addShutdownHook(new Thread(process::destroyForcibly)); + return process; } public static void dumpFileContentOnFailure(final Callable operation, final File logFile, diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/LegacyJarFormatWorksTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/LegacyJarFormatWorksTest.java index 8a0279bbadb24..bab6e04cdfd3c 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/LegacyJarFormatWorksTest.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/LegacyJarFormatWorksTest.java @@ -7,7 +7,6 @@ import java.io.File; import java.nio.file.Path; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; @@ -17,7 +16,6 @@ import io.quarkus.test.devmode.util.DevModeTestUtils; public class LegacyJarFormatWorksTest extends QuarkusGradleWrapperTestBase { - private static Future jarRun; @Test public void testLegacyJarFormatWorks() throws Exception { @@ -45,22 +43,9 @@ public void testLegacyJarFormatWorks() throws Exception { String logs = FileUtils.readFileToString(output, "UTF-8"); - assertThatOutputWorksCorrectly(logs); - - // test that the application name and version are properly set - assertThat(DevModeTestUtils.getHttpResponse("/hello", () -> { - return jarRun == null ? null : jarRun.isDone() ? "jar run mode has terminated" : null; - }).equals("hello")); + assertThat(logs).contains("INFO").contains("cdi, resteasy"); } finally { process.destroy(); } } - - private void assertThatOutputWorksCorrectly(String logs) { - assertThat(logs.isEmpty()).isFalse(); - String infoLogLevel = "INFO"; - assertThat(logs.contains(infoLogLevel)).isTrue(); - assertThat(logs.contains("cdi, resteasy")).isTrue(); - } - } diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/MultiModuleUberJarTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/MultiModuleUberJarTest.java index 3df21df54e2fb..7c8fca2aca04d 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/MultiModuleUberJarTest.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/MultiModuleUberJarTest.java @@ -7,7 +7,6 @@ import java.io.File; import java.nio.file.Path; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; @@ -17,7 +16,6 @@ import io.quarkus.test.devmode.util.DevModeTestUtils; public class MultiModuleUberJarTest extends QuarkusGradleWrapperTestBase { - private static Future jarRun; @Test public void testUberJarForMultiModule() throws Exception { @@ -44,20 +42,10 @@ public void testUberJarForMultiModule() throws Exception { }, output, ConditionTimeoutException.class); String logs = FileUtils.readFileToString(output, "UTF-8"); - assertThatOutputWorksCorrectly(logs); - // test that the http response is correct - assertThat(DevModeTestUtils.getHttpResponse("/hello", () -> { - return jarRun == null ? null : jarRun.isDone() ? "jar run mode has terminated" : null; - }).equals("hello common")); + assertThat(logs).contains("INFO").contains("cdi, resteasy"); } finally { process.destroy(); } } - - private void assertThatOutputWorksCorrectly(String logs) { - assertThat(logs.isEmpty()).isFalse(); - assertThat(logs.contains("INFO")).isTrue(); - assertThat(logs.contains("cdi, resteasy")).isTrue(); - } } diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/MutableJarFormatBootsInDevModeTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/MutableJarFormatBootsInDevModeTest.java index 74ace56e20fdd..829a75f1421cd 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/MutableJarFormatBootsInDevModeTest.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/MutableJarFormatBootsInDevModeTest.java @@ -8,7 +8,6 @@ import java.io.File; import java.nio.file.Path; import java.util.Collections; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; @@ -18,7 +17,6 @@ import io.quarkus.test.devmode.util.DevModeTestUtils; public class MutableJarFormatBootsInDevModeTest extends QuarkusGradleWrapperTestBase { - private static Future jarRun; @Test public void testFastJarFormatWorks() throws Exception { @@ -47,22 +45,9 @@ public void testFastJarFormatWorks() throws Exception { String logs = FileUtils.readFileToString(output, "UTF-8"); - assertThatOutputWorksCorrectly(logs); - - // test that the application name and version are properly set - assertThat(DevModeTestUtils.getHttpResponse("/hello", () -> { - return jarRun == null ? null : jarRun.isDone() ? "jar run mode has terminated" : null; - }).equals("hello")); + assertThat(logs).contains("INFO").contains("cdi, resteasy"); } finally { process.destroy(); } } - - private void assertThatOutputWorksCorrectly(String logs) { - assertThat(logs.isEmpty()).isFalse(); - String infoLogLevel = "INFO"; - assertThat(logs.contains(infoLogLevel)).isTrue(); - assertThat(logs.contains("cdi, resteasy")).isTrue(); - } - } diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusGradleWrapperTestBase.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusGradleWrapperTestBase.java index 8ffd6bdb97e79..8980070833bee 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusGradleWrapperTestBase.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/QuarkusGradleWrapperTestBase.java @@ -49,6 +49,8 @@ public BuildResult runGradleWrapper(File projectDir, String... args) throws IOEx .redirectError(logOutput) .start(); + Runtime.getRuntime().addShutdownHook(new Thread(p::destroyForcibly)); + //long timeout for native tests //that may also need to download docker boolean done = p.waitFor(10, TimeUnit.MINUTES); diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/UberJarFormatWorksTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/UberJarFormatWorksTest.java index 6746780908197..c353748b8c578 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/UberJarFormatWorksTest.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/UberJarFormatWorksTest.java @@ -7,7 +7,6 @@ import java.io.File; import java.nio.file.Path; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; @@ -18,8 +17,6 @@ public class UberJarFormatWorksTest extends QuarkusGradleWrapperTestBase { - private static Future jarRun; - @Test public void testUberJarFormatWorks() throws Exception { @@ -45,23 +42,9 @@ public void testUberJarFormatWorks() throws Exception { String logs = FileUtils.readFileToString(output, "UTF-8"); - assertThatOutputWorksCorrectly(logs); - - // test that the application name and version are properly set - assertThat(DevModeTestUtils.getHttpResponse("/hello", () -> { - return jarRun == null ? null : jarRun.isDone() ? "jar run mode has terminated" : null; - }).equals("hello")); + assertThat(logs).contains("INFO").contains("cdi, resteasy"); } finally { process.destroy(); } - } - - private void assertThatOutputWorksCorrectly(String logs) { - assertThat(logs.isEmpty()).isFalse(); - String infoLogLevel = "INFO"; - assertThat(logs.contains(infoLogLevel)).isTrue(); - assertThat(logs.contains("cdi, resteasy")).isTrue(); - } - }