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

Calling FreePorts.anyInterfacePort() four times in a row can return the
same port twice. Vert.x then shares that port between two NetServers of
one instance and round-robins connections, so a plain probe could reach
the TLS marker backend. The marker backends now bind port 0 and report
their actual port, and the two proxy ports come from a new
FreePorts.anyInterfacePorts(n), which holds every socket open until all
ports are chosen. The IoT broker tests use the helper instead of a
hand-written dedupe loop, and FreePortsTest covers it.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-15 09:21:18 -04:00
1 parent 77384dc3de
commit 0c41806cd9
5 files changed
+94 -17

No files matched your search

@@ -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 {
@@ -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");
@@ -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");
@@ -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;
}
}
}
@@ -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));
}
}