From 8ab700dc482ba329226efc22f013e6495dc2479f Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Fri, 23 Aug 2024 15:17:02 -0400 Subject: [PATCH] Updates test after further testing on Linux. Signed-off-by: Santiago Pericas-Geertsen --- .../helidon/webserver/ConnectionIdleTest.java | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java b/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java index b60f9f085e5..e678923efd9 100644 --- a/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java +++ b/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java @@ -19,7 +19,6 @@ import java.net.SocketException; import java.time.Duration; import java.util.List; -import java.util.concurrent.Callable; import java.util.logging.Logger; import io.helidon.common.http.Http; @@ -28,9 +27,9 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.fail; /** * Tests support for idle connection timeouts. @@ -84,33 +83,26 @@ public void testIdleConnectionClosed() throws Exception { String res = client.receive(); assertThat(res, containsString("Hello World!")); + // second request while connection is active + client.request(Http.Method.GET, + "/hello", + null); + res = client.receive(); + assertThat(res, containsString("Hello World!")); + // wait for connection to time out due to inactivity Thread.sleep(2 * IDLE_TIMEOUT); - // now fail attempting to use connection again - assertEventuallyThrows(SocketException.class, () -> { - client.request(Http.Method.GET, - "/hello", - null); - client.receive(); - return null; - }, 5 * IDLE_TIMEOUT); - } - } - - private static void assertEventuallyThrows(Class exc, Callable runnable, long millis) - throws InterruptedException { - long start = System.currentTimeMillis(); - do { + // try again and either get nothing or an exception try { - runnable.call(); - } catch (Throwable t) { - if (t.getClass().equals(exc)) { - return; - } + client.request(Http.Method.GET, + "/hello", + null); + res = client.receive(); + assertThat(res, is("")); + } catch (SocketException e) { + // falls through as possible outcome } - Thread.sleep(millis / 3); - } while (System.currentTimeMillis() - start <= millis); - fail("Predicate failed after " + millis + " milliseconds"); + } } }