-
Notifications
You must be signed in to change notification settings - Fork 10
/
producer_test.go
63 lines (49 loc) · 1.49 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
package kafka_wrapper_test
import (
"github.com/IBM/sarama"
"github.com/Trendyol/kafka-wrapper"
"github.com/Trendyol/kafka-wrapper/params"
"github.com/Trendyol/kafka-wrapper/test_utils"
testifyAssert "github.com/stretchr/testify/assert"
"time"
)
func (s *testKafkaSuite) Test_produce_when_broker_is_reachable() {
// Given
var (
assert = testifyAssert.New(s.T())
message = "test"
connectionParams = params.ConnectionParameters{ConsumerGroupID: "some-id"}
topic = "some-topic"
receivedPayload string
err error
producer sarama.SyncProducer
)
connectionParams.Brokers = s.Wrapper.GetBrokerAddress()
connectionParams.Conf = test_utils.CreateBasicConf()
time.Sleep(5 * time.Second)
producer, err = kafka_wrapper.NewProducer(connectionParams)
// When
_, _, err = producer.SendMessage(&sarama.ProducerMessage{
Value: sarama.StringEncoder(message),
Topic: topic,
})
time.Sleep(5 * time.Second)
_, receivedPayload, _ = test_utils.Consume(connectionParams, topic)
// Then
assert.Nil(err)
assert.Equal(receivedPayload, message)
}
func (s *testKafkaSuite) Test_not_produce_when_broker_is_not_reachable() {
// Given
var (
assert = testifyAssert.New(s.T())
wrongConf = params.ConnectionParameters{ConsumerGroupID: "some-id"}
expectedError error
)
wrongConf.Brokers = "localhost:9093"
wrongConf.Conf = test_utils.CreateBasicConf()
// When
_, expectedError = kafka_wrapper.NewProducer(wrongConf)
// Then
assert.NotNil(expectedError)
}