@@ -7,13 +7,11 @@ package writer
7
7
8
8
import (
9
9
"compress/gzip"
10
- "fmt"
11
10
"io"
12
11
"runtime"
13
12
"sync"
14
13
"testing"
15
14
16
- "github.com/DataDog/zstd"
17
15
"github.com/stretchr/testify/assert"
18
16
"github.com/stretchr/testify/require"
19
17
"google.golang.org/protobuf/proto"
@@ -44,45 +42,38 @@ func (s MockSampler) GetTargetTPS() float64 {
44
42
var mockSampler = MockSampler {TargetTPS : 5 , Enabled : true }
45
43
46
44
func TestTraceWriter (t * testing.T ) {
47
- testCases := []bool {false , true }
45
+ srv := newTestServer ()
46
+ cfg := & config.AgentConfig {
47
+ Hostname : testHostname ,
48
+ DefaultEnv : testEnv ,
49
+ Endpoints : []* config.Endpoint {{
50
+ APIKey : "123" ,
51
+ Host : srv .URL ,
52
+ }},
53
+ TraceWriter : & config.WriterConfig {ConnectionLimit : 200 , QueueSize : 40 },
54
+ }
48
55
49
- for _ , tc := range testCases {
50
- srv := newTestServer ()
51
- cfg := & config.AgentConfig {
52
- Hostname : testHostname ,
53
- DefaultEnv : testEnv ,
54
- Endpoints : []* config.Endpoint {{
55
- APIKey : "123" ,
56
- Host : srv .URL ,
57
- }},
58
- TraceWriter : & config.WriterConfig {ConnectionLimit : 200 , QueueSize : 40 },
56
+ t .Run ("ok" , func (t * testing.T ) {
57
+ testSpans := []* SampledChunks {
58
+ randomSampledSpans (20 , 8 ),
59
+ randomSampledSpans (10 , 0 ),
60
+ randomSampledSpans (40 , 5 ),
59
61
}
60
- if tc {
61
- cfg .Features = map [string ]struct {}{"zstd-encoding" : {}}
62
+ // Use a flush threshold that allows the first two entries to not overflow,
63
+ // but overflow on the third.
64
+ defer useFlushThreshold (testSpans [0 ].Size + testSpans [1 ].Size + 10 )()
65
+ tw := NewTraceWriter (cfg , mockSampler , mockSampler , mockSampler , telemetry .NewNoopCollector (), & statsd.NoOpClient {}, & timing.NoopReporter {})
66
+ tw .In = make (chan * SampledChunks )
67
+ go tw .Run ()
68
+ for _ , ss := range testSpans {
69
+ tw .In <- ss
62
70
}
63
-
64
- t .Run (fmt .Sprintf ("zstd_%t" , tc ), func (t * testing.T ) {
65
- testSpans := []* SampledChunks {
66
- randomSampledSpans (20 , 8 ),
67
- randomSampledSpans (10 , 0 ),
68
- randomSampledSpans (40 , 5 ),
69
- }
70
- // Use a flush threshold that allows the first two entries to not overflow,
71
- // but overflow on the third.
72
- defer useFlushThreshold (testSpans [0 ].Size + testSpans [1 ].Size + 10 )()
73
- tw := NewTraceWriter (cfg , mockSampler , mockSampler , mockSampler , telemetry .NewNoopCollector (), & statsd.NoOpClient {}, & timing.NoopReporter {})
74
- tw .In = make (chan * SampledChunks )
75
- go tw .Run ()
76
- for _ , ss := range testSpans {
77
- tw .In <- ss
78
- }
79
- tw .Stop ()
80
- // One payload flushes due to overflowing the threshold, and the second one
81
- // because of stop.
82
- assert .Equal (t , 2 , srv .Accepted ())
83
- payloadsContain (t , srv .Payloads (), testSpans , tc )
84
- })
85
- }
71
+ tw .Stop ()
72
+ // One payload flushes due to overflowing the threshold, and the second one
73
+ // because of stop.
74
+ assert .Equal (t , 2 , srv .Accepted ())
75
+ payloadsContain (t , srv .Payloads (), testSpans )
76
+ })
86
77
}
87
78
88
79
func TestTraceWriterMultipleEndpointsConcurrent (t * testing.T ) {
@@ -131,7 +122,7 @@ func TestTraceWriterMultipleEndpointsConcurrent(t *testing.T) {
131
122
132
123
wg .Wait ()
133
124
tw .Stop ()
134
- payloadsContain (t , srv .Payloads (), testSpans , false )
125
+ payloadsContain (t , srv .Payloads (), testSpans )
135
126
}
136
127
137
128
// useFlushThreshold sets n as the number of bytes to be used as the flush threshold
@@ -154,25 +145,14 @@ func randomSampledSpans(spans, events int) *SampledChunks {
154
145
}
155
146
156
147
// payloadsContain checks that the given payloads contain the given set of sampled spans.
157
- func payloadsContain (t * testing.T , payloads []* payload , sampledSpans []* SampledChunks , shouldUseZstd bool ) {
148
+ func payloadsContain (t * testing.T , payloads []* payload , sampledSpans []* SampledChunks ) {
158
149
t .Helper ()
159
150
var all pb.AgentPayload
160
151
for _ , p := range payloads {
161
152
assert := assert .New (t )
162
- var slurp []byte
163
- var err error
164
- var reader io.ReadCloser
165
-
166
- if shouldUseZstd {
167
- reader = zstd .NewReader (p .body )
168
- assert .NotNil (reader )
169
- } else {
170
- reader , err = gzip .NewReader (p .body )
171
- assert .NoError (err )
172
- }
173
-
174
- slurp , err = io .ReadAll (reader )
175
-
153
+ gzipr , err := gzip .NewReader (p .body )
154
+ assert .NoError (err )
155
+ slurp , err := io .ReadAll (gzipr )
176
156
assert .NoError (err )
177
157
var payload pb.AgentPayload
178
158
err = proto .Unmarshal (slurp , & payload )
@@ -227,7 +207,7 @@ func TestTraceWriterFlushSync(t *testing.T) {
227
207
tw .FlushSync ()
228
208
// Now all trace payloads should be sent
229
209
assert .Equal (t , 1 , srv .Accepted ())
230
- payloadsContain (t , srv .Payloads (), testSpans , false )
210
+ payloadsContain (t , srv .Payloads (), testSpans )
231
211
})
232
212
}
233
213
@@ -296,7 +276,7 @@ func TestTraceWriterSyncStop(t *testing.T) {
296
276
tw .Stop ()
297
277
// Now all trace payloads should be sent
298
278
assert .Equal (t , 1 , srv .Accepted ())
299
- payloadsContain (t , srv .Payloads (), testSpans , false )
279
+ payloadsContain (t , srv .Payloads (), testSpans )
300
280
})
301
281
}
302
282
0 commit comments