-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage_test.go
338 lines (325 loc) · 6.82 KB
/
message_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package zkafka
import (
"bytes"
"context"
"sync"
"testing"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"github.com/stretchr/testify/require"
"github.com/zillow/zfmt"
)
func Test_makeProducerMessageRaw(t *testing.T) {
type args struct {
ctx context.Context
serviceName string
topic string
key *string
value []byte
}
tests := []struct {
name string
args args
want kafka.Message
hasHeaders bool
}{
{
name: "has formatter with valid input, no key, no partition",
args: args{
serviceName: "concierge/test/test_group",
topic: "test_topic",
value: []byte("string"),
},
want: kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: ptr("test_topic"),
// this indicates any partition to confluent-kafka-go
Partition: -1,
},
Opaque: nil,
Headers: nil,
},
hasHeaders: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer recoverThenFail(t)
got := makeProducerMessageRaw(tt.args.ctx, tt.args.topic, tt.args.key, tt.args.value)
require.Equal(t, tt.want.TopicPartition, got.TopicPartition)
require.Equal(t, tt.want.Key, got.Key)
require.Equal(t, tt.want.Key, got.Key)
if tt.hasHeaders {
require.NotEmpty(t, got.Headers)
}
})
}
}
func TestMessage_Headers(t *testing.T) {
type fields struct {
msg kafka.Message
}
tests := []struct {
name string
fields fields
want map[string][]byte
}{
{
name: "empty message",
fields: fields{},
want: make(map[string][]byte),
},
{
name: "msgs with headers",
fields: fields{msg: kafka.Message{
Headers: []kafka.Header{
{
Key: "key1",
Value: []byte("value1"),
},
{
Key: "key2",
Value: []byte("value2"),
},
},
}},
want: map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer recoverThenFail(t)
got := headers(tt.fields.msg)
require.Equal(t, tt.want, got)
})
}
}
func TestMessage_Decode(t *testing.T) {
type fields struct {
value []byte
fmt kFormatter
}
type args struct {
v any
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "empty message, empty input => error",
fields: fields{},
args: args{},
wantErr: true,
},
{
name: "valid message, no formatter, empty input => error",
fields: fields{
value: []byte("test"),
},
args: args{},
wantErr: true,
},
{
name: "valid message, formatter, empty input => error",
fields: fields{
value: []byte("test"),
fmt: zfmtShim{&zfmt.StringFormatter{}},
},
args: args{},
wantErr: true,
},
{
name: "valid message, formatter, valid input => no error",
fields: fields{
value: []byte("test"),
fmt: zfmtShim{&zfmt.StringFormatter{}},
},
args: args{
v: &bytes.Buffer{},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer recoverThenFail(t)
m := Message{
value: tt.fields.value,
fmt: tt.fields.fmt,
}
err := m.Decode(tt.args.v)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
func TestMessage_Done(t *testing.T) {
type fields struct {
Key string
Headers map[string][]byte
value []byte
fmt zfmt.Formatter
doneSig chan bool
}
tests := []struct {
name string
fields fields
}{
{
name: "multiple calls to done",
fields: fields{
doneSig: make(chan bool, 1),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer recoverThenFail(t)
isCalled := false
m := &Message{
Key: tt.fields.Key,
Headers: tt.fields.Headers,
value: tt.fields.value,
fmt: zfmtShim{F: tt.fields.fmt},
doneFunc: func(ctx context.Context) {
isCalled = true
},
}
// call done multiple times and function should still return
var wg sync.WaitGroup
wg.Add(3)
for i := 0; i < 3; i++ {
go func() {
defer wg.Done()
m.DoneWithContext(context.Background())
}()
}
wg.Wait()
require.True(t, isCalled, "doneFunc should have been called at least once")
})
}
}
func Test_addHeaders(t *testing.T) {
type args struct {
kafkaMessage kafka.Message
headers map[string][]byte
}
tests := []struct {
name string
args args
want kafka.Message
}{
{
name: "populated RequestContext",
args: args{
kafkaMessage: kafka.Message{},
headers: map[string][]byte{
"x-b3-traceid": []byte("2"),
"x-request-id": []byte("1"),
"x-user-id": []byte("userID1"),
"x-application-trail": []byte("trail"),
},
},
want: kafka.Message{
Headers: []kafka.Header{
{
Key: "x-b3-traceid",
Value: []byte("2"),
},
{
Key: "x-request-id",
Value: []byte("1"),
},
{
Key: "x-user-id",
Value: []byte("userID1"),
},
{
Key: "x-application-trail",
Value: []byte("trail"),
},
},
},
},
{
name: "conflicting-fields-are-overwritten",
args: args{
kafkaMessage: kafka.Message{
Headers: []kafka.Header{
{
Key: "x-request-id",
Value: []byte("999"),
},
{
Key: "extra-header",
Value: []byte("77"),
},
},
},
headers: map[string][]byte{
"x-b3-traceid": []byte("2"),
"x-request-id": []byte("1"),
"x-user-id": []byte("userID1"),
"x-application-trail": []byte("trail"),
},
},
want: kafka.Message{
Headers: []kafka.Header{
{
Key: "x-b3-traceid",
Value: []byte("2"),
},
{
Key: "x-request-id",
Value: []byte("1"),
},
{
Key: "x-user-id",
Value: []byte("userID1"),
},
{
Key: "x-application-trail",
Value: []byte("trail"),
},
{
Key: "extra-header",
Value: []byte("77"),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer recoverThenFail(t)
got := addHeaders(tt.args.kafkaMessage, tt.args.headers)
require.ElementsMatch(t, tt.want.Headers, got.Headers, "RecordHeaders do not match")
})
}
}
func TestMessage_Value(t *testing.T) {
defer recoverThenFail(t)
valueStr := "here is some string"
m := Message{
value: []byte(valueStr),
}
got := m.Value()
require.NotSame(t, &m.value, &got, "should not return the same reference")
require.Equal(t, valueStr, string(got), "should return the same string representation")
}
func TestMessage_Value_HandleNil(t *testing.T) {
defer recoverThenFail(t)
m := Message{
value: nil,
}
got := m.Value()
require.Nil(t, got, "should have returned nil since underlying data is nil")
}