Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

test(config): allocate distinct ports in TlsProxyServerTest (#5707)

Kenshin Okinaka0c41806cd99c
diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerTest.java
index 8ed748ad39..8cd6bf2617 100644
--- a/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/config/TlsProxyServerTest.java
@@ -133,14 +133,14 @@ class TlsProxyServerTest {
@Test
@Timeout(20)
void bindsBothPorts_andRoutesByFirstByte() throws Exception {
- int httpBe = FreePorts.anyInterfacePort();
- int httpsBe = FreePorts.anyInterfacePort();
- int publicPort = FreePorts.anyInterfacePort();
- int awsHttpsPort = FreePorts.anyInterfacePort();
- httpBackend = startMarkerBackend(httpBe, "PLAIN");
- httpsBackend = startMarkerBackend(httpsBe, "TLS");
+ httpBackend = startMarkerBackend("PLAIN");
+ httpsBackend = startMarkerBackend("TLS");
+ int[] ports = FreePorts.anyInterfacePorts(2);
+ int publicPort = ports[0];
+ int awsHttpsPort = ports[1];
- proxy = new TlsProxyServer(vertx, configWith(true, publicPort, awsHttpsPort), "127.0.0.1", httpBe, httpsBe);
+ proxy = new TlsProxyServer(vertx, configWith(true, publicPort, awsHttpsPort), "127.0.0.1",
+ httpBackend.actualPort(), httpsBackend.actualPort());
// Public port: TLS ClientHello → HTTPS backend; anything else → HTTP backend.
assertEquals("TLS", roundTrip(publicPort, TLS_HANDSHAKE));
@@ -160,11 +160,11 @@ class TlsProxyServerTest {
// ---- helpers ----
- private NetServer startMarkerBackend(int port, String marker) throws Exception {
+ private NetServer startMarkerBackend(String marker) throws Exception {
NetServer server = vertx.createNetServer();
server.connectHandler(sock -> sock.write(marker));
CompletableFuture<Void> started = new CompletableFuture<>();
- server.listen(port, "127.0.0.1").onComplete(ar -> {
+ server.listen(0, "127.0.0.1").onComplete(ar -> {
if (ar.succeeded()) {
started.complete(null);
} else {
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java
index 71028f7b9e..de2ece556c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerDeviceVerificationTest.java
@@ -93,10 +93,9 @@ class IotMqttBrokerDeviceVerificationTest {
@BeforeEach
void brokerWithTlsOn() throws IOException {
- plainPort = FreePorts.anyInterfacePort();
- do {
- tlsPort = FreePorts.anyInterfacePort();
- } while (tlsPort == plainPort);
+ int[] ports = FreePorts.anyInterfacePorts(2);
+ plainPort = ports[0];
+ tlsPort = ports[1];
when(config.services().iot().enabled()).thenReturn(true);
when(config.services().iot().mqtt().enabled()).thenReturn(true);
when(config.services().iot().mqtt().host()).thenReturn("127.0.0.1");
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
index 037850daf6..1aedc3da97 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
@@ -155,10 +155,9 @@ class IotMqttBrokerServiceTest {
@BeforeEach
void brokerWithTlsOn() throws IOException {
- plainPort = FreePorts.anyInterfacePort();
- do {
- tlsPort = FreePorts.anyInterfacePort();
- } while (tlsPort == plainPort);
+ int[] ports = FreePorts.anyInterfacePorts(2);
+ plainPort = ports[0];
+ tlsPort = ports[1];
when(config.services().iot().enabled()).thenReturn(true);
when(config.services().iot().mqtt().enabled()).thenReturn(true);
when(config.services().iot().mqtt().host()).thenReturn("127.0.0.1");
diff --git a/src/test/java/io/github/hectorvent/floci/testutil/FreePorts.java b/src/test/java/io/github/hectorvent/floci/testutil/FreePorts.java
index 27ea8f4bc3..3479a636f4 100644
--- a/src/test/java/io/github/hectorvent/floci/testutil/FreePorts.java
+++ b/src/test/java/io/github/hectorvent/floci/testutil/FreePorts.java
@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.testutil;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
+import java.util.ArrayList;
+import java.util.List;
/** Helpers for choosing ports in tests that bind their own servers. */
public final class FreePorts {
@@ -17,6 +19,31 @@ public final class FreePorts {
}
}
+ /**
+ * Returns {@code count} distinct ports selected by binding on any local interface.
+ * Every socket stays open until all ports are chosen, so no port is returned twice. Calling
+ * {@link #anyInterfacePort()} several times in a row can return the same port twice, and a
+ * Vert.x instance then silently shares that port between its servers instead of failing the
+ * second bind.
+ */
+ public static int[] anyInterfacePorts(int count) throws IOException {
+ if (count < 1) {
+ throw new IllegalArgumentException("count must be at least 1, was " + count);
+ }
+ List<ServerSocket> sockets = new ArrayList<>();
+ try {
+ int[] ports = new int[count];
+ for (int i = 0; i < count; i++) {
+ ServerSocket socket = new ServerSocket(0);
+ sockets.add(socket);
+ ports[i] = socket.getLocalPort();
+ }
+ return ports;
+ } finally {
+ closeAll(sockets);
+ }
+ }
+
/**
* Returns a port selected by binding to IPv4 loopback, for host-side Docker bindings.
* Docker Desktop needs this host port named explicitly: a daemon-picked host port is allocated
@@ -27,4 +54,22 @@ public final class FreePorts {
return socket.getLocalPort();
}
}
+
+ private static void closeAll(List<ServerSocket> sockets) throws IOException {
+ IOException failure = null;
+ for (ServerSocket socket : sockets) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ if (failure == null) {
+ failure = e;
+ } else {
+ failure.addSuppressed(e);
+ }
+ }
+ }
+ if (failure != null) {
+ throw failure;
+ }
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/testutil/FreePortsTest.java b/src/test/java/io/github/hectorvent/floci/testutil/FreePortsTest.java
new file mode 100644
index 0000000000..012d3e4a79
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/testutil/FreePortsTest.java
@@ -0,0 +1,34 @@
+package io.github.hectorvent.floci.testutil;
+
+import org.junit.jupiter.api.Test;
+
+import java.net.ServerSocket;
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class FreePortsTest {
+
+ @Test
+ void anyInterfacePorts_returnsDistinctPorts() throws Exception {
+ int[] ports = FreePorts.anyInterfacePorts(32);
+
+ assertEquals(32, ports.length);
+ assertEquals(32, Arrays.stream(ports).distinct().count());
+ }
+
+ @Test
+ void anyInterfacePorts_releasesEveryPort() throws Exception {
+ for (int port : FreePorts.anyInterfacePorts(4)) {
+ try (ServerSocket socket = new ServerSocket(port)) {
+ assertEquals(port, socket.getLocalPort());
+ }
+ }
+ }
+
+ @Test
+ void anyInterfacePorts_rejectsCountBelowOne() {
+ assertThrows(IllegalArgumentException.class, () -> FreePorts.anyInterfacePorts(0));
+ }
+}