-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_test.go
More file actions
111 lines (99 loc) · 2.13 KB
/
Copy pathasync_test.go
File metadata and controls
111 lines (99 loc) · 2.13 KB
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
107
108
109
110
111
package samsa
import (
"testing"
"github.com/IBM/sarama"
)
func TestNew(t *testing.T) {
mockAsyncProducer := new(MockAsyncProducer)
cases := []struct {
name string
cfg Config
shouldErr bool
}{
{
name: "Success",
cfg: Config{
Endpoints: []string{"localhost:9092"},
Topic: "test-topic",
AsyncProducer: mockAsyncProducer,
},
shouldErr: false,
},
{
name: "Error",
cfg: Config{
Endpoints: []string{},
Topic: "",
},
shouldErr: true,
},
}
for _, tc := range cases {
t.Run(
tc.name, func(t *testing.T) {
_, err := NewAsyncKafkaWriter(tc.cfg)
if (err != nil) != tc.shouldErr {
t.Errorf("New() error = %v, shouldErr %v", err, tc.shouldErr)
}
},
)
}
}
type MockAsyncProducer struct {
sarama.AsyncProducer
inputChan chan *sarama.ProducerMessage
errChan chan *sarama.ProducerError
}
func (m *MockAsyncProducer) Input() chan<- *sarama.ProducerMessage {
return m.inputChan
}
func (m *MockAsyncProducer) Errors() <-chan *sarama.ProducerError {
return m.errChan
}
func TestWriteAsync(t *testing.T) {
t.Parallel()
tests := []struct {
name string
bufferSize int
msg []byte
wantN int
wantErr bool
}{
{
name: "RegularCase",
bufferSize: 1,
msg: []byte("Message"),
wantN: 7,
wantErr: false,
},
{
name: "EmptyMessage",
bufferSize: 1,
msg: []byte(""),
wantN: 0,
wantErr: false,
},
}
for _, test := range tests {
t.Run(
test.name, func(t *testing.T) {
conf := Config{
Topic: "test-topic",
BufferSize: test.bufferSize,
AsyncProducer: &MockAsyncProducer{
inputChan: make(chan *sarama.ProducerMessage),
errChan: make(chan *sarama.ProducerError),
},
}
writer, err := NewAsyncKafkaWriter(conf)
if err != nil {
t.Fatalf("unable to create KafkaWriter: %v", err)
}
gotN, gotErr := writer.Write(test.msg)
if gotN != test.wantN || (gotErr != nil) != test.wantErr {
t.Fatalf("KafkaWriter.Write() = %v, %v, want %v, %v", gotN, gotErr, test.wantN, test.wantErr)
}
},
)
}
}