This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer_test.go
268 lines (232 loc) · 5.99 KB
/
consumer_test.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"context"
"errors"
"github.com/Shopify/sarama"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/signalfx/golib/datapoint"
"github.com/signalfx/golib/event"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"os"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
var testConfig *testingConfig
type testSaramaClient struct {
topics []string
err error
mu sync.Mutex
}
func (t *testSaramaClient) setTopics(topics []string) {
t.mu.Lock()
t.topics = topics
t.mu.Unlock()
}
func (t *testSaramaClient) setError(err error) {
t.mu.Lock()
t.err = err
t.mu.Unlock()
}
func (t *testSaramaClient) Close() error {
return t.err
}
func (t *testSaramaClient) Topics() ([]string, error) {
t.mu.Lock()
defer t.mu.Unlock()
return t.topics, t.err
}
type testCluster struct {
msgs chan *sarama.ConsumerMessage
errs chan error
err error
mu sync.Mutex
consumer *consumer
}
type saramaSession struct {
}
func (s saramaSession) Claims() map[string][]int32 {
panic("not implemented")
}
func (s saramaSession) MemberID() string {
panic("not implemented")
}
func (s saramaSession) GenerationID() int32 {
panic("not implemented")
}
func (s saramaSession) MarkOffset(topic string, partition int32, offset int64, metadata string) {
panic("not implemented")
}
func (s saramaSession) ResetOffset(topic string, partition int32, offset int64, metadata string) {
panic("not implemented")
}
func (s saramaSession) MarkMessage(msg *sarama.ConsumerMessage, metadata string) {
}
func (s saramaSession) Context() context.Context {
panic("not implemented")
}
type saramaClaim struct {
msgs chan *sarama.ConsumerMessage
}
func (s *saramaClaim) Topic() string {
panic("not implemented")
}
func (s *saramaClaim) Partition() int32 {
panic("not implemented")
}
func (s *saramaClaim) InitialOffset() int64 {
panic("not implemented")
}
func (s *saramaClaim) HighWaterMarkOffset() int64 {
panic("not implemented")
}
func (s *saramaClaim) Messages() <-chan *sarama.ConsumerMessage {
return s.msgs
}
func (t *testCluster) Consume(ctx context.Context, topics []string, handler sarama.ConsumerGroupHandler) error {
for {
select {
case <-ctx.Done():
return nil
default:
if err := t.consumer.ConsumeClaim(&saramaSession{}, &saramaClaim{msgs: testConfig.tcluster.msgs}); err != nil {
panic(err)
}
}
}
}
func (t *testCluster) setError(err error) {
t.mu.Lock()
t.err = err
t.mu.Unlock()
}
func (t *testCluster) Close() error {
return t.err
}
func (t *testCluster) Errors() <-chan error {
return t.errs
}
type testParser struct {
p parsers.Parser
err error
mu sync.Mutex
}
func (t *testParser) getError() error {
t.mu.Lock()
defer t.mu.Unlock()
return t.err
}
func (t *testParser) setError(err error) {
t.mu.Lock()
t.err = err
t.mu.Unlock()
}
func (t *testParser) Parse(buf []byte) ([]telegraf.Metric, error) {
if err := t.getError(); err != nil {
return nil, err
}
return t.p.Parse(buf)
}
func (t *testParser) ParseLine(line string) (telegraf.Metric, error) {
if err := t.getError(); err != nil {
return nil, err
}
return t.p.ParseLine(line)
}
func (t *testParser) SetDefaultTags(tags map[string]string) {
}
type testingConfig struct {
config
client *testSaramaClient
tcluster *testCluster
tParser *testParser
}
func getTestConfig(t *testing.T) *testingConfig {
if testConfig == nil {
os.Args = append(os.Args, "-KafkaBroker", "0.0.0.0:0", "-SfxToken", "TOKEN", "-SfxEndpoint", "localhost:-1")
tc, err := getConfig()
testConfig = &testingConfig{
config: *tc,
}
assert.Nil(t, err)
}
testConfig.batchSize = 1
testConfig.metricInterval = time.Millisecond * 10
testConfig.debugServer = "0.0.0.0:0"
testConfig.client = &testSaramaClient{}
testConfig.tcluster = &testCluster{
msgs: make(chan *sarama.ConsumerMessage, 10),
errs: make(chan error, 10),
}
p, _ := parsers.NewInfluxParser()
testConfig.tParser = &testParser{
p: p,
}
instanceConfig = &testConfig.config
instanceConfig.newClientConstructor = func(addrs []string, conf *sarama.Config) (saramaClient, error) {
return testConfig.client, nil
}
instanceConfig.newClusterConstructor = func(addrs []string, groupID string, config *sarama.Config) (sarama.ConsumerGroup, error) {
return testConfig.tcluster, nil
}
instanceConfig.parserConstructor = func() (parsers.Parser, error) {
return testConfig.tParser, nil
}
return testConfig
}
func TestConsumer(t *testing.T) {
Convey("test consumer fail", t, func() {
config := getTestConfig(t)
config.parser = "unknown"
dps := make(chan *datapoint.Datapoint, 10)
evts := make(chan *event.Event, 10)
_, err := newConsumer(context.Background(), &config.config, 0, nil, dps, evts)
So(err, ShouldNotBeNil)
config.client.setError(nil)
config.parser = telegrafParser
})
Convey("test consumer", t, func() {
config := getTestConfig(t)
dps := make(chan *datapoint.Datapoint, 10)
evts := make(chan *event.Event, 10)
c, err := newConsumer(context.Background(), &config.config, 0, []string{"topic"}, dps, evts)
config.tcluster.consumer = c
So(err, ShouldBeNil)
So(c, ShouldNotBeNil)
Convey("test err", func() {
config.tcluster.errs <- errors.New("blarg")
for atomic.LoadInt64(&c.stats.numErrs) == 0 {
runtime.Gosched()
}
})
Convey("test msgs", func() {
config.tcluster.msgs <- getMessage()
for atomic.LoadInt64(&c.stats.numMessages) == 0 {
runtime.Gosched()
}
config.tParser.setError(errors.New("nope"))
config.tcluster.msgs <- getMessage()
for atomic.LoadInt64(&c.stats.numParseErrs) == 0 {
runtime.Gosched()
}
config.tParser.setError(nil)
})
Convey("test datapoints", func() {
So(len(c.Datapoints()), ShouldEqual, 4)
})
Reset(func() {
if err := c.close(); err != nil {
t.Fatal(err)
}
})
})
}
func getMessage() *sarama.ConsumerMessage {
return &sarama.ConsumerMessage{
Value: []byte("metricB,host=myhost4799,env=test val1=8090,val2=8086,val3=32317,val4=30775,val5=19817 1523297127863135218"),
}
}