-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgroup_consumer_test.go
401 lines (330 loc) · 10.3 KB
/
group_consumer_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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package gtrs
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
)
// Consumer groups are not supported in miniredis yet, we'll have to mock clients
type groupCreateMock struct{}
func (gcm groupCreateMock) XGroupCreateMkStream(ctx context.Context, stream, group, start string) *redis.StatusCmd {
return redis.NewStatusResult("OK", nil)
}
// TestGroupConsumer_SimpleSync
type simpleSyncMock struct {
*redis.Client
groupCreateMock
acks int64
}
func (sc *simpleSyncMock) XReadGroup(ctx context.Context, a *redis.XReadGroupArgs) *redis.XStreamSliceCmd {
return redis.NewXStreamSliceCmdResult([]redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{
{ID: "0-1", Values: map[string]interface{}{"name": "TestTown"}},
},
}}, nil)
}
func (sc *simpleSyncMock) XAck(ctx context.Context, stream, group string, ids ...string) *redis.IntCmd {
atomic.AddInt64(&sc.acks, 1)
return redis.NewIntResult(1, nil)
}
func TestGroupConsumer_SimpleSync(t *testing.T) {
rdb := simpleSyncMock{}
cs := NewGroupMultiStreamConsumer[city](context.TODO(), &rdb, "g1", "c1", map[string]string{"s1": ">"})
var i int64 = 0
var readCount int64 = 100
for msg := range cs.Chan() {
assert.Nil(t, msg.Err)
cs.Ack(msg)
cs.AwaitAcks()
assert.Equal(t, i+1, rdb.acks)
if i += 1; i >= readCount {
break
}
}
assert.Equal(t, readCount, rdb.acks)
}
// TestGroupConsumer_SwitchToNew
type switchToNewMock struct {
*redis.Client
groupCreateMock
maxHandout int
}
func (sc switchToNewMock) XReadGroup(ctx context.Context, a *redis.XReadGroupArgs) *redis.XStreamSliceCmd {
if a.Streams[1] == ">" {
return redis.NewXStreamSliceCmdResult([]redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{
{ID: "1-1", Values: map[string]interface{}{"name": "NewTown"}},
},
}}, nil)
} else if a.Streams[1] == fmt.Sprintf("0-%v", sc.maxHandout) {
return redis.NewXStreamSliceCmdResult([]redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{},
}}, nil)
} else {
id, _ := strconv.ParseInt(strings.Split(a.Streams[1], "-")[1], 10, 64)
return redis.NewXStreamSliceCmdResult([]redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%v", id+1), Values: map[string]interface{}{"name": "OldTown"}},
},
}}, nil)
}
}
func (sc *switchToNewMock) XAck(ctx context.Context, stream, group string, ids ...string) *redis.IntCmd {
return redis.NewIntResult(1, nil)
}
func TestGroupConsumer_SwitchToNew(t *testing.T) {
var readCount = 100
var maxHistory = 50
rdb := switchToNewMock{maxHandout: maxHistory}
cs := NewGroupMultiStreamConsumer[city](context.TODO(), &rdb, "g1", "c1", map[string]string{"s1": "0-0"})
var i = 0
for msg := range cs.Chan() {
assert.Nil(t, msg.Err)
if i < maxHistory {
assert.Equal(t, "OldTown", msg.Data.Name)
} else {
assert.Equal(t, "NewTown", msg.Data.Name)
}
if i += 1; i >= readCount {
break
}
}
}
// TestGroupConsumer_RemainingAck
type remainingAckMock struct {
*redis.Client
groupCreateMock
}
func (sc *remainingAckMock) XAck(ctx context.Context, stream, group string, ids ...string) *redis.IntCmd {
if ids[0] == "0-0" || ids[0] == "0-1" {
// fail not zero ack
return redis.NewIntResult(0, nil)
} else {
select {
case <-ctx.Done():
case <-time.After(1000 * time.Second):
}
return redis.NewIntResult(0, errors.New("must cancel"))
}
}
func (sc *remainingAckMock) XReadGroup(ctx context.Context, a *redis.XReadGroupArgs) *redis.XStreamSliceCmd {
return redis.NewXStreamSliceCmdResult([]redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{
{ID: "0-1", Values: map[string]interface{}{"name": "TestTown"}},
},
}}, nil)
}
func TestGroupConsumer_RemainingAck(t *testing.T) {
var ackCount = 100
rdb := remainingAckMock{}
cs := NewGroupMultiStreamConsumer[city](context.TODO(), &rdb, "g1", "c1", map[string]string{"s1": "0-0"}, GroupConsumerConfig{
AckBufferSize: uint(ackCount) + 1,
})
for i := 1; i <= ackCount; i += 1 {
cs.Ack(Message[city]{ID: fmt.Sprintf("0-%v", i)})
}
time.Sleep(50 * time.Millisecond)
rm := cs.Close()
assert.Len(t, rm, ackCount)
}
// TestGroupConsumer_AckErrors
type ackErrorMock struct {
*redis.Client
groupCreateMock
i int
}
func (ae *ackErrorMock) XAck(ctx context.Context, stream, group string, ids ...string) *redis.IntCmd {
return redis.NewIntResult(0, errors.New("must fail"))
}
func (ae *ackErrorMock) XReadGroup(ctx context.Context, a *redis.XReadGroupArgs) *redis.XStreamSliceCmd {
defer func() { ae.i += 1 }()
return redis.NewXStreamSliceCmdResult([]redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%v", ae.i), Values: map[string]interface{}{"name": "TestTown"}},
},
}}, nil)
}
func TestGroupConsumer_AckErrors(t *testing.T) {
var readCount = 5_000
rdb := ackErrorMock{}
cs := NewGroupMultiStreamConsumer[city](context.TODO(), &rdb, "g1", "c1", map[string]string{"s1": "0-0"})
var ackErrors = 0
var seen = 0
for msg := range cs.Chan() {
if _, ok := msg.Err.(AckError); ok {
ackErrors += 1
} else {
seen += 1
cs.Ack(msg)
}
if seen == readCount {
break
}
}
lastErrs := cs.AwaitAcks()
unseen := cs.Close()
assert.NotZero(t, len(lastErrs)+len(unseen))
assert.Equal(t, readCount, ackErrors+len(unseen)+len(lastErrs))
}
// TestGroupConsumer_AckErrorCancel
func TestGroupConsumer_AckErrorCancel(t *testing.T) {
var readCount = 100
rdb := ackErrorMock{}
ctx, cancelFunc := context.WithCancel(context.TODO())
cs := NewGroupMultiStreamConsumer[city](ctx, &rdb, "g1", "c1", map[string]string{"s1": "0-0"})
var msgs []Message[city]
for msg := range cs.Chan() {
assert.Nil(t, msg.Err)
msgs = append(msgs, msg)
if len(msgs) == readCount {
break
}
}
cancelFunc()
for _, msg := range msgs {
cs.Ack(msg)
}
remaining := cs.Close()
assert.Len(t, remaining, readCount)
}
// TestGroupConsumer_FailCreate
type failCreateMock struct {
*redis.Client
}
func (fcm failCreateMock) XGroupCreateMkStream(ctx context.Context, stream, group, start string) *redis.StatusCmd {
return redis.NewStatusResult("", errors.New("test error"))
}
func TestGroupConsumer_CreateError(t *testing.T) {
rdb := failCreateMock{}
cs := NewGroupMultiStreamConsumer[city](context.TODO(), &rdb, "g1", "c1", map[string]string{"s1": "0-0"})
msg := <-cs.Chan()
assert.NotNil(t, msg.Err)
assert.IsType(t, ReadError{}, msg.Err)
assert.ErrorContains(t, msg.Err, "read error: test error")
}
// TestGroupConsumer_ReadError
type readErrorMock struct {
groupCreateMock
*redis.Client
}
func (rem readErrorMock) XReadGroup(ctx context.Context, a *redis.XReadGroupArgs) *redis.XStreamSliceCmd {
return redis.NewXStreamSliceCmdResult(nil, errors.New("test error"))
}
func TestGroupConsumer_ReadError(t *testing.T) {
rdb := readErrorMock{}
cs := NewGroupMultiStreamConsumer[city](context.TODO(), &rdb, "g1", "c1", map[string]string{"s1": "0-0"})
msg := <-cs.Chan()
assert.NotNil(t, msg.Err)
assert.NotNil(t, errors.Unwrap(msg.Err))
assert.IsType(t, ReadError{}, msg.Err)
assert.ErrorContains(t, msg.Err, "read error: test error")
}
// TestGroupConsumer_ConcurrentRead
type readConcurentErrorMock struct {
groupCreateMock
*redis.Client
i int
}
func (rem *readConcurentErrorMock) XReadGroup(ctx context.Context, a *redis.XReadGroupArgs) *redis.XStreamSliceCmd {
defer func() { rem.i += 5 }()
var val = []redis.XStream{}
if rem.i <= 35 {
val = []redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%d", rem.i), Values: map[string]any{"name": "TestTown", "size": 1000}},
{ID: fmt.Sprintf("0-%d", rem.i+1), Values: map[string]any{"name": "TestTown", "size": 1000}},
{ID: fmt.Sprintf("0-%d", rem.i+2), Values: map[string]any{"name": "TestTown", "size": 1000}},
{ID: fmt.Sprintf("0-%d", rem.i+3), Values: map[string]any{"name": "TestTown", "size": 1000}},
},
}, {
Stream: "s2",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%d", rem.i), Values: map[string]any{"name": "TestTown", "size": 1000}},
},
}, {
Stream: "s3",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%d", rem.i), Values: map[string]any{"name": "TestTown", "size": 1000}},
{ID: fmt.Sprintf("0-%d", rem.i+1), Values: map[string]any{"name": "TestTown", "size": 1000}},
},
}}
}
if rem.i >= 40 && rem.i < 70 {
val = []redis.XStream{{
Stream: "s1",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%d", rem.i), Values: map[string]any{"name": "TestTown", "size": 1000}},
},
}, {
Stream: "s2",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%d", rem.i), Values: map[string]any{"name": "TestTown", "size": 1000}},
{ID: fmt.Sprintf("0-%d", rem.i+1), Values: map[string]any{"name": "TestTown", "size": 1000}},
{ID: fmt.Sprintf("0-%d", rem.i+2), Values: map[string]any{"name": "TestTown", "size": 1000}},
},
}, {
Stream: "s3",
Messages: []redis.XMessage{
{ID: fmt.Sprintf("0-%d", rem.i), Values: map[string]any{"name": "TestTown", "size": 1000}},
},
}}
}
return redis.NewXStreamSliceCmdResult(val, nil)
}
func (rem *readConcurentErrorMock) XAck(ctx context.Context, stream, group string, ids ...string) *redis.IntCmd {
if len(ids) > 0 {
i := -1
fmt.Sscanf(ids[0], "0-%d", &i)
if i != -1 && i%15 == 0 {
return redis.NewIntResult(0, errors.New("must fail by modulo"))
}
}
return redis.NewIntResult(1, nil)
}
func TestGroupConsumer_ConcurrentRead(t *testing.T) {
rdb := readConcurentErrorMock{}
cs := NewGroupMultiStreamConsumer[city](context.TODO(), &rdb, "g1", "c1", map[string]string{"s1": "0-0", "s2": "0-0", "s3": "0-0"})
msg := make([]Message[city], 0, 21)
msgError := make([]Message[city], 0, 21)
msgList := make([]Message[city], 0, 121)
for end := true; end; {
select {
case tmp := <-cs.Chan():
if tmp.Err == nil {
cs.Ack(tmp)
} else if _, ok := tmp.Err.(AckError); ok {
msgError = append(msgError, tmp)
continue
} else {
t.Errorf("error not found: %s", tmp.Err.Error())
}
msgList = append(msgList, tmp)
if len(msgList)%2 == 0 {
ml := cs.AwaitAcks()
if len(ml) > 0 {
msg = append(msg, ml...)
}
}
case <-time.Tick(750 * time.Millisecond):
msg = append(msg, cs.AwaitAcks()...)
end = false
}
}
assert.Greater(t, len(msgError), 1)
assert.Greater(t, len(msg), 1)
assert.Equal(t, len(msg)+len(msgError), 15)
assert.Equal(t, len(cs.Close())+len(msgList)+len(msg)+len(msgError), 101)
}