diff --git a/src/test/java/io/floci/aws/config/TlsProxyServerTest.java b/src/test/java/io/floci/aws/config/TlsProxyServerTest.java index 8eddaf8845..89545085d9 100644 --- a/src/test/java/io/floci/aws/config/TlsProxyServerTest.java +++ b/src/test/java/io/floci/aws/config/TlsProxyServerTest.java @@ -2,6 +2,7 @@ package io.floci.aws.config; import io.floci.aws.config.TlsProxyServer.ServerNameTarget; import io.floci.aws.testing.ParallelLocks; +import io.floci.aws.testing.TcpListenerProbe; import io.floci.aws.testutil.FreePorts; import io.vertx.core.Context; import io.vertx.core.Vertx; @@ -12,7 +13,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.parallel.ResourceLock; -import java.io.IOException; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -122,7 +122,7 @@ class TlsProxyServerTest { proxy = new TlsProxyServer(vertx, configWith(true, publicPort, 0), "127.0.0.1", 4510, 4511); awaitListening(publicPort); - assertFalse(accepts(external, publicPort), + assertFalse(TcpListenerProbe.accepts(new InetSocketAddress(external, publicPort)), "a proxy configured for 127.0.0.1 must not be reachable on " + external.getHostAddress()); } @@ -456,20 +456,11 @@ class TlsProxyServerTest { } private void awaitListening(int port) { - InetAddress loopback = InetAddress.ofLiteral("127.0.0.1"); + InetSocketAddress listener = new InetSocketAddress("127.0.0.1", port); 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) { - try (Socket s = new Socket()) { - s.connect(new InetSocketAddress(address, port), 1000); - return true; - } catch (IOException e) { - return false; - } + .until(() -> TcpListenerProbe.accepts(listener)); } private static InetAddress nonLoopbackIpv4() throws SocketException { diff --git a/src/test/java/io/floci/aws/services/iot/IotMqttBrokerServiceTest.java b/src/test/java/io/floci/aws/services/iot/IotMqttBrokerServiceTest.java index f05be0f48b..896059b6c5 100644 --- a/src/test/java/io/floci/aws/services/iot/IotMqttBrokerServiceTest.java +++ b/src/test/java/io/floci/aws/services/iot/IotMqttBrokerServiceTest.java @@ -25,6 +25,7 @@ import io.floci.aws.services.sns.SnsService; import io.floci.aws.services.sqs.SqsService; import io.floci.aws.testing.IotWebSocketSigV4; import io.floci.aws.testing.ParallelLocks; +import io.floci.aws.testing.TcpListenerProbe; import io.floci.aws.testing.ThreadStates; import io.floci.aws.testutil.FreePorts; import io.netty.handler.codec.mqtt.MqttProperties; @@ -2515,12 +2516,7 @@ class IotMqttBrokerServiceTest { } private static boolean accepts(int port) { - try (Socket socket = new Socket()) { - socket.connect(new InetSocketAddress("127.0.0.1", port), 500); - return true; - } catch (IOException refused) { - return false; - } + return TcpListenerProbe.accepts(new InetSocketAddress("127.0.0.1", port), 500); } /** Vert.x resolves close() a moment before the OS releases the port; poll like the lazy-start test. */ diff --git a/src/test/java/io/floci/aws/services/rds/RdsStopStartDockerNightlyIntegrationTest.java b/src/test/java/io/floci/aws/services/rds/RdsStopStartDockerNightlyIntegrationTest.java index 8a99d2569a..27557170a9 100644 --- a/src/test/java/io/floci/aws/services/rds/RdsStopStartDockerNightlyIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/rds/RdsStopStartDockerNightlyIntegrationTest.java @@ -9,6 +9,7 @@ import io.floci.aws.services.rds.model.DbInstance; import io.floci.aws.testing.AwsTestClients; import io.floci.aws.testing.EventRuleQueue; import io.floci.aws.testing.RestAssuredJsonUtils; +import io.floci.aws.testing.TcpListenerProbe; import io.quarkus.test.junit.QuarkusTest; import io.restassured.specification.RequestSpecification; import jakarta.inject.Inject; @@ -24,7 +25,7 @@ import tools.jackson.databind.JsonNode; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; -import java.net.Socket; +import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -135,8 +136,9 @@ class RdsStopStartDockerNightlyIntegrationTest { DbInstance running = rdsService.getDbInstance(id); String containerId = running.getContainerId(); int port = running.getEndpoint().port(); + InetSocketAddress endpoint = new InetSocketAddress("localhost", port); psql(containerId, "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('before-stop');"); - assertTrue(portOpen(port), "the endpoint must accept connections while available"); + assertTrue(TcpListenerProbe.accepts(endpoint), "the endpoint must accept connections while available"); query("StopDBInstance") .formParam("DBInstanceIdentifier", id) @@ -147,7 +149,7 @@ class RdsStopStartDockerNightlyIntegrationTest { DbInstance stopped = rdsService.getDbInstance(id); assertNull(stopped.getContainerId(), "a stopped instance has no container"); assertFalse(containerExists(containerId), "the container is gone while stopped"); - assertFalse(portOpen(port), "the endpoint refuses connections while stopped"); + assertFalse(TcpListenerProbe.accepts(endpoint), "the endpoint refuses connections while stopped"); query("ModifyDBInstance") .formParam("DBInstanceIdentifier", id) @@ -167,7 +169,7 @@ class RdsStopStartDockerNightlyIntegrationTest { waitForInstance(id, "available"); DbInstance started = rdsService.getDbInstance(id); assertTrue(started.getEndpoint().port() == port, "the endpoint keeps its port"); - assertTrue(portOpen(port), "the endpoint accepts connections again"); + assertTrue(TcpListenerProbe.accepts(endpoint), "the endpoint accepts connections again"); assertTrue(psql(started.getContainerId(), "SELECT v FROM marker;").contains("before-stop"), "the row written before the stop must survive on the volume"); @@ -229,14 +231,6 @@ class RdsStopStartDockerNightlyIntegrationTest { .when().post("/").then().statusCode(200); } - private static boolean portOpen(int port) { - try (Socket socket = new Socket("localhost", port)) { - return socket.isConnected(); - } catch (IOException e) { - return false; - } - } - private boolean containerExists(String containerId) { try { dockerClient.inspectContainerCmd(containerId).exec(); diff --git a/src/test/java/io/floci/aws/services/redshiftserverless/RedshiftServerlessRuntimeDockerNightlyIntegrationTest.java b/src/test/java/io/floci/aws/services/redshiftserverless/RedshiftServerlessRuntimeDockerNightlyIntegrationTest.java index bcf31d7e48..f6ce22fe64 100644 --- a/src/test/java/io/floci/aws/services/redshiftserverless/RedshiftServerlessRuntimeDockerNightlyIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/redshiftserverless/RedshiftServerlessRuntimeDockerNightlyIntegrationTest.java @@ -3,6 +3,7 @@ package io.floci.aws.services.redshiftserverless; import io.floci.aws.core.common.jdbc.JdbcConnections; import io.floci.aws.services.redshiftserverless.model.ScheduledAction; import io.floci.aws.testing.RestAssuredJsonUtils; +import io.floci.aws.testing.TcpListenerProbe; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.ExtractableResponse; import io.restassured.response.Response; @@ -16,7 +17,6 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import java.net.InetSocketAddress; -import java.net.Socket; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -294,15 +294,17 @@ class RedshiftServerlessRuntimeDockerNightlyIntegrationTest { int port = RestAssuredJsonUtils.awsAction("RedshiftServerless", "CreateWorkgroup", """ {"workgroupName":"%s","namespaceName":"%s"} """.formatted(workgroup, namespace)).then().statusCode(200).extract().path("workgroup.endpoint.port"); + InetSocketAddress endpoint; try { - assertTrue(isListening(port), "the endpoint must be live once the workgroup is created"); + endpoint = new InetSocketAddress("127.0.0.1", port); + assertTrue(TcpListenerProbe.accepts(endpoint), "the endpoint must be live once the workgroup is created"); } finally { RestAssuredJsonUtils.awsAction("RedshiftServerless", "DeleteWorkgroup", "{\"workgroupName\":\"" + workgroup + "\"}").then().statusCode(200); RestAssuredJsonUtils.awsAction("RedshiftServerless", "DeleteNamespace", "{\"namespaceName\":\"" + namespace + "\"}").then().statusCode(200); } - assertFalse(isListening(port), "the endpoint must be closed once the workgroup is deleted"); + assertFalse(TcpListenerProbe.accepts(endpoint), "the endpoint must be closed once the workgroup is deleted"); } @Test @@ -364,13 +366,4 @@ class RedshiftServerlessRuntimeDockerNightlyIntegrationTest { }); return id; } - - private static boolean isListening(int port) { - try (Socket socket = new Socket()) { - socket.connect(new InetSocketAddress("127.0.0.1", port), 1000); - return true; - } catch (Exception e) { - return false; - } - } } diff --git a/src/test/java/io/floci/aws/testing/TcpListenerProbe.java b/src/test/java/io/floci/aws/testing/TcpListenerProbe.java new file mode 100644 index 0000000000..e28d554ae5 --- /dev/null +++ b/src/test/java/io/floci/aws/testing/TcpListenerProbe.java @@ -0,0 +1,25 @@ +package io.floci.aws.testing; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; + +public final class TcpListenerProbe { + + private TcpListenerProbe() { + } + + /** Tries one connection to a test listener with a one-second timeout. */ + public static boolean accepts(InetSocketAddress endpoint) { + return accepts(endpoint, 1_000); + } + + public static boolean accepts(InetSocketAddress endpoint, int timeoutMillis) { + try (Socket socket = new Socket()) { + socket.connect(endpoint, timeoutMillis); + return true; + } catch (IOException unavailable) { + return false; + } + } +}