-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup_reader_test.go
134 lines (108 loc) · 2.77 KB
/
group_reader_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
package krater
import (
"bytes"
"strconv"
"testing"
"github.com/bradfitz/iter"
"gopkg.in/Shopify/sarama.v1"
kcon "github.com/ORBAT/krater/kafkaconsumer"
)
type fakeConsumer struct {
outMsgs chan *sarama.ConsumerMessage
errCh chan error
ackCh chan *sarama.ConsumerMessage
msgs chan *sarama.ConsumerMessage
ackCount int
nMsgs int
}
func genMsgs(n int, topic string) (msgs []*sarama.ConsumerMessage, nBytes int) {
msgs = make([]*sarama.ConsumerMessage, n)
for i := range iter.N(n) {
val := []byte("msgval" + strconv.Itoa(i))
nBytes += len(val)
msgs[i] = &sarama.ConsumerMessage{Value: val, Topic: topic, Partition: int32(i), Offset: int64(i)}
}
return
}
func newFakeConsumer(chsz int, t testing.TB) *fakeConsumer {
fc := &fakeConsumer{
outMsgs: make(chan *sarama.ConsumerMessage, chsz),
errCh: make(chan error, chsz),
ackCh: make(chan *sarama.ConsumerMessage, chsz),
msgs: make(chan *sarama.ConsumerMessage, chsz),
}
go func() { // verify that acks are done in the same order the messages were received in
for msg := range fc.ackCh {
fc.ackCount += 1
select {
case expMsg := <-fc.msgs:
if expMsg != msg {
t.Errorf("expected message %+v but got %+v", expMsg, msg)
}
default:
t.Errorf("Message %+v was unexpectedly acked", msg)
}
if fc.ackCount >= fc.nMsgs {
t.Logf("Received %d acks, nMsgs %d, closing msg chan", fc.ackCount, fc.nMsgs)
close(fc.outMsgs)
}
}
}()
return fc
}
func (fc *fakeConsumer) addMsgs(msgs []*sarama.ConsumerMessage) {
fc.nMsgs += len(msgs)
for _, msg := range msgs {
fc.outMsgs <- msg
fc.msgs <- msg
}
}
func (fc *fakeConsumer) Ack(msg *sarama.ConsumerMessage) {
fc.ackCh <- msg
}
func (fc *fakeConsumer) addError(err error) {
fc.errCh <- err
}
func (_ fakeConsumer) Interrupt() {}
func (fc *fakeConsumer) Close() error {
close(fc.ackCh)
close(fc.errCh)
close(fc.outMsgs)
return nil
}
func (fc *fakeConsumer) Messages() <-chan *sarama.ConsumerMessage {
return fc.outMsgs
}
func (fc *fakeConsumer) Errors() <-chan error {
return fc.errCh
}
var _ kcon.Consumer = &fakeConsumer{}
const (
testGroup = "group1"
)
func newReader(fc kcon.Consumer) *GroupReader {
if gr, err := NewGroupReader(testGroup, []string{testTopic}, "bla:1234", nil); err != nil {
panic(err)
} else {
gr.newCg = func() (kcon.Consumer, error) {
return fc, nil
}
return gr
}
}
func TestOkWriteTo(t *testing.T) {
// defer LogTo(os.Stderr)()
fc := newFakeConsumer(20, t)
msgs, nBytes := genMsgs(10, testTopic)
fc.addMsgs(msgs)
gr := newReader(fc)
var buf bytes.Buffer
n, err := gr.WriteTo(&buf)
if err != nil {
t.Errorf("WriteTo got error %+v", err)
}
if n != int64(nBytes) {
t.Errorf("Expected a total byte count of %d but got %d", nBytes, n)
}
t.Logf("buf %+v", buf.String())
}