diff --git a/src/test/java/io/floci/aws/config/TlsProxyServerTest.java b/src/test/java/io/floci/aws/config/TlsProxyServerTest.java index 436d4501a0..8eddaf8845 100644 --- a/src/test/java/io/floci/aws/config/TlsProxyServerTest.java +++ b/src/test/java/io/floci/aws/config/TlsProxyServerTest.java @@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -454,16 +455,12 @@ class TlsProxyServerTest { } } - private void awaitListening(int port) throws Exception { - long deadline = System.currentTimeMillis() + 10_000; - while (System.currentTimeMillis() < deadline) { - try (Socket s = new Socket("127.0.0.1", port)) { - return; - } catch (IOException e) { - Thread.sleep(50); - } - } - fail("port " + port + " not listening within timeout"); + private void awaitListening(int port) { + InetAddress loopback = InetAddress.ofLiteral("127.0.0.1"); + await("TLS proxy on 127.0.0.1:" + port) + .pollInterval(50, TimeUnit.MILLISECONDS) + .atMost(10, TimeUnit.SECONDS) + .until(() -> accepts(loopback, port)); } private static boolean accepts(InetAddress address, int port) {