From 3ff8fb7864eedb8a44e23f1b3be4e327168b033b Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Fri, 22 Mar 2024 13:03:19 +0100 Subject: [PATCH] Avoid all caching in DevModeClient Hoping it could stabilize DevMojoIT. --- .../test/devmode/util/DevModeClient.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/test-framework/devmode-test-utils/src/main/java/io/quarkus/test/devmode/util/DevModeClient.java b/test-framework/devmode-test-utils/src/main/java/io/quarkus/test/devmode/util/DevModeClient.java index cb71d4e0449f6..02d4a040a781f 100644 --- a/test-framework/devmode-test-utils/src/main/java/io/quarkus/test/devmode/util/DevModeClient.java +++ b/test-framework/devmode-test-utils/src/main/java/io/quarkus/test/devmode/util/DevModeClient.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -157,6 +158,7 @@ public String getHttpResponse(String path, boolean allowError, Supplier public String getHttpResponse(String path, boolean allowError, Supplier brokenReason, long timeout, TimeUnit tu) { AtomicReference resp = new AtomicReference<>(); + await() .pollDelay(1, TimeUnit.SECONDS) .atMost(timeout, tu).until(() -> { @@ -166,12 +168,15 @@ public String getHttpResponse(String path, boolean allowError, Supplier return true; } try { - URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path))); + URL url = prepareUrl(path); + String content; if (!allowError) { content = IOUtils.toString(url, StandardCharsets.UTF_8); } else { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDefaultUseCaches(false); + conn.setUseCaches(false); // the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.8 conn.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2"); if (conn.getResponseCode() >= 400) { @@ -179,7 +184,9 @@ public String getHttpResponse(String path, boolean allowError, Supplier } else { content = IOUtils.toString(conn.getInputStream(), StandardCharsets.UTF_8); } + conn.disconnect(); } + resp.set(content); return true; } catch (Exception e) { @@ -203,8 +210,10 @@ public boolean getHttpResponse(String path, int expectedStatus, long timeout, Ti .pollDelay(1, TimeUnit.SECONDS) .atMost(timeout, tu).until(() -> { try { - URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path))); + URL url = prepareUrl(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDefaultUseCaches(false); + connection.setUseCaches(false); // the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.2 connection.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2"); if (connection.getResponseCode() == expectedStatus) { @@ -230,8 +239,10 @@ public boolean getStrictHttpResponse(String path, int expectedStatus) { .pollDelay(1, TimeUnit.SECONDS) .atMost(5, TimeUnit.MINUTES).until(() -> { try { - URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path))); + URL url = prepareUrl(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDefaultUseCaches(false); + connection.setUseCaches(false); // the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.2 connection.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2"); code.set(connection.getResponseCode() == expectedStatus); @@ -258,8 +269,10 @@ public String get(String urlStr) throws IOException { public boolean isCode(String path, int code) { try { - URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path))); + URL url = prepareUrl(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDefaultUseCaches(false); + connection.setUseCaches(false); // the default Accept header used by HttpURLConnection is not compatible with // RESTEasy negotiation as it uses q=.2 connection.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2"); @@ -268,4 +281,16 @@ public boolean isCode(String path, int code) { return false; } } + + private URL prepareUrl(String path) throws MalformedURLException { + String urlString = "http://localhost:" + port + (path.startsWith("/") ? path : "/" + path); + if (urlString.contains("?")) { + urlString += "&"; + } else { + urlString += "?"; + } + urlString += "_test_timestamp=" + System.currentTimeMillis(); + + return new URL(urlString); + } }