Skip to content

Commit 1bd6a0a

Browse files
Make isPortOpen() to have predictable timeout. (#10832)
Make `isPortOpen()` to have predictable timeout. Merge branch 'master' into alexeyk/fix-port-utils Merge branch 'master' into alexeyk/fix-port-utils Co-authored-by: alexey.kuznetsov <alexey.kuznetsov@datadoghq.com>
1 parent 76fb55f commit 1bd6a0a

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

  • dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/utils

dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/utils/PortUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package datadog.trace.agent.test.utils;
22

33
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.InetSocketAddress;
46
import java.net.ServerSocket;
57
import java.net.Socket;
68
import java.util.concurrent.TimeUnit;
@@ -9,6 +11,7 @@
911
public class PortUtils {
1012

1113
public static int UNUSABLE_PORT = 61;
14+
private static final int PORT_CONNECT_TIMEOUT_MS = 1000;
1215

1316
private static final int FREE_PORT_RANGE_START = 20000;
1417
private static final int FREE_PORT_RANGE_END = 40000;
@@ -96,7 +99,12 @@ private static boolean isPortOpen(final int port) {
9699
}
97100

98101
private static boolean isPortOpen(String host, int port) {
99-
try (final Socket socket = new Socket(host, port)) {
102+
try (final Socket socket = new Socket()) {
103+
InetSocketAddress address =
104+
host == null
105+
? new InetSocketAddress(InetAddress.getLoopbackAddress(), port)
106+
: new InetSocketAddress(host, port);
107+
socket.connect(address, PORT_CONNECT_TIMEOUT_MS);
100108
return true;
101109
} catch (final IOException e) {
102110
return false;

0 commit comments

Comments
 (0)