From fb67a6fafe1efbb3f44ddbdfae7b39a22d7e87d7 Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Mon, 26 Oct 2026 06:38:42 -0500 Subject: [PATCH] test(elbv2): wait for TLS listener binding during readiness (#6626) --- .../ElbV2HttpsDataPlaneIntegrationTest.java | 6 +- .../services/elbv2/ElbV2TlsReadiness.java | 25 +++++ .../services/elbv2/ElbV2TlsReadinessTest.java | 92 +++++++++++++++++++ 3 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadiness.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadinessTest.java diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.java index 8d139af597..fa9823a045 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HttpsDataPlaneIntegrationTest.java @@ -1678,8 +1678,7 @@ class ElbV2HttpsDataPlaneIntegrationTest { private static HttpsResult awaitHttpsAtPort(String host, int port, Certificate... trustedCertificates) { HttpsResult[] result = new HttpsResult[1]; - await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(100)) - .ignoreException(SSLHandshakeException.class).untilAsserted(() -> { + ElbV2TlsReadiness.await(() -> { result[0] = httpsRequest(host, port, trustedCertificates); }); return result[0]; @@ -1687,8 +1686,7 @@ class ElbV2HttpsDataPlaneIntegrationTest { private static void awaitHttpsMaterial(String host, int status, String body, Certificate expected, Certificate... trustedCertificates) { - await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(100)) - .ignoreException(SSLHandshakeException.class).untilAsserted(() -> { + ElbV2TlsReadiness.await(() -> { HttpsResult response = httpsRequest(host, HTTPS_PORT, trustedCertificates); assertEquals(status, response.statusCode()); assertEquals(body, response.body()); diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadiness.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadiness.java new file mode 100644 index 0000000000..cb39baaf37 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadiness.java @@ -0,0 +1,25 @@ +package io.github.hectorvent.floci.services.elbv2; + +import org.awaitility.core.ThrowingRunnable; + +import javax.net.ssl.SSLHandshakeException; +import java.net.ConnectException; +import java.time.Duration; + +import static org.awaitility.Awaitility.with; + +final class ElbV2TlsReadiness { + + private ElbV2TlsReadiness() {} + + static void await(ThrowingRunnable assertion) { + await(Duration.ofSeconds(10), assertion); + } + + static void await(Duration timeout, ThrowingRunnable assertion) { + with().atMost(timeout).pollInterval(Duration.ofMillis(100)) + .ignoreExceptionsMatching(failure -> failure instanceof ConnectException + || failure instanceof SSLHandshakeException) + .untilAsserted(assertion); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadinessTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadinessTest.java new file mode 100644 index 0000000000..ffcc665ca0 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2TlsReadinessTest.java @@ -0,0 +1,92 @@ +package io.github.hectorvent.floci.services.elbv2; + +import org.awaitility.core.ConditionTimeoutException; +import org.junit.jupiter.api.Test; + +import javax.net.ssl.SSLHandshakeException; +import java.io.IOException; +import java.net.ConnectException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.time.Duration; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.*; + +class ElbV2TlsReadinessTest { + + @Test + void connectionRefusalBeforeBindDoesNotEndReadiness() throws Exception { + try (ServerSocket listener = new ServerSocket()) { + int port = unusedLoopbackPort(); + AtomicInteger refusals = new AtomicInteger(); + ElbV2TlsReadiness.await(() -> { + try (Socket socket = new Socket()) { + socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port), 500); + try (Socket accepted = listener.accept()) { + accepted.getOutputStream().write(42); + socket.setSoTimeout(500); + assertEquals(42, socket.getInputStream().read()); + } + } catch (ConnectException refusal) { + assertEquals(1, refusals.incrementAndGet()); + listener.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), port)); + listener.setSoTimeout(500); + throw refusal; + } + }); + assertEquals(1, refusals.get()); + } + } + + @Test + void permanentlyStoppedListenerStillTimesOut() throws Exception { + int port = unusedLoopbackPort(); + AtomicInteger attempts = new AtomicInteger(); + assertThrows(ConditionTimeoutException.class, () -> ElbV2TlsReadiness.await( + Duration.ofMillis(350), () -> { + attempts.incrementAndGet(); + try (Socket socket = new Socket()) { + socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port), 500); + fail("Stopped listener unexpectedly accepted a connection"); + } + })); + assertTrue(attempts.get() > 1); + } + + @Test + void wrongResponseStillTimesOut() { + assertThrows(ConditionTimeoutException.class, () -> ElbV2TlsReadiness.await( + Duration.ofMillis(350), () -> assertEquals("winner", "wrong listener"))); + } + + @Test + void transientHandshakeFailureRetainsAcceptedRetry() { + AtomicInteger attempts = new AtomicInteger(); + ElbV2TlsReadiness.await(() -> { + if (attempts.incrementAndGet() == 1) { + throw new SSLHandshakeException("TLS material not installed yet"); + } + }); + assertEquals(2, attempts.get()); + } + + @Test + void unrelatedIoFailureIsNotRetried() { + AtomicInteger attempts = new AtomicInteger(); + IOException failure = new IOException("Unexpected protocol failure"); + assertSame(failure, assertThrows(IOException.class, () -> ElbV2TlsReadiness.await(() -> { + attempts.incrementAndGet(); + throw failure; + }))); + assertEquals(1, attempts.get()); + } + + private static int unusedLoopbackPort() throws IOException { + try (ServerSocket reservation = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { + return reservation.getLocalPort(); + } + } +}