-
Notifications
You must be signed in to change notification settings - Fork 14
/
producer_test.go
106 lines (86 loc) · 2.16 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package kafka
import (
"context"
"testing"
"github.com/gofiber/fiber/v2/utils"
"github.com/segmentio/kafka-go"
)
func Test_producer_Produce_Successfully(t *testing.T) {
// Given
mw := &mockWriter{}
p := producer{w: mw}
// When
err := p.Produce(context.Background(), Message{})
// Then
if err != nil {
t.Fatalf("Producing err %s", err.Error())
}
}
func Test_producer_Produce_interceptor_Successfully(t *testing.T) {
// Given
mw := &mockWriter{}
msg := Message{Headers: make([]Header, 0)}
msg.Headers = append(msg.Headers, kafka.Header{
Key: "x-correlation-id",
Value: []byte(utils.UUIDv4()),
})
interceptor := newMockProducerInterceptor()
p := producer{w: mw, interceptors: interceptor}
// When
err := p.Produce(context.Background(), msg)
// Then
if err != nil {
t.Fatalf("Producing err %s", err.Error())
}
}
func Test_producer_ProduceBatch_Successfully(t *testing.T) {
// Given
mw := &mockWriter{}
p := producer{w: mw}
// When
err := p.ProduceBatch(context.Background(), []Message{{}, {}, {}})
// Then
if err != nil {
t.Fatalf("Batch Producing err %s", err.Error())
}
}
func Test_producer_ProduceBatch_interceptor_Successfully(t *testing.T) {
// Given
mw := &mockWriter{}
interceptor := newMockProducerInterceptor()
p := producer{w: mw, interceptors: interceptor}
// When
err := p.ProduceBatch(context.Background(), []Message{{}, {}, {}})
// Then
if err != nil {
t.Fatalf("Batch Producing err %s", err.Error())
}
}
func Test_producer_Close_Successfully(t *testing.T) {
// Given
mw := &mockWriter{}
p := producer{w: mw}
// When
err := p.Close()
// Then
if err != nil {
t.Fatalf("Closing err %s", err.Error())
}
}
type mockWriter struct{}
func (m *mockWriter) WriteMessages(_ context.Context, _ ...kafka.Message) error {
return nil
}
func (m *mockWriter) Close() error {
return nil
}
type mockProducerInterceptor struct{}
func (i *mockProducerInterceptor) OnProduce(ctx ProducerInterceptorContext) {
ctx.Message.Headers = append(ctx.Message.Headers, kafka.Header{
Key: "test",
Value: []byte("test"),
})
}
func newMockProducerInterceptor() []ProducerInterceptor {
return []ProducerInterceptor{&mockProducerInterceptor{}}
}