-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducer_test.go
45 lines (40 loc) · 877 Bytes
/
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
package kafkamock
import (
"encoding/json"
"github.com/confluentinc/confluent-kafka-go/kafka"
"testing"
)
type CustomEvent struct {
Foo string `json:"foo"`
}
type MyApp struct {
producer Producer
}
func (a * MyApp) sendMessage() {
event := CustomEvent{
Foo: "My Event!",
}
value, _ := json.Marshal(event)
topic := "ms.myapp.messages"
_ = a.producer.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &topic,
Partition: 2,
Offset: 4,
},
Value: value,
}, nil)
}
func TestProduce(t *testing.T) {
p := ProducerMock{}
app := MyApp{&p}
p.AssertProducedCount(t, 0)
app.sendMessage()
p.AssertProducedCount(t, 1)
p.LastProduced().AssertTopic(t, "ms.myapp.messages")
p.LastProduced().AssertPartition(t, 2)
p.LastProduced().AssertOffset(t, 4)
p.LastProduced().AssertValueEquals(t, CustomEvent{
Foo: "My Event!",
})
}