-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathPortUtilsTest.groovy
More file actions
79 lines (61 loc) · 2.1 KB
/
PortUtilsTest.groovy
File metadata and controls
79 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package utils
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.config.inversion.ConfigHelper
import datadog.trace.agent.test.utils.PortUtils
import java.util.concurrent.TimeUnit
class PortUtilsTest extends InstrumentationSpecification {
@Override
protected void configurePreAgent() {
super.configurePreAgent()
// Opt out of strict config validation - test module loads test instrumentations with fake names
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST)
}
def "expect waitForPortToOpen succeed"() {
given:
int port = PortUtils.randomOpenPort()
def socket = new ServerSocket(port) // Emulating port opened.
def process = Mock(Process)
process.isAlive() >> true
when:
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
then:
noExceptionThrown()
cleanup:
socket.close()
}
def "expect to handle port open timeout"() {
given:
int port = PortUtils.randomOpenPort()
def process = Mock(Process)
process.isAlive() >> true
when:
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
then:
def ex = thrown(RuntimeException)
ex.message.startsWith("Timed out waiting for port $port to be opened, started to wait at:")
}
def "expect to handle process abnormal termination"() {
given:
int port = PortUtils.randomOpenPort()
def process = Mock(Process)
process.isAlive() >> false
process.exitValue() >> 123
when:
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
then:
def ex = thrown(RuntimeException)
ex.message == "Process exited abnormally exitCode=123 before port=$port was opened"
}
def "expect to handle process termination before port opened"() {
given:
int port = PortUtils.randomOpenPort()
def process = Mock(Process)
process.isAlive() >> false
process.exitValue() >> 0
when:
PortUtils.waitForPortToOpen(port, 1, TimeUnit.SECONDS, process)
then:
def ex = thrown(RuntimeException)
ex.message == "Process finished before port=$port was opened"
}
}