forked from Trendyol/kafka-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer_test.go
103 lines (87 loc) · 2.66 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
package kafka_wrapper_test
import (
"time"
"github.com/Shopify/sarama"
kafka_wrapper "github.com/Trendyol/kafka-wrapper"
"github.com/Trendyol/kafka-wrapper/test_utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("When consuming a message", func() {
Context("and the broker is reachable", func() {
var (
conf = kafka_wrapper.ConnectionParameters{
ConsumerGroupID: "consumerGroup",
Topics: []string{"testtopic"},
RetryTopic: "testtopic_RETRY",
ErrorTopic: "testtopic_ERROR",
}
expectedMessage = "test"
messageChn = make(chan string, 1)
receivedMessage string
subscriptionError error
)
test_utils.BeforeAll(func() {
conf.Brokers = KafkaContainer.Address()
conf.Conf = configuration("2.2.0")
time.Sleep(5 * time.Second)
testProducer, err := kafka_wrapper.NewProducer(conf)
Expect(err).NotTo(HaveOccurred())
_, _, err = testProducer.SendMessage(&sarama.ProducerMessage{
Value: sarama.StringEncoder(expectedMessage),
Topic: conf.Topics[0],
})
Expect(err).NotTo(HaveOccurred())
testConsumer, err := kafka_wrapper.NewConsumer(conf)
Expect(err).NotTo(HaveOccurred())
testConsumer.Subscribe(newEventHandler(messageChn))
receivedMessage = <-messageChn
})
It("should not produce error", func() {
Expect(subscriptionError).NotTo(HaveOccurred())
})
It("should consume the expected expectedMessage", func() {
Expect(receivedMessage).Should(Equal(expectedMessage))
})
})
Context("and the broker is unreachable", func() {
var (
wrongConf = kafka_wrapper.ConnectionParameters{
Conf: sarama.NewConfig(),
Brokers: "localhost:9093",
Topics: []string{"createClaim2"},
RetryTopic: "createClaim2_RETRY",
ErrorTopic: "createClaim2_ERROR",
}
expectedError error
)
test_utils.BeforeAll(func() {
_, expectedError = kafka_wrapper.NewConsumer(wrongConf)
})
It("should produce an error", func() {
Expect(expectedError).To(HaveOccurred())
})
})
})
func newEventHandler(message chan string) kafka_wrapper.EventHandler {
return &testEventHandler{
message: message,
subscriptionStatusCh: make(chan bool),
}
}
type testEventHandler struct {
message chan string
subscriptionStatusCh chan bool
}
func (ge *testEventHandler) Setup(sarama.ConsumerGroupSession) error {
return nil
}
func (ge *testEventHandler) Cleanup(sarama.ConsumerGroupSession) error {
return nil
}
func (ge *testEventHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for message := range claim.Messages() {
ge.message <- string(message.Value)
}
return nil
}