forked from bsm/sarama-cluster
-
Notifications
You must be signed in to change notification settings - Fork 4
/
unit_test_wip.no-go
206 lines (168 loc) · 4.2 KB
/
unit_test_wip.no-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
/*
Unit tests using a mocked sarama.Client
Copyright 2017 MistSys
*/
package consumer_test
import (
"sync"
"testing"
"github.com/Shopify/sarama"
consumer "github.com/mistsys/sarama-consumer"
)
// NewClient().Close()
func TestClientCreateClose(t *testing.T) {
// create a consumer
sconfig := sarama.NewConfig()
sclient := &mockClient{
config: sconfig,
partitions: map[string][]int32{
"sarama-consumer-sidechannel-offsets": []int32{0, 1, 2},
},
}
config := consumer.NewConfig()
client, err := consumer.NewClient("group", config, sclient)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func(errors <-chan error) {
for err := range errors {
t.Error(err)
}
wg.Done()
}(client.Errors())
client.Close()
wg.Wait()
}
// NewClient() when sidechannel topic does not exist and is not "" in config should cause an error
func TestClientSidechannelTopicNotCreated(t *testing.T) {
// create a consumer
sconfig := sarama.NewConfig()
sclient := &mockClient{
config: sconfig,
// no topics on the simulated kafka
}
config := consumer.NewConfig()
client, err := consumer.NewClient("group", config, sclient)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func(errors <-chan error) {
for err := range errors {
t.Log(err)
}
wg.Done()
}(client.Errors())
client.Close()
wg.Wait()
}
func TestConsumerCreateClose(t *testing.T) {
// create a consumer
sconfig := sarama.NewConfig()
sclient := &mockClient{
config: sconfig,
partitions: map[string][]int32{
"topic1": []int32{0, 1, 2},
},
}
config := consumer.NewConfig()
client, err := consumer.NewClient("group", config, sclient)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func(errors <-chan error) {
for err := range errors {
t.Error(err)
}
}(client.Errors())
con, err := client.Consume("nonexistant-topic")
if err == nil {
t.Fatal("Consuming non-existant topic should have failed")
}
con, err = client.Consume("topic1")
if err != nil {
t.Error(err)
}
if con == nil {
t.Error("no consumer")
} else {
// immediately close down
con.Close()
}
client.Close()
wg.Wait()
}
// mock sarama.Client which implements the metadata API sufficiently for our unit test purposes
type mockClient struct {
config *sarama.Config
partitions map[string][]int32
}
func (mc *mockClient) Brokers() []*sarama.Broker {
return nil
}
func (mc *mockClient) Config() *sarama.Config {
return mc.config
}
func (mc *mockClient) Topics() ([]string, error) {
var topics = make([]string, 0, len(mc.partitions))
for t := range mc.partitions {
topics = append(topics, t)
}
return topics, nil
}
func (mc *mockClient) Partitions(topic string) ([]int32, error) {
if p, ok := mc.partitions[topic]; ok {
return p, nil
}
return nil, sarama.ErrUnknownTopicOrPartition
}
func (mc *mockClient) WritablePartitions(topic string) ([]int32, error) {
return mc.Partitions(topic)
}
func (mc *mockClient) Leader(topic string, part int32) (*sarama.Broker, error) {
return nil, sarama.ErrBrokerNotAvailable
}
func (mc *mockClient) Replicas(topic string, part int32) ([]int32, error) {
return nil, sarama.ErrNotEnoughReplicas
}
func (mc *mockClient) RefreshMetadata(topics ...string) error {
return nil
}
func (mc *mockClient) GetOffset(topic string, part int32, time int64) (int64, error) {
return 0, nil
}
func (mc *mockClient) Coordinator(group string) (*sarama.Broker, error) {
return nil, sarama.ErrBrokerNotAvailable
}
func (mc *mockClient) RefreshCoordinator(group string) error {
return nil
}
func (mc *mockClient) Close() error {
return nil
}
func (mc *mockClient) Closed() bool {
return false
}
// a sortable, comparible list of partition ids
type partitionslist []int32 // a list of the partition ids
// implement sort.Interface
func (pl partitionslist) Len() int { return len(pl) }
func (pl partitionslist) Swap(i, j int) { pl[i], pl[j] = pl[j], pl[i] }
func (pl partitionslist) Less(i, j int) bool { return pl[i] < pl[j] }
// and compare two sorted partitionslist for equality
func (pl partitionslist) Equal(pl2 partitionslist) bool {
if len(pl) != len(pl2) {
return false
}
for i, m := range pl {
if m != pl2[i] {
return false
}
}
return true
}