-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbatch_test.go
More file actions
312 lines (266 loc) · 7.98 KB
/
Copy pathbatch_test.go
File metadata and controls
312 lines (266 loc) · 7.98 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
package GoEventBus
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
)
// TestBatch_Basic verifies that a batch handler receives all events published for its projection.
func TestBatch_Basic(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
var got []Event
var mu sync.Mutex
es.RegisterBatch("order.placed", 100, func(_ context.Context, evs []Event) ([]Result, error) {
mu.Lock()
got = append(got, evs...)
mu.Unlock()
return nil, nil
})
for i := 0; i < 5; i++ {
_ = es.Subscribe(bg, Event{Projection: "order.placed"})
}
es.Publish()
mu.Lock()
defer mu.Unlock()
if len(got) != 5 {
t.Fatalf("batch handler received %d events; want 5", len(got))
}
}
// TestBatch_Chunking verifies that events are chunked by the configured size.
func TestBatch_Chunking(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
var calls []int // lengths of each call
var mu sync.Mutex
es.RegisterBatch("tick", 3, func(_ context.Context, evs []Event) ([]Result, error) {
mu.Lock()
calls = append(calls, len(evs))
mu.Unlock()
return nil, nil
})
for i := 0; i < 7; i++ {
_ = es.Subscribe(bg, Event{Projection: "tick"})
}
es.Publish()
mu.Lock()
defer mu.Unlock()
// 7 events with size=3 → chunks of [3, 3, 1]
if len(calls) != 3 {
t.Fatalf("handler called %d times; want 3", len(calls))
}
if calls[0] != 3 || calls[1] != 3 || calls[2] != 1 {
t.Fatalf("chunk sizes %v; want [3 3 1]", calls)
}
}
// TestBatch_FanOut verifies that multiple batch handlers on the same projection
// each receive all events independently.
func TestBatch_FanOut(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
var countA, countB int32
es.RegisterBatch("evt", 100, func(_ context.Context, evs []Event) ([]Result, error) {
atomic.AddInt32(&countA, int32(len(evs)))
return nil, nil
})
es.RegisterBatch("evt", 100, func(_ context.Context, evs []Event) ([]Result, error) {
atomic.AddInt32(&countB, int32(len(evs)))
return nil, nil
})
for i := 0; i < 4; i++ {
_ = es.Subscribe(bg, Event{Projection: "evt"})
}
es.Publish()
if countA != 4 {
t.Errorf("handler A received %d events; want 4", countA)
}
if countB != 4 {
t.Errorf("handler B received %d events; want 4", countB)
}
}
// TestBatch_MixedWithRegular verifies that batch and regular handlers for the
// same projection both fire independently.
func TestBatch_MixedWithRegular(t *testing.T) {
disp := Dispatcher{}
var regularCalls int32
disp.Register("mixed", func(_ context.Context, ev Event) (Result, error) {
atomic.AddInt32(®ularCalls, 1)
return Result{}, nil
})
es := NewEventStore(&disp, 1<<16, DropOldest)
var batchTotal int32
es.RegisterBatch("mixed", 100, func(_ context.Context, evs []Event) ([]Result, error) {
atomic.AddInt32(&batchTotal, int32(len(evs)))
return nil, nil
})
for i := 0; i < 3; i++ {
_ = es.Subscribe(bg, Event{Projection: "mixed"})
}
es.Publish()
if regularCalls != 3 {
t.Errorf("regular handler called %d times; want 3", regularCalls)
}
if batchTotal != 3 {
t.Errorf("batch handler received %d events; want 3", batchTotal)
}
}
// TestBatch_DLQ verifies that a batch handler error sends all events in the
// failing chunk to the dead-letter queue.
func TestBatch_DLQ(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
es.DLQ = NewDeadLetterQueue()
batchErr := errors.New("bulk insert failed")
es.RegisterBatch("write", 100, func(_ context.Context, _ []Event) ([]Result, error) {
return nil, batchErr
})
for i := 0; i < 4; i++ {
_ = es.Subscribe(bg, Event{Projection: "write"})
}
es.Publish()
entries := es.DLQ.Entries()
if len(entries) != 4 {
t.Fatalf("DLQ has %d entries; want 4", len(entries))
}
for _, dl := range entries {
if !errors.Is(dl.Err, batchErr) {
t.Errorf("DLQ entry error = %v; want %v", dl.Err, batchErr)
}
if dl.Attempts != 1 {
t.Errorf("DLQ entry Attempts = %d; want 1", dl.Attempts)
}
}
}
// TestBatch_DLQ_PerChunk verifies that only the failing chunk lands in DLQ
// when events span multiple chunks and one chunk succeeds.
func TestBatch_DLQ_PerChunk(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
es.DLQ = NewDeadLetterQueue()
var calls int32
batchErr := errors.New("second chunk failed")
es.RegisterBatch("write", 2, func(_ context.Context, evs []Event) ([]Result, error) {
n := atomic.AddInt32(&calls, 1)
if n == 2 {
return nil, batchErr
}
return nil, nil
})
for i := 0; i < 4; i++ {
_ = es.Subscribe(bg, Event{Projection: "write"})
}
es.Publish()
// 4 events, size=2 → 2 chunks. First succeeds, second fails → 2 in DLQ.
entries := es.DLQ.Entries()
if len(entries) != 2 {
t.Fatalf("DLQ has %d entries; want 2", len(entries))
}
}
// TestBatch_PanicRecovery verifies that a panicking batch handler is recovered
// and all events in the chunk land in the DLQ.
func TestBatch_PanicRecovery(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
es.DLQ = NewDeadLetterQueue()
es.RegisterBatch("panic", 100, func(_ context.Context, _ []Event) ([]Result, error) {
panic("batch exploded")
})
for i := 0; i < 3; i++ {
_ = es.Subscribe(bg, Event{Projection: "panic"})
}
es.Publish() // must not panic
if es.DLQ.Len() != 3 {
t.Errorf("DLQ has %d entries; want 3", es.DLQ.Len())
}
}
// TestBatch_OnErrorHook verifies that OnError fires once per event in a
// failing batch chunk.
func TestBatch_OnErrorHook(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
var errorFires int32
es.OnError(func(_ context.Context, _ Event, _ error) {
atomic.AddInt32(&errorFires, 1)
})
es.RegisterBatch("failing", 100, func(_ context.Context, _ []Event) ([]Result, error) {
return nil, errors.New("fail")
})
for i := 0; i < 5; i++ {
_ = es.Subscribe(bg, Event{Projection: "failing"})
}
es.Publish()
if errorFires != 5 {
t.Errorf("OnError fired %d times; want 5", errorFires)
}
}
// TestBatch_Metrics verifies that processedCount is incremented per event, not
// per batch call.
func TestBatch_Metrics(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
es.RegisterBatch("m", 100, func(_ context.Context, _ []Event) ([]Result, error) {
return nil, nil
})
for i := 0; i < 6; i++ {
_ = es.Subscribe(bg, Event{Projection: "m"})
}
es.Publish()
_, processed, _ := es.Metrics()
if processed != 6 {
t.Errorf("processed = %d; want 6", processed)
}
}
// TestBatch_Async verifies batch handlers work correctly in async mode.
func TestBatch_Async(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
es.Async = true
var received int32
es.RegisterBatch("async", 3, func(_ context.Context, evs []Event) ([]Result, error) {
atomic.AddInt32(&received, int32(len(evs)))
return nil, nil
})
for i := 0; i < 9; i++ {
_ = es.Subscribe(bg, Event{Projection: "async"})
}
es.Publish()
_ = es.Drain(context.Background())
if received != 9 {
t.Errorf("async batch received %d events; want 9", received)
}
}
// TestBatch_Results verifies that per-event Results returned by the handler are
// forwarded to OnAfter hooks.
func TestBatch_Results(t *testing.T) {
disp := Dispatcher{}
es := NewEventStore(&disp, 1<<16, DropOldest)
es.RegisterBatch("res", 100, func(_ context.Context, evs []Event) ([]Result, error) {
out := make([]Result, len(evs))
for i := range out {
out[i] = Result{Message: "ok"}
}
return out, nil
})
var afterMessages []string
var mu sync.Mutex
es.OnAfter(func(_ context.Context, _ Event, r Result, _ error) {
mu.Lock()
afterMessages = append(afterMessages, r.Message)
mu.Unlock()
})
for i := 0; i < 3; i++ {
_ = es.Subscribe(bg, Event{Projection: "res"})
}
es.Publish()
mu.Lock()
defer mu.Unlock()
if len(afterMessages) != 3 {
t.Fatalf("OnAfter fired %d times; want 3", len(afterMessages))
}
for _, msg := range afterMessages {
if msg != "ok" {
t.Errorf("OnAfter result.Message = %q; want \"ok\"", msg)
}
}
}