-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathTracerFlareTest.groovy
More file actions
104 lines (83 loc) · 2.95 KB
/
TracerFlareTest.groovy
File metadata and controls
104 lines (83 loc) · 2.95 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package datadog.trace.api.flare
import datadog.trace.test.util.DDSpecification
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import static java.nio.charset.StandardCharsets.UTF_8
class TracerFlareTest extends DDSpecification {
// give each mock its own type - we use that to disambiguate reporters
interface Reporter1 extends TracerFlare.Reporter {}
interface Reporter2 extends TracerFlare.Reporter {}
def "test tracer flare"() {
setup:
TracerFlare.addReporter {} // exercises default methods
def textReporter = Mock(Reporter1)
TracerFlare.addReporter(textReporter)
def binaryReporter = Mock(Reporter2)
TracerFlare.addReporter(binaryReporter)
when:
def entries = buildAndExtractZip()
then:
1 * textReporter.prepareForFlare()
1 * binaryReporter.prepareForFlare()
then:
1 * textReporter.addReportToFlare(_) >> { ZipOutputStream zos ->
TracerFlare.addText(zos, "test.txt", "example text")
throw new IllegalStateException("txt (expected)") // should not stop flare
}
1 * binaryReporter.addReportToFlare(_) >> { ZipOutputStream zos ->
TracerFlare.addBinary(zos, "test.bin", [0, 1, 2, 3, 4, 5, 6, 7] as byte[])
throw new IllegalStateException("bin (expected)") // should not stop flare
}
then:
1 * textReporter.cleanupAfterFlare()
1 * binaryReporter.cleanupAfterFlare()
0 * _
and:
entries.size() == 3
entries["test.txt"] == "example text"
entries["test.bin"] == [0, 1, 2, 3, 4, 5, 6, 7] as byte[]
entries["flare_errors.txt"] =~
/^(java.lang.IllegalStateException: (bin|txt) \(expected\)\n){2}$/
}
def "test getReporter finds reporter by class name"() {
setup:
def reporter1 = Mock(Reporter1)
def reporter2 = Mock(Reporter2)
TracerFlare.addReporter(reporter1)
TracerFlare.addReporter(reporter2)
when:
def found1 = TracerFlare.getReporter(reporter1.getClass().getName())
def found2 = TracerFlare.getReporter(reporter2.getClass().getName())
then:
found1 == reporter1
found2 == reporter2
}
def "test getReporter returns null for non-existent reporter"() {
setup:
def reporter = Mock(Reporter1)
TracerFlare.addReporter(reporter)
when:
def found = TracerFlare.getReporter("com.example.NonExistentReporter")
then:
found == null
}
def buildAndExtractZip() {
TracerFlare.prepareForFlare()
def out = new ByteArrayOutputStream()
try (ZipOutputStream zip = new ZipOutputStream(out)) {
TracerFlare.addReportsToFlare(zip)
} finally {
TracerFlare.cleanupAfterFlare()
}
def entries = [:]
def zip = new ZipInputStream(new ByteArrayInputStream(out.toByteArray()))
def entry
while (entry = zip.nextEntry) {
def bytes = new ByteArrayOutputStream()
bytes << zip
entries.put(entry.name, entry.name.endsWith(".bin")
? bytes.toByteArray() : new String(bytes.toByteArray(), UTF_8))
}
return entries
}
}