-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream_test.go
332 lines (290 loc) · 7.1 KB
/
stream_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
package memlog
import (
"context"
"errors"
"sync"
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestLog_Stream(t *testing.T) {
t.Run("streams records from log start then cancels stream", func(t *testing.T) {
testCases := []struct {
name string
logStart Offset
streamStart Offset
segSize int
stopAfter int
}{
{
name: "log starts at 0, stream starts at 0, receives 5, cancels stream",
logStart: 0,
streamStart: 0,
segSize: 10,
stopAfter: 5,
},
{
name: "log starts at 10, stream starts at 10, receives 5, cancels stream",
logStart: 10,
streamStart: 10,
segSize: 10,
stopAfter: 5,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testData := NewTestDataSlice(t, tc.segSize)
opts := []Option{
WithStartOffset(tc.logStart),
WithMaxSegmentSize(tc.segSize),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l, err := New(ctx, opts...)
assert.NilError(t, err)
// seed log
for _, d := range testData {
_, err = l.Write(ctx, d)
assert.NilError(t, err)
}
stream := l.Stream(ctx, tc.streamStart)
counter := 0
for {
if r, ok := stream.Next(); ok {
assert.Equal(t, r.Metadata.Offset, Offset(counter)+tc.streamStart)
counter++
if counter == tc.stopAfter {
cancel()
}
continue
}
break
}
assert.Assert(t, errors.Is(stream.Err(), context.Canceled))
assert.Equal(t, counter, tc.stopAfter)
})
}
})
t.Run("stream returns out of range error", func(t *testing.T) {
testCases := []struct {
name string
logStart Offset
writeRecords int
streamStart Offset
segSize int
}{
{
name: "log starts at 0, stream starts at -10",
logStart: 0,
writeRecords: 10,
streamStart: -10,
segSize: 10,
},
{
name: "log starts at 10, stream starts at 0",
logStart: 10,
writeRecords: 10,
streamStart: 0,
segSize: 10,
},
{
name: "log starts at 0, first records purged, stream starts at 60",
logStart: 0,
writeRecords: 100,
streamStart: 60,
segSize: 10,
},
{
name: "log starts at 100, first records purged, stream starts at 150",
logStart: 100,
writeRecords: 100,
streamStart: 150,
segSize: 10,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testData := NewTestDataSlice(t, tc.writeRecords)
opts := []Option{
WithStartOffset(tc.logStart),
WithMaxSegmentSize(tc.segSize),
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
l, err := New(ctx, opts...)
assert.NilError(t, err)
// seed log
for _, d := range testData {
_, err = l.Write(ctx, d)
assert.NilError(t, err)
}
stream := l.Stream(ctx, tc.streamStart)
for {
if _, ok := stream.Next(); ok {
t.Fatalf("should not receive from stream")
} else {
break
}
}
assert.Assert(t, errors.Is(stream.Err(), ErrOutOfRange))
})
}
})
t.Run("stream starts at future offset, streams records then cancels stream", func(t *testing.T) {
testCases := []struct {
name string
logStart Offset
streamStart Offset
segSize int
writeRecords int
stopAfter int
}{
{
name: "log starts at 0, stream starts at 5, receives 5, cancels stream",
logStart: 0,
streamStart: 5,
segSize: 10,
writeRecords: 10,
stopAfter: 5,
},
{
name: "log starts at 10, stream starts at 18, receives 2, cancels stream",
logStart: 10,
streamStart: 18,
segSize: 10,
writeRecords: 10,
stopAfter: 2,
},
{
name: "log starts at 0, stream starts at 10, receives 30 with purge, cancels stream",
logStart: 0,
streamStart: 10,
segSize: 10,
writeRecords: 50,
stopAfter: 30,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testData := NewTestDataSlice(t, tc.writeRecords)
opts := []Option{
WithStartOffset(tc.logStart),
WithMaxSegmentSize(tc.segSize),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l, err := New(ctx, opts...)
assert.NilError(t, err)
// seed log with initial data
for _, d := range testData[:tc.writeRecords/2] {
_, writeErr := l.Write(ctx, d)
assert.NilError(t, writeErr)
}
go func() {
// write more
for _, d := range testData[tc.writeRecords/2:] {
_, writeErr := l.Write(ctx, d)
validErr := func(err error) bool {
return err == nil || errors.Is(err, context.Canceled)
}
assert.Assert(t, validErr(writeErr))
time.Sleep(time.Millisecond * 50)
}
}()
stream := l.Stream(ctx, tc.streamStart)
counter := 0
for {
if r, ok := stream.Next(); ok {
assert.Equal(t, r.Metadata.Offset, Offset(counter)+tc.streamStart)
counter++
if counter == tc.stopAfter {
cancel()
}
continue
}
break
}
assert.Assert(t, errors.Is(stream.Err(), context.Canceled))
assert.Equal(t, counter, tc.stopAfter)
})
}
})
t.Run("two stream receivers, starting at different offsets until stream cancelled", func(t *testing.T) {
const (
logStart = Offset(0)
segSize = 1000
streamOneStart = Offset(0)
streamTwoStart = Offset(5)
writeRecords = 10
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
opts := []Option{
WithStartOffset(logStart),
WithMaxSegmentSize(segSize),
}
l, err := New(ctx, opts...)
assert.NilError(t, err)
var wg sync.WaitGroup
// writer
wg.Add(1)
go func() {
ticker := time.NewTicker(time.Millisecond * 10) // 100 wps
defer func() {
ticker.Stop()
wg.Done()
}()
counter := 0
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if counter == writeRecords {
return
}
_, writeErr := l.Write(ctx, []byte(`{"id":"someID","message":"write data"}`))
assert.NilError(t, writeErr)
counter++
}
}
}()
// stream reader one
wg.Add(1)
s1Counter := 0
go func() {
defer wg.Done()
stream := l.Stream(ctx, streamOneStart)
for {
r, ok := stream.Next()
if ok {
assert.Equal(t, r.Metadata.Offset, logStart+Offset(s1Counter)+streamOneStart)
s1Counter++
continue
}
break
}
assert.Assert(t, errors.Is(stream.Err(), context.DeadlineExceeded))
}()
// stream reader two
wg.Add(1)
s2Counter := 0
go func() {
defer wg.Done()
stream := l.Stream(ctx, streamTwoStart)
for {
r, ok := stream.Next()
if ok {
assert.Equal(t, r.Metadata.Offset, logStart+Offset(s2Counter)+streamTwoStart)
s2Counter++
continue
}
break
}
assert.Assert(t, errors.Is(stream.Err(), context.DeadlineExceeded))
}()
wg.Wait()
assert.Equal(t, s1Counter, 10)
assert.Equal(t, s2Counter, 5)
})
}