/
consumer_test.go
228 lines (189 loc) · 5.31 KB
/
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
package gtrs
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
)
func TestConsumer_SimpleSync(t *testing.T) {
ms, rdb := startMiniredis(t)
cs := NewConsumer[city](context.TODO(), rdb, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: 0,
BufferSize: 0,
})
var sent int32 = 0 // number of items sent
var confim int32 = 0 // confirm status of receiver
cities := make([]city, 100)
for i := 0; i < len(cities); i++ {
cities[i] = city{Name: fmt.Sprintf("City %v", i), Size: i}
}
go func() {
// Send cities one by one and wait for confirm.
for _, city := range cities {
atomic.StoreInt32(&confim, 0) // clear confirm flag
atomic.AddInt32(&sent, 1)
ms.XAdd("s1", "*", []string{"name", city.Name, "size", fmt.Sprint(city.Size)})
for atomic.LoadInt32(&confim) == 0 {
continue // wait for confirm
}
}
}()
i := 0
for msg := range cs.Chan() {
assert.Nil(t, msg.Err)
assert.Equal(t, cities[i], msg.Data)
assert.Equal(t, int32(i+1), atomic.LoadInt32(&sent))
atomic.StoreInt32(&confim, 1)
i += 1
if i == len(cities) {
break
}
}
}
func TestConsumer_ClientError(t *testing.T) {
ms, rdb := startMiniredis(t)
cs := NewConsumer[city](context.TODO(), rdb, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: 0,
BufferSize: 0,
})
ms.Close()
msg := <-cs.Chan()
assert.NotNil(t, msg.Err)
assert.IsType(t, ReadError{}, msg.Err)
}
func TestConsumer_ParserError(t *testing.T) {
ms, rdb := startMiniredis(t)
cs := NewConsumer[nonParsable](context.TODO(), rdb, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: 0,
BufferSize: 0,
})
ms.XAdd("s1", "*", []string{"Test", "Yes"})
msg := <-cs.Chan()
assert.NotNil(t, msg.Err)
assert.IsType(t, ParseError{}, msg.Err)
assert.Equal(t, errNotParsable, errors.Unwrap(msg.Err))
assert.ErrorIs(t, msg.Err, errNotParsable)
}
func TestConsuer_FieldParseError(t *testing.T) {
type FPE struct {
Name float64
}
ms, rdb := startMiniredis(t)
cs := NewConsumer[FPE](context.TODO(), rdb, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: 0,
BufferSize: 0,
})
ms.XAdd("s1", "*", []string{"name", "FAILFAIL"})
msg := <-cs.Chan()
assert.NotNil(t, msg.Err)
var fpe FieldParseError
assert.ErrorAs(t, msg.Err, &fpe)
assert.Equal(t, "Name", fpe.Field)
assert.Equal(t, "FAILFAIL", fpe.Value)
assert.NotNil(t, fpe.Err)
}
func TestConsumer_Close(t *testing.T) {
_, rdb := startMiniredis(t)
cs := NewConsumer[nonParsable](context.TODO(), rdb, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: 0,
BufferSize: 0,
})
cs.Close()
_, ok := <-cs.Chan()
assert.False(t, ok)
}
func TestConsumer_CloseGetSeenIDs(t *testing.T) {
var readCount = 100
var consumeCount = 75
ms, rdb := startMiniredis(t)
cs := NewConsumer[city](context.TODO(), rdb, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: 0,
BufferSize: 0,
})
for i := 1; i <= readCount; i++ {
ms.XAdd("s1", fmt.Sprintf("0-%v", i), []string{"name", "Town"})
}
for i := 1; i <= consumeCount; i++ {
<-cs.Chan()
}
seen := cs.Close()
assert.Equal(t, fmt.Sprintf("0-%v", consumeCount), seen["s1"])
}
func TestConsumer_CancelContext(t *testing.T) {
var readCount = 100
var consumeCount = 75
ms, rdb := startMiniredis(t)
ctx, cancelFunc := context.WithCancel(context.TODO())
defer cancelFunc()
cs := NewConsumer[city](ctx, rdb, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: 0,
BufferSize: 0,
})
for i := 1; i <= readCount; i++ {
ms.XAdd("s1", fmt.Sprintf("0-%v", i), []string{"name", "Town"})
}
stopid := fmt.Sprintf("0-%v", consumeCount)
for msg := range cs.Chan() {
assert.Nil(t, msg.Err)
if msg.ID == stopid {
cancelFunc()
}
}
seen := cs.Close()
assert.Equal(t, fmt.Sprintf("0-%v", consumeCount), seen["s1"])
}
type benchmarkClientMock struct {
*redis.Client
msgs []redis.XMessage
}
func (bmc benchmarkClientMock) XRead(ctx context.Context, a *redis.XReadArgs) *redis.XStreamSliceCmd {
return redis.NewXStreamSliceCmdResult([]redis.XStream{
{Stream: "s1", Messages: bmc.msgs},
}, nil)
}
// BenchmarkConsumer measures the throughput of the whole pipeline.
//
// An unbuffered channel read takes about 100-150 ns, a buffered channel read takes up to 100ns.
// A goroutine wakeup takes less than 100ns.
//
// The current implementation takes about 400-600 ns for a single loop iteration. That is not optimal,
// but gives nonetheless an acceptable thoughput of 2 M/s, which is likely to be faster than
// any ordinary users needs.
func BenchmarkConsumer(b *testing.B) {
b.SetParallelism(1)
msgbuf := make([]redis.XMessage, 5000)
for i := 0; i < len(msgbuf); i++ {
msgbuf[i] = redis.XMessage{
ID: fmt.Sprint(i),
Values: map[string]any{"A": "B"},
}
}
for _, size := range []uint{0, 10, 100, 1000, 10000} {
b.Run(fmt.Sprintf("s-%v", size), func(b *testing.B) {
mock := benchmarkClientMock{msgs: msgbuf}
cs := NewConsumer[empty](context.TODO(), mock, StreamIDs{"s1": "0-0"}, StreamConsumerConfig{
Block: 0,
Count: int64(len(msgbuf)),
BufferSize: size,
})
b.StartTimer()
var read int
for range cs.Chan() {
if read += 1; read == b.N {
break
}
}
b.StopTimer()
})
}
}