forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
211 lines (174 loc) · 4.06 KB
/
writer_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package kafka
import (
"context"
"errors"
"io"
"strings"
"testing"
"time"
)
func TestWriter(t *testing.T) {
t.Parallel()
tests := []struct {
scenario string
function func(*testing.T)
}{
{
scenario: "closing a writer right after creating it returns promptly with no error",
function: testWriterClose,
},
{
scenario: "writing 1 message through a writer using round-robin balancing produces 1 message to the first partition",
function: testWriterRoundRobin1,
},
{
scenario: "running out of max attempts should return an error",
function: testWriterMaxAttemptsErr,
},
}
for _, test := range tests {
testFunc := test.function
t.Run(test.scenario, func(t *testing.T) {
t.Parallel()
testFunc(t)
})
}
}
func newTestWriter(config WriterConfig) *Writer {
if len(config.Brokers) == 0 {
config.Brokers = []string{"localhost:9092"}
}
return NewWriter(config)
}
func testWriterClose(t *testing.T) {
const topic = "test-writer-0"
createTopic(t, topic, 1)
w := newTestWriter(WriterConfig{
Topic: topic,
})
if err := w.Close(); err != nil {
t.Error(err)
}
}
func testWriterRoundRobin1(t *testing.T) {
const topic = "test-writer-1"
createTopic(t, topic, 1)
offset, err := readOffset(topic, 0)
if err != nil {
t.Fatal(err)
}
w := newTestWriter(WriterConfig{
Topic: topic,
Balancer: &RoundRobin{},
})
defer w.Close()
if err := w.WriteMessages(context.Background(), Message{
Value: []byte("Hello World!"),
}); err != nil {
t.Error(err)
return
}
msgs, err := readPartition(topic, 0, offset)
if err != nil {
t.Error("error reading partition", err)
return
}
if len(msgs) != 1 {
t.Error("bad messages in partition", msgs)
return
}
for _, m := range msgs {
if string(m.Value) != "Hello World!" {
t.Error("bad messages in partition", msgs)
break
}
}
}
func TestValidateWriter(t *testing.T) {
tests := []struct {
config WriterConfig
errorOccured bool
}{
{config: WriterConfig{}, errorOccured: true},
{config: WriterConfig{Brokers: []string{"broker1", "broker2"}}, errorOccured: true},
{config: WriterConfig{Brokers: []string{"broker1"}, Topic: "topic1"}, errorOccured: false},
}
for _, test := range tests {
err := test.config.Validate()
if test.errorOccured && err == nil {
t.Fail()
}
if !test.errorOccured && err != nil {
t.Fail()
}
}
}
type fakeWriter struct{}
func (f *fakeWriter) messages() chan<- writerMessage {
ch := make(chan writerMessage, 1)
go func() {
for {
msg := <-ch
msg.res <- &writerError{
err: errors.New("bad attempt"),
}
}
}()
return ch
}
func (f *fakeWriter) close() {
}
func testWriterMaxAttemptsErr(t *testing.T) {
const topic = "test-writer-2"
createTopic(t, topic, 1)
w := newTestWriter(WriterConfig{
Topic: topic,
MaxAttempts: 1,
Balancer: &RoundRobin{},
newPartitionWriter: func(p int, config WriterConfig, stats *writerStats) partitionWriter {
return &fakeWriter{}
},
})
defer w.Close()
if err := w.WriteMessages(context.Background(), Message{
Value: []byte("Hello World!"),
}); err == nil {
t.Error("expected error")
return
} else if err != nil {
if !strings.Contains(err.Error(), "bad attempt") {
t.Errorf("unexpected error: %s", err)
return
}
}
}
func readOffset(topic string, partition int) (offset int64, err error) {
var conn *Conn
if conn, err = DialLeader(context.Background(), "tcp", "localhost:9092", topic, partition); err != nil {
return
}
defer conn.Close()
offset, err = conn.ReadLastOffset()
return
}
func readPartition(topic string, partition int, offset int64) (msgs []Message, err error) {
var conn *Conn
if conn, err = DialLeader(context.Background(), "tcp", "localhost:9092", topic, partition); err != nil {
return
}
defer conn.Close()
conn.Seek(offset, SeekAbsolute)
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
batch := conn.ReadBatch(0, 1000000000)
defer batch.Close()
for {
var msg Message
if msg, err = batch.ReadMessage(); err != nil {
if err == io.EOF {
err = nil
}
return
}
msgs = append(msgs, msg)
}
}