-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic_test.go
More file actions
365 lines (306 loc) · 8.42 KB
/
Copy pathtopic_test.go
File metadata and controls
365 lines (306 loc) · 8.42 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
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
package topic_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"lowbit.dev/topic"
)
var ctx = context.Background()
// --- Publish ---
func TestPublish_NoSubscribers(t *testing.T) {
tp := topic.New[int]()
if err := tp.Publish(ctx, 1); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
func TestPublish_SingleSubscriber(t *testing.T) {
tp := topic.New[int]()
var got int
tp.Subscribe(func(_ context.Context, v int) error {
got = v
return nil
})
if err := tp.Publish(ctx, 42); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != 42 {
t.Fatalf("expected 42, got %d", got)
}
}
func TestPublish_FanoutAllHandlersRun(t *testing.T) {
tp := topic.New[int]()
var calls int
for range 5 {
tp.Subscribe(func(_ context.Context, _ int) error {
calls++
return nil
})
}
if err := tp.Publish(ctx, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if calls != 5 {
t.Fatalf("expected 5 calls, got %d", calls)
}
}
func TestPublish_AllHandlersRunEvenIfOneFails(t *testing.T) {
tp := topic.New[int]()
var calls int
sentinel := errors.New("boom")
tp.Subscribe(func(_ context.Context, _ int) error { calls++; return sentinel })
tp.Subscribe(func(_ context.Context, _ int) error { calls++; return nil })
tp.Subscribe(func(_ context.Context, _ int) error { calls++; return sentinel })
err := tp.Publish(ctx, 0)
if calls != 3 {
t.Fatalf("expected all 3 handlers to run, got %d", calls)
}
if err == nil {
t.Fatal("expected joined error, got nil")
}
if !errors.Is(err, sentinel) {
t.Fatalf("expected sentinel in error, got %v", err)
}
}
func TestPublish_ErrorsAreJoined(t *testing.T) {
tp := topic.New[int]()
errA := errors.New("A")
errB := errors.New("B")
tp.Subscribe(func(_ context.Context, _ int) error { return errA })
tp.Subscribe(func(_ context.Context, _ int) error { return errB })
err := tp.Publish(ctx, 0)
if !errors.Is(err, errA) {
t.Errorf("expected errA in joined error, got %v", err)
}
if !errors.Is(err, errB) {
t.Errorf("expected errB in joined error, got %v", err)
}
}
func TestPublish_EventIsPassedCorrectly(t *testing.T) {
type payload struct{ Value string }
tp := topic.New[payload]()
var received payload
tp.Subscribe(func(_ context.Context, p payload) error {
received = p
return nil
})
tp.Publish(ctx, payload{Value: "hello"})
if received.Value != "hello" {
t.Fatalf("expected 'hello', got %q", received.Value)
}
}
func TestPublish_ContextPassedToHandler(t *testing.T) {
type key struct{}
tp := topic.New[int]()
var got any
tp.Subscribe(func(ctx context.Context, _ int) error {
got = ctx.Value(key{})
return nil
})
ctx := context.WithValue(context.Background(), key{}, "marker")
tp.Publish(ctx, 0)
if got != "marker" {
t.Fatalf("expected context value to propagate, got %v", got)
}
}
// --- Subscribe / Unsubscribe ---
func TestSubscribe_CancelRemovesHandler(t *testing.T) {
tp := topic.New[int]()
var calls int
cancel := tp.Subscribe(func(_ context.Context, _ int) error {
calls++
return nil
})
tp.Publish(ctx, 0)
cancel()
tp.Publish(ctx, 0)
if calls != 1 {
t.Fatalf("expected 1 call after cancel, got %d", calls)
}
}
func TestSubscribe_CancelIsIdempotent(t *testing.T) {
tp := topic.New[int]()
cancel := tp.Subscribe(func(_ context.Context, _ int) error { return nil })
// Calling cancel multiple times must not panic.
cancel()
cancel()
cancel()
}
func TestSubscribe_CancelOnlyRemovesOwnHandler(t *testing.T) {
tp := topic.New[int]()
var aRan, bRan bool
cancelA := tp.Subscribe(func(_ context.Context, _ int) error { aRan = true; return nil })
tp.Subscribe(func(_ context.Context, _ int) error { bRan = true; return nil })
cancelA()
tp.Publish(ctx, 0)
if aRan {
t.Error("handler A should not run after cancel")
}
if !bRan {
t.Error("handler B should still run")
}
}
func TestSubscribe_DispatchOrderIsSubscriptionOrder(t *testing.T) {
tp := topic.New[int]()
var order []int
tp.Subscribe(func(_ context.Context, _ int) error { order = append(order, 1); return nil })
tp.Subscribe(func(_ context.Context, _ int) error { order = append(order, 2); return nil })
tp.Subscribe(func(_ context.Context, _ int) error { order = append(order, 3); return nil })
tp.Publish(ctx, 0)
for i, v := range order {
if v != i+1 {
t.Fatalf("expected order [1 2 3], got %v", order)
}
}
}
// --- Concurrency ---
func TestPublish_ConcurrentPublishersDoNotRace(t *testing.T) {
tp := topic.New[int]()
tp.Subscribe(func(_ context.Context, _ int) error { return nil })
var wg sync.WaitGroup
for range 100 {
wg.Add(1)
go func() {
defer wg.Done()
tp.Publish(ctx, 1)
}()
}
wg.Wait()
}
func TestSubscribe_ConcurrentSubscribePublishDoNotRace(t *testing.T) {
tp := topic.New[int]()
var wg sync.WaitGroup
for range 50 {
wg.Add(1)
go func() {
defer wg.Done()
cancel := tp.Subscribe(func(_ context.Context, _ int) error { return nil })
tp.Publish(ctx, 0)
cancel()
}()
}
wg.Wait()
}
func TestSubscribe_SnapshotIsolation(t *testing.T) {
// A handler that subscribes during its own dispatch must not affect
// the in-flight snapshot — the newly registered handler should not
// run for the current event.
tp := topic.New[int]()
var secondCalls int
tp.Subscribe(func(_ context.Context, _ int) error {
tp.Subscribe(func(_ context.Context, _ int) error {
secondCalls++
return nil
})
return nil
})
tp.Publish(ctx, 0)
if secondCalls != 0 {
t.Fatalf("handler subscribed during dispatch should not run for current event, got %d calls", secondCalls)
}
}
// --- WithRetry ---
func TestWithRetry_SucceedsFirstAttempt(t *testing.T) {
tp := topic.New[int]()
var calls int
tp.Subscribe(func(_ context.Context, _ int) error {
calls++
return nil
}, tp.WithRetry(3, time.Millisecond))
if err := tp.Publish(ctx, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if calls != 1 {
t.Fatalf("expected 1 call, got %d", calls)
}
}
func TestWithRetry_RetriesOnFailure(t *testing.T) {
tp := topic.New[int]()
var calls int
tp.Subscribe(func(_ context.Context, _ int) error {
calls++
if calls < 3 {
return errors.New("transient")
}
return nil
}, tp.WithRetry(5, time.Millisecond))
if err := tp.Publish(ctx, 0); err != nil {
t.Fatalf("unexpected error after eventual success: %v", err)
}
if calls != 3 {
t.Fatalf("expected 3 calls, got %d", calls)
}
}
func TestWithRetry_ExhaustsAndReturnsLastError(t *testing.T) {
tp := topic.New[int]()
sentinel := errors.New("permanent")
tp.Subscribe(func(_ context.Context, _ int) error {
return sentinel
}, tp.WithRetry(3, time.Millisecond))
err := tp.Publish(ctx, 0)
if !errors.Is(err, sentinel) {
t.Fatalf("expected sentinel error, got %v", err)
}
}
func TestWithRetry_RespectsContextCancellation(t *testing.T) {
tp := topic.New[int]()
tp.Subscribe(func(_ context.Context, _ int) error {
return errors.New("fail")
}, tp.WithRetry(10, 100*time.Millisecond))
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
start := time.Now()
err := tp.Publish(ctx, 0)
elapsed := time.Since(start)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected DeadlineExceeded, got %v", err)
}
// Should have exited well before all 10 retries × 100ms = 1s.
if elapsed > 200*time.Millisecond {
t.Fatalf("WithRetry did not respect context cancellation, took %v", elapsed)
}
}
// --- WithRecovery ---
func TestWithRecovery_CatchesPanic(t *testing.T) {
tp := topic.New[int]()
tp.Subscribe(func(_ context.Context, _ int) error {
panic("explosion")
}, tp.WithRecovery())
err := tp.Publish(ctx, 0)
if err == nil {
t.Fatal("expected error from recovered panic, got nil")
}
if err.Error() != "handler panic recovered: explosion" {
t.Fatalf("unexpected error message: %v", err)
}
}
func TestWithRecovery_DoesNotAffectSuccessfulHandler(t *testing.T) {
tp := topic.New[int]()
var called bool
tp.Subscribe(func(_ context.Context, _ int) error {
called = true
return nil
}, tp.WithRecovery())
if err := tp.Publish(ctx, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !called {
t.Fatal("handler was not called")
}
}
func TestWithRecovery_OtherHandlersRunAfterPanic(t *testing.T) {
tp := topic.New[int]()
var afterRan bool
tp.Subscribe(func(_ context.Context, _ int) error {
panic("boom")
}, tp.WithRecovery())
tp.Subscribe(func(_ context.Context, _ int) error {
afterRan = true
return nil
})
tp.Publish(ctx, 0)
if !afterRan {
t.Fatal("handler after panicking handler should still run")
}
}