Skip to content

Commit 297624e

Browse files
committed
Bind test servers to loopback address
Binding to 0.0.0.0 is not guaranteed to result in a socket that is reachable via the loopback address, even when requesting an ephemeral port assignment by binding to port 0. It is possible for another process on the system to bind to the loopback address at that same port, and then client requests will go to that process instead of the test server, resulting in occasional cryptic test failures. By instead binding to the loopback address specifically, rather than the wildcard address, we can prevent binding to a port that is already in use. This significantly improves the reliability of the test suite.
1 parent 7746feb commit 297624e

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/TestValidateAfterInactivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.junit.jupiter.api.Test;
4848

4949
import java.io.IOException;
50+
import java.net.InetAddress;
5051
import java.net.InetSocketAddress;
5152
import java.net.SocketException;
5253
import java.net.StandardSocketOptions;
@@ -78,7 +79,7 @@ class AbstractTestValidateAfterInactivity {
7879

7980
@BeforeEach
8081
void setup() throws Exception {
81-
serverSocket = ServerSocketChannel.open().bind(new InetSocketAddress(0));
82+
serverSocket = ServerSocketChannel.open().bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
8283
port = ((InetSocketAddress) serverSocket.getLocalAddress()).getPort();
8384

8485
serverThread = new Thread(this::runServer);

httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientEarlyHintsEndToEnd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package org.apache.hc.client5.http.examples;
2828

2929
import java.io.IOException;
30+
import java.net.InetAddress;
3031
import java.net.InetSocketAddress;
3132
import java.nio.ByteBuffer;
3233
import java.nio.charset.StandardCharsets;
@@ -142,7 +143,7 @@ public void releaseResources() { /* no-op for demo */ }
142143
})
143144
.create();
144145
server.start();
145-
final Future<ListenerEndpoint> lf = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
146+
final Future<ListenerEndpoint> lf = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP);
146147
final int port = ((InetSocketAddress) lf.get().getAddress()).getPort();
147148

148149
// --- Async client with Early Hints listener

httpclient5/src/test/java/org/apache/hc/client5/http/impl/async/EarlyHintsAsyncExecTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.junit.jupiter.api.Assertions.assertTrue;
3131

3232
import java.io.IOException;
33+
import java.net.InetAddress;
3334
import java.net.InetSocketAddress;
3435
import java.nio.ByteBuffer;
3536
import java.nio.charset.StandardCharsets;
@@ -156,7 +157,7 @@ public void releaseResources() { /* no-op for test */ }
156157
server.start();
157158

158159
// Bind to ephemeral port and retrieve it from the listener endpoint
159-
final Future<ListenerEndpoint> lf = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
160+
final Future<ListenerEndpoint> lf = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP);
160161
final ListenerEndpoint ep = lf.get(5, TimeUnit.SECONDS);
161162
final int port = ((InetSocketAddress) ep.getAddress()).getPort();
162163

0 commit comments

Comments
 (0)