5
5
"encoding/json"
6
6
"fmt"
7
7
"io"
8
+ "io/ioutil"
8
9
"log"
9
- "net"
10
10
"net/http"
11
11
"net/http/httptest"
12
12
"os"
@@ -22,42 +22,6 @@ import (
22
22
. "github.com/onsi/gomega"
23
23
)
24
24
25
- // FakeCollector implements a fake trace collector that will
26
- // listen on an endpoint untill a trace is received and then will
27
- // return that parsed trace
28
- type FakeCollector struct {
29
- Endpoint string
30
- }
31
-
32
- // Listen on the endpoint for one trace and push it to outChannel
33
- func (fc * FakeCollector ) Listen (outChannel chan * protocol.Trace ) {
34
- ln , err := net .Listen ("tcp" , fc .Endpoint )
35
- if err != nil {
36
- outChannel <- nil
37
- return
38
- }
39
- defer ln .Close ()
40
- conn , err := ln .Accept ()
41
- if err != nil {
42
- outChannel <- nil
43
- return
44
- }
45
- defer conn .Close ()
46
- var buf = make ([]byte , 0 )
47
- _ , err = conn .Read (buf )
48
- if err != nil {
49
- outChannel <- nil
50
- return
51
- }
52
- var receivedTrace protocol.Trace
53
- err = json .Unmarshal (buf , & receivedTrace )
54
- if err != nil {
55
- outChannel <- nil
56
- return
57
- }
58
- outChannel <- & receivedTrace
59
- }
60
-
61
25
func TestEpsagonTracer (t * testing.T ) {
62
26
RegisterFailHandler (Fail )
63
27
RunSpecs (t , "Epsagon Core Suite" )
@@ -75,8 +39,10 @@ var _ = Describe("epsagonTracer suite", func() {
75
39
})
76
40
77
41
func runWithTracer (endpoint string , operations func ()) {
42
+ tracer .GlobalTracer = nil
78
43
tracer .CreateGlobalTracer (& tracer.Config {
79
44
CollectorURL : endpoint ,
45
+ Token : "1" ,
80
46
})
81
47
tracer .GlobalTracer .Start ()
82
48
defer tracer .StopGlobalTracer ()
@@ -85,20 +51,36 @@ func runWithTracer(endpoint string, operations func()) {
85
51
86
52
// testWithTracer runs a test with
87
53
func testWithTracer (timeout * time.Duration , operations func ()) * protocol.Trace {
88
- endpoint := "127.0.0.1:54769"
89
54
traceChannel := make (chan * protocol.Trace )
90
- fc := FakeCollector {Endpoint : endpoint }
91
- go fc .Listen (traceChannel )
92
- go runWithTracer ("http://" + endpoint , operations )
55
+ fakeCollectorServer := httptest .NewServer (http .HandlerFunc (
56
+ func (res http.ResponseWriter , req * http.Request ) {
57
+ buf , err := ioutil .ReadAll (req .Body )
58
+ if err != nil {
59
+ traceChannel <- nil
60
+ return
61
+ }
62
+ var receivedTrace protocol.Trace
63
+ err = json .Unmarshal (buf , & receivedTrace )
64
+ if err != nil {
65
+ traceChannel <- nil
66
+ return
67
+ }
68
+ traceChannel <- & receivedTrace
69
+ res .Write ([]byte ("" ))
70
+ },
71
+ ))
72
+ go runWithTracer (fakeCollectorServer .URL , operations )
93
73
if timeout == nil {
94
74
defaultTimeout := time .Second * 10
95
75
timeout = & defaultTimeout
96
76
}
97
77
timer := time .NewTimer (* timeout )
98
78
select {
99
79
case <- timer .C :
80
+ fakeCollectorServer .Close ()
100
81
return nil
101
82
case trace := <- traceChannel :
83
+ fakeCollectorServer .Close ()
102
84
return trace
103
85
}
104
86
}
@@ -175,22 +157,34 @@ func Test_handleSendTracesResponse(t *testing.T) {
175
157
}
176
158
177
159
func Test_AddLabel_sanity (t * testing.T ) {
178
- defaultTimeout := time .Second * 100
160
+ defaultTimeout := time .Second * 5
179
161
timeout := & defaultTimeout
180
162
trace := testWithTracer (timeout , func () { epsagon .Label ("test_key" , "test_value" ) })
181
- println (trace )
163
+ Expect (trace ). ToNot ( BeNil () )
182
164
}
183
165
184
166
func Test_AddError_sanity (t * testing.T ) {
185
- defaultTimeout := time .Second * 100
167
+ defaultTimeout := time .Second * 5
186
168
timeout := & defaultTimeout
187
169
trace := testWithTracer (timeout , func () { epsagon .Error ("some error" ) })
188
- println (trace )
170
+ Expect (trace ). ToNot ( BeNil () )
189
171
}
190
172
191
173
func Test_AddTypeError (t * testing.T ) {
192
- defaultTimeout := time .Second * 100
174
+ defaultTimeout := time .Second * 5
193
175
timeout := & defaultTimeout
194
176
trace := testWithTracer (timeout , func () { epsagon .TypeError ("some error" , "test error type" ) })
195
- println (trace )
177
+ Expect (trace ).ToNot (BeNil ())
178
+ }
179
+
180
+ func Test_MaxTraceSize_sanity (t * testing.T ) {
181
+ defer os .Unsetenv (tracer .MaxTraceSizeEnvVar )
182
+ os .Setenv (tracer .MaxTraceSizeEnvVar , "2048" )
183
+ defaultTimeout := time .Second * 5
184
+ timeout := & defaultTimeout
185
+ trace := testWithTracer (timeout , func () { epsagon .Label ("1" , "2" ) })
186
+ Expect (trace ).ToNot (BeNil ())
187
+ os .Setenv (tracer .MaxTraceSizeEnvVar , "64" )
188
+ trace = testWithTracer (timeout , func () { epsagon .Label ("1" , "2" ) })
189
+ Expect (trace ).To (BeNil ())
196
190
}
0 commit comments