Skip to content

Commit

Permalink
test: common: Improve binding of temporary address for Inet sockets
Browse files Browse the repository at this point in the history
Retry if the port just became unavailable for whatever reason.
  • Loading branch information
kohlschuetter committed Sep 13, 2024
1 parent 7b67cb3 commit 40e9492
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public static SocketAddress wildcardBindAddress() throws IOException {

@Override
public SocketAddress newTempAddress() throws IOException {
return wildcardBindAddress();
SocketAddress bindAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
try (ServerSocket sock = new ServerSocket()) {
sock.bind(bindAddr);
return sock.getLocalSocketAddress();
} catch (BindException e) {
throw new TestAbortedException("Cannot bind to " + bindAddr, e);
}
}

@Override
Expand Down

0 comments on commit 40e9492

Please sign in to comment.