This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
consumerBuilder_test.go
231 lines (219 loc) · 7.16 KB
/
consumerBuilder_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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package kafkaclient
import (
"sort"
"testing"
"time"
"github.com/Shopify/sarama"
"github.com/bsm/sarama-cluster"
"github.com/stretchr/testify/suite"
"github.com/uber-go/kafka-client/internal/consumer"
"github.com/uber-go/kafka-client/kafka"
"github.com/uber-go/tally"
"go.uber.org/zap"
)
type (
ConsumerBuilderTestSuite struct {
suite.Suite
builder *consumerBuilder
config *kafka.ConsumerConfig
resolver kafka.NameResolver
mockSaramaClientConstructor *mockSaramaClientConstructor
mockSaramaConsumerConstructor *mockSaramaConsumerConstructor
mockSaramaProducerConstructor *mockSaramaProducerConstructor
}
mockSaramaConsumerConstructor struct {
clusterTopicMap map[string][]string
}
mockSaramaClientConstructor struct {
clusters map[string]bool
}
mockSaramaProducerConstructor struct{}
)
func TestConsumerBuilderTestSuite(t *testing.T) {
suite.Run(t, new(ConsumerBuilderTestSuite))
}
func (s *ConsumerBuilderTestSuite) SetupTest() {
s.config = kafka.NewConsumerConfig("consumergroup", []kafka.ConsumerTopic{
{
Topic: kafka.Topic{
Name: "topic",
Cluster: "cluster",
},
RetryQ: kafka.Topic{
Name: "retry-topic",
Cluster: "dlq-cluster",
Delay: time.Microsecond,
},
DLQ: kafka.Topic{
Name: "dlq-topic",
Cluster: "dlq-cluster",
},
MaxRetries: 1,
},
{
Topic: kafka.Topic{
Name: "topic1",
Cluster: "cluster",
},
RetryQ: kafka.Topic{},
DLQ: kafka.Topic{},
MaxRetries: 1,
},
})
s.resolver = kafka.NewStaticNameResolver(
map[string][]string{
"topic": {"cluster"},
"retry-topic": {"dlq-cluster"},
"dlq-topic": {"dlq-cluster"},
},
map[string][]string{
"cluster": {"broker1:9092"},
"dlq-cluster": {"broker2:9092"},
},
)
s.builder = newConsumerBuilder(
s.config,
s.resolver,
tally.NoopScope,
zap.NewNop(),
WithClientID("test-id"),
)
s.mockSaramaConsumerConstructor = &mockSaramaConsumerConstructor{
clusterTopicMap: make(map[string][]string),
}
s.mockSaramaClientConstructor = &mockSaramaClientConstructor{
clusters: make(map[string]bool),
}
s.mockSaramaProducerConstructor = &mockSaramaProducerConstructor{}
s.builder.constructors = consumer.Constructors{
NewSaramaProducer: s.mockSaramaProducerConstructor.f,
NewSaramaConsumer: s.mockSaramaConsumerConstructor.f,
NewSaramaClient: s.mockSaramaClientConstructor.f,
}
}
func (s *ConsumerBuilderTestSuite) TestBuild() {
kafkaConsumer, err := s.builder.build()
s.NoError(err)
s.NotNil(kafkaConsumer)
// 3 clusters used in consumer
s.Equal([]string{"cluster", "dlq-cluster", "dlq-cluster"}, func() []string {
output := make([]string, 0, 3)
for cluster := range s.builder.clusterTopicsMap {
output = append(output, cluster.name)
}
sort.Strings(output)
return output
}())
// 3 consumer group names: 2 default for original and retryQ consumer and one dlq-merger
s.Equal([]string{"consumergroup", "consumergroup", "consumergroup-dlq-merger"}, func() []string {
output := make([]string, 0, 3)
for cluster := range s.builder.clusterTopicsMap {
output = append(output, cluster.groupName)
}
sort.Strings(output)
return output
}())
// 2 sarama clients corresponding DLQ producers for 2 clusters. Group name is unused.
s.Equal([]string{"dlq-cluster"}, func() []string {
output := make([]string, 0, 2)
for cluster := range s.builder.clusterSaramaClientMap {
output = append(output, cluster.Cluster)
}
sort.Strings(output)
return output
}())
// consuming from 3 clusters
s.Equal([]string{"cluster", "dlq-cluster", "dlq-cluster"}, func() []string {
output := make([]string, 0, 3)
for cluster := range s.builder.clusterSaramaConsumerMap {
output = append(output, cluster.Cluster)
}
sort.Strings(output)
return output
}())
// consuming from 2 consumer group names
s.Equal([]string{"consumergroup", "consumergroup", "consumergroup-dlq-merger"}, func() []string {
output := make([]string, 0, 3)
for cluster := range s.builder.clusterSaramaConsumerMap {
output = append(output, cluster.Group)
}
sort.Strings(output)
return output
}())
// DLQ producers for 2 topics in 1 cluster
s.Equal([]string{"dlq-topic", "retry-topic"}, func() []string {
output := make([]string, 0, 2)
for topic := range s.builder.clusterTopicSaramaProducerMap["dlq-cluster"] {
output = append(output, topic)
}
sort.Strings(output)
return output
}())
// make sure retry topic with delay gets populated to the read topic list
s.Equal([]string{"retry-topic"}, func() []string {
output := make([]string, 0, 1)
for _, topicList := range s.builder.clusterTopicsMap {
for _, topic := range topicList {
if topic.Delay > 0 {
output = append(output, topic.Name)
}
}
}
sort.Strings(output)
return output
}())
// saramaClient constructor called for dlq-cluster = broker2:9092
s.Equal([]string{"broker2:9092"}, func() []string {
output := make([]string, 0, 1)
for broker := range s.mockSaramaClientConstructor.clusters {
output = append(output, broker)
}
sort.Strings(output)
return output
}())
// Sarama consumer constructor called for broker1:9092 and broker2:9092 clusters
s.Equal([]string{"broker1:9092", "broker2:9092"}, func() []string {
output := make([]string, 0, 2)
for broker := range s.mockSaramaConsumerConstructor.clusterTopicMap {
output = append(output, broker)
}
sort.Strings(output)
return output
}())
}
func (s *ConsumerBuilderTestSuite) TestBuildConsumersWithCommitDisabled() {
s.builder.kafkaConfig.Offsets.Commits.Enabled = false
consumer, err := s.builder.build()
s.NoError(err)
s.NotNil(consumer)
}
func (m *mockSaramaConsumerConstructor) f(brokers []string, _ string, topics []string, _ *cluster.Config) (consumer.SaramaConsumer, error) {
m.clusterTopicMap[brokers[0]] = topics
return nil, nil
}
func (m *mockSaramaClientConstructor) f(brokers []string, _ *sarama.Config) (sarama.Client, error) {
m.clusters[brokers[0]] = true
return nil, nil
}
func (m *mockSaramaProducerConstructor) f(sarama.Client) (sarama.AsyncProducer, error) {
return nil, nil
}