Skip to content

Commit 10fbab9

Browse files
Added time to error message when port open timed out. (#9287)
1 parent ee6947e commit 10fbab9

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ private static boolean isPortOpen(String host, int port) {
105105

106106
public static void waitForPortToOpen(
107107
final int port, final long timeout, final TimeUnit unit, final Process process) {
108-
final long waitUntil = System.currentTimeMillis() + unit.toMillis(timeout);
108+
final long startedAt = System.currentTimeMillis();
109+
final long waitUntil = startedAt + unit.toMillis(timeout);
109110

110111
while (System.currentTimeMillis() < waitUntil) {
111112
try {
@@ -133,7 +134,13 @@ public static void waitForPortToOpen(
133134
}
134135
}
135136

136-
throw new RuntimeException("Timed out waiting for port " + port + " to be opened");
137+
throw new RuntimeException(
138+
"Timed out waiting for port "
139+
+ port
140+
+ " to be opened, started to wait at: "
141+
+ startedAt
142+
+ ", timed out at: "
143+
+ System.currentTimeMillis());
137144
}
138145

139146
public static void waitForPortToOpen(String host, int port, long timeout, TimeUnit unit) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package utils
2+
3+
import datadog.trace.agent.test.AgentTestRunner
4+
import datadog.trace.agent.test.utils.PortUtils
5+
6+
import java.util.concurrent.TimeUnit
7+
8+
class PortUtilsTest extends AgentTestRunner {
9+
def "expect waitForPortToOpen succeed"() {
10+
given:
11+
int port = PortUtils.randomOpenPort()
12+
def socket = new ServerSocket(port) // Emulating port opened.
13+
14+
def process = Mock(Process)
15+
process.isAlive() >> true
16+
17+
when:
18+
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
19+
20+
then:
21+
noExceptionThrown()
22+
23+
cleanup:
24+
socket.close()
25+
}
26+
27+
def "expect to handle port open timeout"() {
28+
given:
29+
int port = PortUtils.randomOpenPort()
30+
def process = Mock(Process)
31+
process.isAlive() >> true
32+
33+
when:
34+
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
35+
36+
then:
37+
def ex = thrown(RuntimeException)
38+
ex.message.startsWith("Timed out waiting for port $port to be opened, started to wait at:")
39+
}
40+
41+
def "expect to handle process abnormal termination"() {
42+
given:
43+
int port = PortUtils.randomOpenPort()
44+
def process = Mock(Process)
45+
process.isAlive() >> false
46+
process.exitValue() >> 123
47+
48+
when:
49+
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
50+
51+
then:
52+
def ex = thrown(RuntimeException)
53+
ex.message == "Process exited abnormally exitCode=123 before port=$port was opened"
54+
}
55+
56+
def "expect to handle process termination before port opened"() {
57+
given:
58+
int port = PortUtils.randomOpenPort()
59+
def process = Mock(Process)
60+
process.isAlive() >> false
61+
process.exitValue() >> 0
62+
63+
when:
64+
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
65+
66+
then:
67+
def ex = thrown(RuntimeException)
68+
ex.message == "Process finished before port=$port was opened"
69+
}
70+
}

0 commit comments

Comments
 (0)