Skip to content

Commit 9f3cef7

Browse files
Improved test coverage.
1 parent d080318 commit 9f3cef7

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
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)