forked from Trendyol/kafka-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer_test.go
83 lines (69 loc) · 2.06 KB
/
producer_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
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 publishing a message", func() {
Context("and the broker is reachable", func() {
var (
message = "test"
conf = kafka_wrapper.ConnectionParameters{
ConsumerGroupID: "some-id",
Topics: []string{"createClaim"},
}
receivedPayload string
err error
producer sarama.SyncProducer
)
test_utils.BeforeAll(func() {
conf.Brokers = KafkaContainer.Address()
conf.Conf = configuration("2.2.0")
time.Sleep(5 * time.Second)
producer, err = kafka_wrapper.NewProducer(conf)
Expect(err).NotTo(HaveOccurred())
_, _, err = producer.SendMessage(&sarama.ProducerMessage{
Value: sarama.StringEncoder(message),
Topic: conf.Topics[0],
})
time.Sleep(5 * time.Second)
_, receivedPayload, _ = test_utils.Consume(conf)
})
It("should not produce an error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should send the message with expected payload", func() {
Expect(receivedPayload).Should(Equal(message))
})
})
Context("and the broker is unreachable", func() {
var (
wrongConf = kafka_wrapper.ConnectionParameters{
Conf: sarama.NewConfig(),
Brokers: "localhost:9093",
Topics: []string{"createClaim"},
}
expectedError error
)
test_utils.BeforeAll(func() {
_, expectedError = kafka_wrapper.NewProducer(wrongConf)
})
It("should produce an error", func() {
Expect(expectedError).To(HaveOccurred())
})
})
})
func configuration(version string) *sarama.Config {
kafkaConfig := sarama.NewConfig()
v, err := sarama.ParseKafkaVersion("2.2.0")
Expect(err).NotTo(HaveOccurred())
kafkaConfig.Version = v
kafkaConfig.ClientID = "oms-event-generator"
kafkaConfig.Consumer.Offsets.Initial = sarama.OffsetOldest
kafkaConfig.Producer.Return.Successes = true
kafkaConfig.Consumer.Return.Errors = true
return kafkaConfig
}