-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmap_broker_readstream_test.go
More file actions
340 lines (315 loc) · 9.3 KB
/
Copy pathmap_broker_readstream_test.go
File metadata and controls
340 lines (315 loc) · 9.3 KB
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
package centrifuge
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type mapBrokerFactory func(t *testing.T) MapBroker
func testMapBrokerReadStream(t *testing.T, factory mapBrokerFactory) {
ctx := context.Background()
publishOpts := func(streamSize int) MapPublishOptions {
_ = streamSize // streamSize now configured via MapChannelOptions
return MapPublishOptions{}
}
removeOpts := func(streamSize int) MapRemoveOptions {
_ = streamSize // streamSize now configured via MapChannelOptions
return MapRemoveOptions{}
}
// publishN publishes n messages with keys "key_1".."key_n" and data "data_1".."data_n".
publishN := func(t *testing.T, broker MapBroker, ch string, n int, streamSize int) string {
t.Helper()
var epoch string
for i := 1; i <= n; i++ {
opts := publishOpts(streamSize)
opts.Data = []byte(fmt.Sprintf("data_%d", i))
res, err := broker.Publish(ctx, ch, fmt.Sprintf("key_%d", i), opts)
require.NoError(t, err)
epoch = res.Position.Epoch
}
return epoch
}
t.Run("empty_channel", func(t *testing.T) {
tests := []struct {
name string
filter StreamFilter
}{
{
name: "read_nonexistent",
filter: StreamFilter{Limit: -1},
},
{
name: "metadata_only",
filter: StreamFilter{Limit: 0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
broker := factory(t)
ch := fmt.Sprintf("empty_%s_%d", tt.name, time.Now().UnixNano())
result, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: tt.filter,
})
require.NoError(t, err)
require.Empty(t, result.Publications)
require.Equal(t, uint64(0), result.Position.Offset)
})
}
})
t.Run("forward_recovery", func(t *testing.T) {
broker := factory(t)
ch := fmt.Sprintf("forward_%d", time.Now().UnixNano())
epoch := publishN(t, broker, ch, 5, 100)
tests := []struct {
name string
filter StreamFilter
wantCount int
wantFirst string // expected data of first pub, empty if no pubs expected
wantLast string // expected data of last pub
}{
{
name: "all_no_since",
filter: StreamFilter{Limit: -1},
wantCount: 5,
wantFirst: "data_1",
wantLast: "data_5",
},
{
name: "limit_2_no_since",
filter: StreamFilter{Limit: 2},
wantCount: 2,
wantFirst: "data_1",
wantLast: "data_2",
},
{
name: "metadata_only",
filter: StreamFilter{Limit: 0},
wantCount: 0,
},
{
name: "since_0_all",
filter: StreamFilter{
Since: &StreamPosition{Offset: 0, Epoch: epoch},
Limit: -1,
},
wantCount: 5,
wantFirst: "data_1",
wantLast: "data_5",
},
{
name: "since_2_partial",
filter: StreamFilter{
Since: &StreamPosition{Offset: 2, Epoch: epoch},
Limit: -1,
},
wantCount: 3,
wantFirst: "data_3",
wantLast: "data_5",
},
{
name: "since_2_limit_2",
filter: StreamFilter{
Since: &StreamPosition{Offset: 2, Epoch: epoch},
Limit: 2,
},
wantCount: 2,
wantFirst: "data_3",
wantLast: "data_4",
},
{
name: "since_top_caught_up",
filter: StreamFilter{
Since: &StreamPosition{Offset: 5, Epoch: epoch},
Limit: -1,
},
wantCount: 0,
},
{
name: "since_beyond_top",
filter: StreamFilter{
Since: &StreamPosition{Offset: 10, Epoch: epoch},
Limit: -1,
},
wantCount: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: tt.filter,
})
require.NoError(t, err)
require.Len(t, result.Publications, tt.wantCount)
require.Equal(t, uint64(5), result.Position.Offset)
require.Equal(t, epoch, result.Position.Epoch)
if tt.wantCount > 0 {
require.Equal(t, []byte(tt.wantFirst), result.Publications[0].Data)
require.Equal(t, []byte(tt.wantLast), result.Publications[tt.wantCount-1].Data)
}
})
}
})
t.Run("reverse_reads", func(t *testing.T) {
broker := factory(t)
ch := fmt.Sprintf("reverse_%d", time.Now().UnixNano())
epoch := publishN(t, broker, ch, 5, 100)
tests := []struct {
name string
filter StreamFilter
wantCount int
wantFirst string
wantLast string
}{
{
name: "all_reverse",
filter: StreamFilter{Limit: -1, Reverse: true},
wantCount: 5,
wantFirst: "data_5",
wantLast: "data_1",
},
{
name: "limit_2_reverse",
filter: StreamFilter{Limit: 2, Reverse: true},
wantCount: 2,
wantFirst: "data_5",
wantLast: "data_4",
},
{
name: "since_top_reverse",
filter: StreamFilter{
Since: &StreamPosition{Offset: 5, Epoch: epoch},
Limit: -1,
Reverse: true,
},
wantCount: 4,
wantFirst: "data_4",
wantLast: "data_1",
},
{
name: "since_3_reverse_limit_2",
filter: StreamFilter{
Since: &StreamPosition{Offset: 3, Epoch: epoch},
Limit: 2,
Reverse: true,
},
wantCount: 2,
wantFirst: "data_2",
wantLast: "data_1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: tt.filter,
})
require.NoError(t, err)
require.Len(t, result.Publications, tt.wantCount)
require.Equal(t, uint64(5), result.Position.Offset)
require.Equal(t, epoch, result.Position.Epoch)
if tt.wantCount > 0 {
require.Equal(t, []byte(tt.wantFirst), result.Publications[0].Data)
require.Equal(t, []byte(tt.wantLast), result.Publications[tt.wantCount-1].Data)
}
})
}
})
t.Run("with_removals", func(t *testing.T) {
broker := factory(t)
ch := fmt.Sprintf("removals_%d", time.Now().UnixNano())
// Publish 3 keyed entries.
for i := 1; i <= 3; i++ {
opts := publishOpts(100)
opts.Data = []byte(fmt.Sprintf("data_%d", i))
_, err := broker.Publish(ctx, ch, fmt.Sprintf("key_%d", i), opts)
require.NoError(t, err)
}
// Remove key_2 — this should produce a removal event at offset 4.
_, err := broker.Remove(ctx, ch, "key_2", removeOpts(100))
require.NoError(t, err)
// Read all — should have 4 entries (3 adds + 1 removal).
result, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: StreamFilter{Limit: -1},
})
require.NoError(t, err)
require.Len(t, result.Publications, 4)
require.Equal(t, uint64(4), result.Position.Offset)
// Last entry should be the removal.
removal := result.Publications[3]
require.True(t, removal.Removed)
require.Equal(t, "key_2", removal.Key)
// Since offset 3 → only the removal event.
result2, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: StreamFilter{
Since: &StreamPosition{Offset: 3, Epoch: result.Position.Epoch},
Limit: -1,
},
})
require.NoError(t, err)
require.Len(t, result2.Publications, 1)
require.True(t, result2.Publications[0].Removed)
require.Equal(t, "key_2", result2.Publications[0].Key)
})
t.Run("stream_trimming", func(t *testing.T) {
broker := factory(t)
ch := fmt.Sprintf("trimming_%d", time.Now().UnixNano())
// Publish 5 messages with StreamSize=3 to force trimming.
var epoch string
for i := 1; i <= 5; i++ {
opts := publishOpts(3)
opts.Data = []byte(fmt.Sprintf("data_%d", i))
res, err := broker.Publish(ctx, ch, fmt.Sprintf("key_%d", i), opts)
require.NoError(t, err)
epoch = res.Position.Epoch
}
// Read all available — should be trimmed to ~3 entries.
result, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: StreamFilter{
Since: &StreamPosition{Offset: 0, Epoch: epoch},
Limit: -1,
},
})
require.NoError(t, err)
require.Equal(t, uint64(5), result.Position.Offset)
require.Equal(t, epoch, result.Position.Epoch)
// After trimming, we should have at most streamSize entries for exact MAXLEN.
// With approximate MAXLEN (~), Redis may keep more entries (only trims at
// macro node boundaries), so for small streams all entries may be retained.
// Memory broker uses exact trimming, so it will have exactly 3.
require.GreaterOrEqual(t, len(result.Publications), 3)
// The last available entry should be offset 5 (data_5).
last := result.Publications[len(result.Publications)-1]
require.Equal(t, []byte("data_5"), last.Data)
})
t.Run("single_publication", func(t *testing.T) {
broker := factory(t)
ch := fmt.Sprintf("single_%d", time.Now().UnixNano())
opts := publishOpts(100)
opts.Data = []byte("only_one")
res, err := broker.Publish(ctx, ch, "key_1", opts)
require.NoError(t, err)
epoch := res.Position.Epoch
// Since offset 0 → 1 pub.
result, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: StreamFilter{
Since: &StreamPosition{Offset: 0, Epoch: epoch},
Limit: -1,
},
})
require.NoError(t, err)
require.Len(t, result.Publications, 1)
require.Equal(t, []byte("only_one"), result.Publications[0].Data)
require.Equal(t, uint64(1), result.Position.Offset)
// Since offset 1 (at top) → no pubs, caught up.
result2, err := broker.ReadStream(ctx, ch, MapReadStreamOptions{
Filter: StreamFilter{
Since: &StreamPosition{Offset: 1, Epoch: epoch},
Limit: -1,
},
})
require.NoError(t, err)
require.Empty(t, result2.Publications)
require.Equal(t, uint64(1), result2.Position.Offset)
require.Equal(t, epoch, result2.Position.Epoch)
})
}