Skip to content

Commit 9283aca

Browse files
committed
fix(test): use ServerSocket bind to check port availability
The previous check used client-side Socket.connect(), which cannot detect ports in TCP TIME_WAIT state. This caused flaky test failures when gRPC server bind failed on seemingly available ports.
1 parent 7f71ab1 commit 9283aca

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

framework/src/test/java/org/tron/common/utils/PublicMethod.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,11 @@ public static int chooseRandomPort(int min, int max) {
343343
}
344344

345345
private static boolean checkPortAvailable(int port) throws IOException {
346-
InetAddress theAddress = InetAddress.getByName("127.0.0.1");
347-
try (Socket socket = new Socket(theAddress, port)) {
348-
// only check
349-
socket.getPort();
350-
} catch (IOException e) {
346+
try (java.net.ServerSocket ss = new java.net.ServerSocket(port)) {
347+
ss.setReuseAddress(true);
351348
return true;
349+
} catch (IOException e) {
350+
return false;
352351
}
353-
return false;
354352
}
355353
}

0 commit comments

Comments
 (0)