-
Notifications
You must be signed in to change notification settings - Fork 101
/
produce_test.go
286 lines (262 loc) · 5.91 KB
/
produce_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"os"
"reflect"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/require"
)
func TestHashCode(t *testing.T) {
data := []struct {
in string
expected int32
}{
{
in: "",
expected: 0,
},
{
in: "a",
expected: 97,
},
{
in: "b",
expected: 98,
},
{
in: "⌘",
expected: 8984,
},
{
in: "😼", //non-bmp character, 4bytes in utf16
expected: 1772959,
},
{
in: "hashCode",
expected: 147696667,
},
{
in: "c03a3475-3ed6-4ed1-8ae5-1c432da43e73",
expected: 1116730239,
},
{
in: "random",
expected: -938285885,
},
}
for _, d := range data {
actual := hashCode(d.in)
if actual != d.expected {
t.Errorf("expected %v but found %v\n", d.expected, actual)
}
}
}
func TestHashCodePartition(t *testing.T) {
data := []struct {
key string
partitions int32
expected int32
}{
{
key: "",
partitions: 0,
expected: -1,
},
{
key: "",
partitions: 1,
expected: 0,
},
{
key: "super-duper-key",
partitions: 1,
expected: 0,
},
{
key: "",
partitions: 1,
expected: 0,
},
{
key: "",
partitions: 2,
expected: 0,
},
{
key: "a",
partitions: 2,
expected: 1,
},
{
key: "b",
partitions: 2,
expected: 0,
},
{
key: "random",
partitions: 2,
expected: 1,
},
{
key: "random",
partitions: 5,
expected: 0,
},
}
for _, d := range data {
actual := hashCodePartition(d.key, d.partitions)
if actual != d.expected {
t.Errorf("expected %v but found %v for key %#v and %v partitions\n", d.expected, actual, d.key, d.partitions)
}
}
}
func TestProduceParseArgs(t *testing.T) {
expectedTopic := "test-topic"
givenBroker := "hans:9092"
expectedBrokers := []string{givenBroker}
target := &produceCmd{}
os.Setenv(ENV_TOPIC, expectedTopic)
os.Setenv(ENV_BROKERS, givenBroker)
target.parseArgs([]string{})
if target.topic != expectedTopic ||
!reflect.DeepEqual(target.brokers, expectedBrokers) {
t.Errorf(
"Expected topic %v and brokers %v from env vars, got topic %v and brokers %v.",
expectedTopic,
expectedBrokers,
target.topic,
target.brokers,
)
return
}
// default brokers to localhost:9092
os.Setenv(ENV_TOPIC, "")
os.Setenv(ENV_BROKERS, "")
expectedBrokers = []string{"localhost:9092"}
target.parseArgs([]string{"-topic", expectedTopic})
if target.topic != expectedTopic ||
!reflect.DeepEqual(target.brokers, expectedBrokers) {
t.Errorf(
"Expected topic %v and brokers %v from env vars, got topic %v and brokers %v.",
expectedTopic,
expectedBrokers,
target.topic,
target.brokers,
)
return
}
// command line arg wins
os.Setenv(ENV_TOPIC, "BLUBB")
os.Setenv(ENV_BROKERS, "BLABB")
expectedBrokers = []string{givenBroker}
target.parseArgs([]string{"-topic", expectedTopic, "-brokers", givenBroker})
if target.topic != expectedTopic ||
!reflect.DeepEqual(target.brokers, expectedBrokers) {
t.Errorf(
"Expected topic %v and brokers %v from env vars, got topic %v and brokers %v.",
expectedTopic,
expectedBrokers,
target.topic,
target.brokers,
)
return
}
}
func newMessage(key, value string, partition int32) message {
var k *string
if key != "" {
k = &key
}
var v *string
if value != "" {
v = &value
}
return message{
Key: k,
Value: v,
Partition: &partition,
}
}
func TestMakeSaramaMessage(t *testing.T) {
target := &produceCmd{decodeKey: "string", decodeValue: "string"}
key, value := "key", "value"
msg := message{Key: &key, Value: &value}
actual, err := target.makeSaramaMessage(msg)
require.Nil(t, err)
require.Equal(t, []byte(key), actual.Key)
require.Equal(t, []byte(value), actual.Value)
target.decodeKey, target.decodeValue = "hex", "hex"
key, value = "41", "42"
msg = message{Key: &key, Value: &value}
actual, err = target.makeSaramaMessage(msg)
require.Nil(t, err)
require.Equal(t, []byte("A"), actual.Key)
require.Equal(t, []byte("B"), actual.Value)
target.decodeKey, target.decodeValue = "base64", "base64"
key, value = "aGFucw==", "cGV0ZXI="
msg = message{Key: &key, Value: &value}
actual, err = target.makeSaramaMessage(msg)
require.Nil(t, err)
require.Equal(t, []byte("hans"), actual.Key)
require.Equal(t, []byte("peter"), actual.Value)
}
func TestDeserializeLines(t *testing.T) {
target := &produceCmd{}
target.partitioner = "hashCode"
data := []struct {
in string
literal bool
partition int32
partitionCount int32
expected message
}{
{
in: "",
literal: false,
partitionCount: 1,
expected: newMessage("", "", 0),
},
{
in: `{"key":"hans","value":"123"}`,
literal: false,
partitionCount: 4,
expected: newMessage("hans", "123", hashCodePartition("hans", 4)),
},
{
in: `{"key":"hans","value":"123","partition":1}`,
literal: false,
partitionCount: 3,
expected: newMessage("hans", "123", 1),
},
{
in: `{"other":"json","values":"avail"}`,
literal: true,
partition: 2,
partitionCount: 4,
expected: newMessage("", `{"other":"json","values":"avail"}`, 2),
},
{
in: `so lange schon`,
literal: false,
partitionCount: 3,
expected: newMessage("", "so lange schon", 0),
},
}
for _, d := range data {
in := make(chan string, 1)
out := make(chan message)
target.literal = d.literal
target.partition = d.partition
go target.deserializeLines(in, out, d.partitionCount)
in <- d.in
select {
case <-time.After(50 * time.Millisecond):
t.Errorf("did not receive output in time")
case actual := <-out:
if !(reflect.DeepEqual(d.expected, actual)) {
t.Errorf(spew.Sprintf("\nexpected %#v\nactual %#v", d.expected, actual))
}
}
}
}