-
Notifications
You must be signed in to change notification settings - Fork 3
/
kafka_consumer_db_collection_test.go
299 lines (249 loc) · 9.2 KB
/
kafka_consumer_db_collection_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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package consumer
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/Shopify/sarama"
"github.com/go-test/deep"
"github.com/inviqa/kafka-consumer-go/config"
"github.com/inviqa/kafka-consumer-go/data/failure/model"
"github.com/inviqa/kafka-consumer-go/log"
"github.com/inviqa/kafka-consumer-go/test/saramatest"
)
func TestNewKafkaConsumerDbCollection(t *testing.T) {
deep.CompareUnexportedFields = true
defer func() {
deep.CompareUnexportedFields = false
}()
cfg := &config.Config{}
repo := newMockRetryManager(false)
fch := make(chan model.Failure)
dp := newDatabaseProducer(repo, fch, nil)
hm := HandlerMap{}
scfg := config.NewSaramaConfig(false, false)
logger := log.NullLogger{}
exp := &kafkaConsumerDbCollection{
cfg: cfg,
producer: dp,
retryManager: repo,
handler: newConsumer(fch, cfg, hm, logger),
handlerMap: hm,
saramaCfg: scfg,
logger: logger,
connectToKafka: defaultKafkaConnector,
maintenanceInterval: defaultMaintenanceInterval,
}
got := newKafkaConsumerDbCollection(cfg, dp, repo, fch, hm, scfg, logger, defaultKafkaConnector)
if diff := deep.Equal(exp, got); diff != nil {
t.Error(diff)
}
}
func TestKafkaConsumerDbCollection_Start(t *testing.T) {
defaultDbRetryPollInterval := dbRetryPollInterval
dbRetryPollInterval = time.Millisecond * 25
defer func() {
dbRetryPollInterval = defaultDbRetryPollInterval
}()
exampleMsg := &sarama.ConsumerMessage{
Topic: "product",
Value: []byte(`{"foo":"bar"}`),
}
t.Run("errors when there are no main topics", func(t *testing.T) {
col := kafkaConsumerDbCollection{cfg: &config.Config{}}
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*1)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err == nil {
t.Error("expected an error but got nil")
}
wg.Wait()
})
t.Run("errors when it cannot connect to kafka", func(t *testing.T) {
col, _ := testKafkaConsumerDbCollection(saramatest.NewMockConsumerGroup(), nil, true)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*1)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err == nil {
t.Error("expected an error but got nil")
}
wg.Wait()
})
t.Run("handles errors on kafka consume", func(t *testing.T) {
mcg := saramatest.NewMockConsumerGroup()
mcg.ErrorOnConsume()
col, _ := testKafkaConsumerDbCollection(mcg, nil, false)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
var wg sync.WaitGroup
// errors from consume should be logged, but not returned
if err := col.start(ctx, &wg); err != nil {
t.Errorf("unexpected error: %s", err)
}
wg.Wait()
if !mcg.Consumed() {
t.Error("expected something to have been consumed, but was not")
}
})
t.Run("successful messages are not retried", func(t *testing.T) {
mcg := saramatest.NewMockConsumerGroup()
mcg.AddMessage(exampleMsg)
col, repo := testKafkaConsumerDbCollection(mcg, func(ctx context.Context, msg *sarama.ConsumerMessage) error {
return nil
}, false)
col.setMaintenanceInterval(time.Millisecond * 20)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err != nil {
t.Errorf("unexpected error: %s", err)
}
wg.Wait()
if got := repo.getPublishedFailureCountByTopic("product"); got != 0 {
t.Errorf("expected 0 failures to be produced in database, but got %d", got)
}
if got := repo.runMaintenanceCallCount; got != 2 {
t.Errorf("expected 2 calls to manager.RunMaintenance(), but got %d instead", got)
}
})
t.Run("retries are marked successful when they eventually succeed", func(t *testing.T) {
mcg := saramatest.NewMockConsumerGroup()
mcg.AddMessage(exampleMsg)
var called bool
col, repo := testKafkaConsumerDbCollection(mcg, func(ctx context.Context, msg *sarama.ConsumerMessage) error {
if !called {
called = true
return errors.New("something bad happened")
}
return nil
}, false)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err != nil {
t.Errorf("unexpected error: %s", err)
}
wg.Wait()
if got := repo.getPublishedFailureCountByTopic("product"); got != 1 {
t.Errorf("expected 1 failure to be produced in database, but got %d", got)
}
if !repo.retrySuccessful {
t.Error("expected the DB retry to have been marked as successful, but it wasn't")
}
})
t.Run("retries are marked as errored when they continue to fail", func(t *testing.T) {
mcg := saramatest.NewMockConsumerGroup()
mcg.AddMessage(exampleMsg)
col, repo := testKafkaConsumerDbCollection(mcg, func(ctx context.Context, msg *sarama.ConsumerMessage) error {
return errors.New("something bad happened")
}, false)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err != nil {
t.Errorf("unexpected error: %s", err)
}
wg.Wait()
if got := repo.getPublishedFailureCountByTopic("product"); got != 1 {
t.Errorf("expected 1 failure to be produced in database, but got %d", got)
}
if !repo.retryErrored {
t.Error("expected the DB retry to have been marked as successful, but it wasn't")
}
})
t.Run("handles error from repository when fetching messages for retry", func(t *testing.T) {
mcg := saramatest.NewMockConsumerGroup()
mcg.AddMessage(exampleMsg)
col, repo := testKafkaConsumerDbCollection(mcg, func(ctx context.Context, msg *sarama.ConsumerMessage) error {
return errors.New("something bad happened")
}, false)
repo.willErrorOnGetBatch = true
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err != nil {
t.Errorf("unexpected error: %s", err)
}
wg.Wait()
if got := repo.getPublishedFailureCountByTopic("product"); got != 1 {
t.Errorf("expected 1 failures to be produced in database, but got %d", got)
}
if repo.retryErrored || repo.retrySuccessful {
t.Error("looks like a retry was updated in the DB, but we did not expect it")
}
})
t.Run("handles error from repository when publishing failure", func(t *testing.T) {
mcg := saramatest.NewMockConsumerGroup()
mcg.AddMessage(exampleMsg)
col, repo := testKafkaConsumerDbCollection(mcg, func(ctx context.Context, msg *sarama.ConsumerMessage) error {
return errors.New("something bad happened")
}, false)
repo.willErrorOnPublishFailure = true
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err != nil {
t.Errorf("unexpected error: %s", err)
}
wg.Wait()
if repo.retryErrored || repo.retrySuccessful {
t.Error("looks like a retry was updated in the DB, but we did not expect it")
}
})
t.Run("gracefully handles messages when there is no registered handler in the handler map", func(t *testing.T) {
mcg := saramatest.NewMockConsumerGroup()
mcg.AddMessage(exampleMsg)
col, repo := testKafkaConsumerDbCollection(mcg, func(ctx context.Context, msg *sarama.ConsumerMessage) error {
return errors.New("something bad happened")
}, false)
delete(col.handlerMap, "product")
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
var wg sync.WaitGroup
if err := col.start(ctx, &wg); err != nil {
t.Errorf("unexpected error: %s", err)
}
wg.Wait()
if repo.retryErrored || repo.retrySuccessful {
t.Error("looks like a retry was updated in the DB, but we did not expect it")
}
})
}
func TestKafkaConsumerDbCollection_Close(t *testing.T) {
t.Run("consumers are closed", func(t *testing.T) {
t.Parallel()
mcg1 := saramatest.NewMockConsumerGroup()
col := &kafkaConsumerDbCollection{mainKafkaConsumer: mcg1}
col.close()
if !mcg1.WasClosed() {
t.Error("consumer collection was not closed properly")
}
})
t.Run("gracefully handles errors from closes", func(t *testing.T) {
t.Parallel()
mcg1 := saramatest.NewMockConsumerGroup()
mcg1.ErrorOnClose()
col := &kafkaConsumerDbCollection{mainKafkaConsumer: mcg1, logger: log.NullLogger{}}
col.close()
if col.mainKafkaConsumer != nil {
t.Errorf("consumer collection was not closed properly")
}
})
t.Run("gracefully closes with no main kafka consumer", func(t *testing.T) {
t.Parallel()
col := &kafkaConsumerDbCollection{logger: log.NullLogger{}}
col.close()
if col.mainKafkaConsumer != nil {
t.Errorf("consumer collection was not closed properly")
}
})
}
func testKafkaConsumerDbCollection(mcg *saramatest.MockConsumerGroup, msgHandler Handler, errorOnConnect bool) (*kafkaConsumerDbCollection, *mockRetryManager) {
fch := make(chan model.Failure, 10)
repo := newMockRetryManager(false)
dp := newDatabaseProducer(repo, fch, log.NullLogger{})
hm := HandlerMap{"product": msgHandler}
connector := testKafkaConnector{consumerGroup: mcg, willError: errorOnConnect}
return newKafkaConsumerDbCollection(newTestConfig(), dp, repo, fch, hm, sarama.NewConfig(), log.NullLogger{}, connector.connectToKafka), repo
}