Light, fluffy, and always free - The AWS Local Emulator alternative
test(elbv2): wait for TLS listener binding during readiness (#6626)
Andre Kuraitfb67a6fafe1e
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.javaindex 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.javanew file mode 100644index 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.javanew file mode 100644index 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();+ }+ }+}