-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
219 lines (194 loc) · 7.12 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
package kafkauniverse
import (
"context"
"errors"
"strconv"
"testing"
"time"
"github.com/IBM/sarama"
"github.com/cloudtrust/kafka-client/mock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
type contextKey int
const (
ctxKey1 contextKey = iota
ctxKey2 contextKey = iota
)
func createDefaultConsumerConfiguration() KafkaConsumerRepresentation {
return KafkaConsumerRepresentation{
ID: ptrString("id-consumer"),
Enabled: ptrBool(true),
Topic: ptrString("topic"),
ConsumerGroupName: ptrString("consumer-group"),
FailureProducer: ptrString("failure-producer"),
}
}
func TestConsumerSimpleFunctions(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var logger = mock.NewLogger(mockCtrl)
var cluster = &cluster{
logger: logger,
}
var consumerConf = createDefaultConsumerConfiguration()
var consumer, _ = newConsumer(cluster, consumerConf, logger)
var mockConsumerGroup = mock.NewConsumerGroup(mockCtrl)
var mockConsumerGroupSession = mock.NewConsumerGroupSession(mockCtrl)
var anError = errors.New("an error")
logger.EXPECT().Warn(gomock.Any(), gomock.Any()).AnyTimes()
t.Run("Default context initializer", func(t *testing.T) {
var ctx = context.TODO()
assert.Equal(t, ctx, consumer.contextInit(ctx))
})
t.Run("Default handler", func(t *testing.T) {
assert.NotNil(t, consumer.handler(context.TODO(), nil))
})
t.Run("Close", func(t *testing.T) {
t.Run("Not initialized", func(t *testing.T) {
var err = consumer.Close()
assert.Nil(t, err)
})
t.Run("Close fails", func(t *testing.T) {
consumer.initialized = true
consumer.enabled = true
consumer.consumerGroup = mockConsumerGroup
mockConsumerGroup.EXPECT().Close().Return(anError)
var err = consumer.Close()
assert.Equal(t, anError, err)
})
t.Run("SetLogEventRate", func(t *testing.T) {
consumer.SetLogEventRate(100)
assert.Equal(t, int64(100), consumer.logEventRate)
consumer.SetLogEventRate(0) // Invalid values are ignored
assert.Equal(t, int64(100), consumer.logEventRate)
})
})
t.Run("Initialize", func(t *testing.T) {
t.Run("Already initialized", func(t *testing.T) {
var consumer, _ = newConsumer(cluster, consumerConf, logger)
consumer.initialized = true
var err = consumer.initialize()
assert.NotNil(t, err)
})
t.Run("Consumer disabled", func(t *testing.T) {
var consumer, _ = newConsumer(cluster, consumerConf, logger)
consumer.enabled = false
var err = consumer.initialize()
assert.Nil(t, err)
})
t.Run("Enabled by default but incorrectly configured", func(t *testing.T) {
cluster.enabled = true
var consumer, _ = newConsumer(cluster, consumerConf, logger)
var err = consumer.initialize()
assert.NotNil(t, err)
})
t.Run("Success", func(t *testing.T) {
cluster.enabled = false
var consumer, _ = newConsumer(cluster, consumerConf, logger)
var err = consumer.initialize()
assert.Nil(t, err)
})
})
t.Run("Setup", func(t *testing.T) {
var consumer, _ = newConsumer(cluster, consumerConf, logger)
assert.Nil(t, consumer.Setup(mockConsumerGroupSession))
})
t.Run("Cleanup", func(t *testing.T) {
var consumer, _ = newConsumer(cluster, consumerConf, logger)
assert.Nil(t, consumer.Cleanup(mockConsumerGroupSession))
})
}
func fillMessageChannel(messages chan *sarama.ConsumerMessage, values ...string) {
go func() {
for _, value := range values {
messages <- &sarama.ConsumerMessage{
Timestamp: time.Now(),
Value: []byte(value),
}
}
close(messages)
}()
}
func TestConsumeClaim(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var logger = mock.NewLogger(mockCtrl)
var cluster = &cluster{
logger: logger,
}
var consumerConf = createDefaultConsumerConfiguration()
var consumer, _ = newConsumer(cluster, consumerConf, logger)
var mockConsumerGroupSession = mock.NewConsumerGroupSession(mockCtrl)
var mockConsumerGroupClaim = mock.NewConsumerGroupClaim(mockCtrl)
var handlerError = errors.New("error from handler")
logger.EXPECT().Error(gomock.Any(), gomock.Any()).AnyTimes()
logger.EXPECT().Info(gomock.Any(), gomock.Any()).AnyTimes()
t.Run("Empty messages", func(t *testing.T) {
var messages = make(chan *sarama.ConsumerMessage)
fillMessageChannel(messages)
mockConsumerGroupClaim.EXPECT().Messages().Return(messages)
var err = consumer.ConsumeClaim(mockConsumerGroupSession, mockConsumerGroupClaim)
assert.Nil(t, err)
})
t.Run("SetContextInitializer", func(t *testing.T) {
var messages = make(chan *sarama.ConsumerMessage)
fillMessageChannel(messages, "345")
var ctxValue = "abc"
consumer.SetContextInitializer(func(ctx context.Context) context.Context { return context.WithValue(ctx, ctxKey1, ctxValue) })
consumer.SetHandler(func(ctx context.Context, msg KafkaMessage) error {
assert.Equal(t, ctxValue, ctx.Value(ctxKey1))
msg.AbortConsuming()
return handlerError
})
mockConsumerGroupClaim.EXPECT().Messages().Return(messages)
mockConsumerGroupClaim.EXPECT().Topic().Return("topic")
var err = consumer.ConsumeClaim(mockConsumerGroupSession, mockConsumerGroupClaim)
assert.Equal(t, handlerError, err)
})
t.Run("AddContentMapper-Disable autocommit", func(t *testing.T) {
var messages = make(chan *sarama.ConsumerMessage)
var content = 345
fillMessageChannel(messages, strconv.Itoa(content), "invalid")
var handlerError = errors.New("error from handler")
consumer.AddContentMapper(func(ctx context.Context, messageOffset int64, in any) (any, error) {
return strconv.Atoi(string(in.([]byte)))
})
consumer.SetHandler(func(ctx context.Context, msg KafkaMessage) error {
assert.Equal(t, content, msg.GetContent().(int))
return handlerError
})
consumer.SetAutoCommit(true)
mockConsumerGroupClaim.EXPECT().Messages().Return(messages).AnyTimes()
mockConsumerGroupClaim.EXPECT().Topic().Return("topic").AnyTimes()
mockConsumerGroupSession.EXPECT().MarkMessage(gomock.Any(), "").Times(2)
var err = consumer.ConsumeClaim(mockConsumerGroupSession, mockConsumerGroupClaim)
assert.Nil(t, err)
})
}
func TestConsumeClaimWithDelay(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockConsumerGroupSession = mock.NewConsumerGroupSession(mockCtrl)
var mockConsumerGroupClaim = mock.NewConsumerGroupClaim(mockCtrl)
var logger = mock.NewLogger(mockCtrl)
logger.EXPECT().Error(gomock.Any(), gomock.Any()).AnyTimes()
logger.EXPECT().Info(gomock.Any(), gomock.Any()).AnyTimes()
var cluster = &cluster{
logger: logger,
}
delay, _ := time.ParseDuration("1ms")
var consumerConf = createDefaultConsumerConfiguration()
consumerConf.ConsumptionDelay = &delay
var consumer, _ = newConsumer(cluster, consumerConf, logger)
consumer.SetHandler(func(ctx context.Context, msg KafkaMessage) error {
return nil
})
var messages = make(chan *sarama.ConsumerMessage)
fillMessageChannel(messages, "message")
mockConsumerGroupClaim.EXPECT().Messages().Return(messages)
mockConsumerGroupClaim.EXPECT().Topic().Return("topic")
mockConsumerGroupSession.EXPECT().MarkMessage(gomock.Any(), "")
var err = consumer.ConsumeClaim(mockConsumerGroupSession, mockConsumerGroupClaim)
assert.Nil(t, err)
}