-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathchannel_medium_test.go
More file actions
607 lines (553 loc) · 19.5 KB
/
Copy pathchannel_medium_test.go
File metadata and controls
607 lines (553 loc) · 19.5 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package centrifuge
import (
"errors"
"math"
"runtime"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// Helper function to create a channelMedium with options.
func setupChannelMedium(t testing.TB, options ChannelMediumOptions, node nodeSubset) *channelMedium {
t.Helper()
channel := "testChannel"
cache, err := newChannelMedium(channel, node, options)
if err != nil {
require.NoError(t, err)
}
return cache
}
type mockNode struct {
handlePublicationFunc func(channel string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error
streamTopFunc func(ch string, historyMetaTTL time.Duration) (StreamPosition, error)
mapStreamTopFunc func(ch string) (StreamPosition, error)
}
func (m *mockNode) handlePublication(channel string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error {
if m.handlePublicationFunc != nil {
return m.handlePublicationFunc(channel, sp, pub, prevPub, localPrevPub)
}
return nil
}
func (m *mockNode) streamTop(ch string, historyMetaTTL time.Duration) (StreamPosition, error) {
if m.streamTopFunc != nil {
return m.streamTopFunc(ch, historyMetaTTL)
}
return StreamPosition{}, nil
}
func (m *mockNode) mapStreamTop(ch string) (StreamPosition, error) {
if m.mapStreamTopFunc != nil {
return m.mapStreamTopFunc(ch)
}
return StreamPosition{}, nil
}
func TestChannelMediumHandlePublication(t *testing.T) {
t.Parallel()
var testCases = []struct {
numPublications int
options ChannelMediumOptions
}{
{
numPublications: 10,
options: ChannelMediumOptions{
enableQueue: false,
KeepLatestPublication: false,
},
},
{
numPublications: 10,
options: ChannelMediumOptions{
enableQueue: true,
KeepLatestPublication: false,
},
},
{
numPublications: 1,
options: ChannelMediumOptions{
enableQueue: true,
KeepLatestPublication: false,
broadcastDelay: 10 * time.Millisecond,
},
},
{
numPublications: 1,
options: ChannelMediumOptions{
enableQueue: true,
KeepLatestPublication: true,
broadcastDelay: 10 * time.Millisecond,
},
},
}
for i, tt := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
numPublications := tt.numPublications
doneCh := make(chan struct{}, numPublications)
cache := setupChannelMedium(t, tt.options, &mockNode{
handlePublicationFunc: func(channel string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error {
doneCh <- struct{}{}
return nil
},
})
pub := &Publication{Data: []byte("test data")}
sp := StreamPosition{Offset: 1}
for i := 0; i < numPublications; i++ {
cache.broadcastPublication(pub, sp, false, nil)
}
for i := 0; i < numPublications; i++ {
select {
case <-doneCh:
case <-time.After(5 * time.Second):
require.Fail(t, "handlePublicationFunc was not called")
}
}
})
}
}
func TestChannelMediumInsufficientState(t *testing.T) {
t.Parallel()
options := ChannelMediumOptions{
enableQueue: true,
KeepLatestPublication: true,
}
doneCh := make(chan struct{})
medium := setupChannelMedium(t, options, &mockNode{
handlePublicationFunc: func(channel string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error {
require.Equal(t, uint64(math.MaxUint64), pub.Offset)
require.Equal(t, uint64(math.MaxUint64), sp.Offset)
close(doneCh)
return nil
},
})
// Simulate the behavior when the state is marked as insufficient
medium.broadcastInsufficientState()
select {
case <-doneCh:
case <-time.After(5 * time.Second):
require.Fail(t, "handlePublicationFunc was not called")
}
}
func TestChannelMediumPositionSync(t *testing.T) {
t.Parallel()
options := ChannelMediumOptions{
SharedPositionSync: true,
}
doneCh := make(chan struct{})
var closeOnce sync.Once
medium := setupChannelMedium(t, options, &mockNode{
streamTopFunc: func(ch string, historyMetaTTL time.Duration) (StreamPosition, error) {
closeOnce.Do(func() {
close(doneCh)
})
return StreamPosition{}, nil
},
})
medium.nowFn = func() time.Time { return time.Now().Add(time.Hour) }
medium.CheckPosition(time.Second, StreamPosition{Offset: 1, Epoch: "test"}, time.Second)
select {
case <-doneCh:
case <-time.After(5 * time.Second):
require.Fail(t, "historyFunc was not called")
}
}
func TestChannelMediumPositionSyncRetry(t *testing.T) {
t.Parallel()
options := ChannelMediumOptions{
SharedPositionSync: true,
}
doneCh := make(chan struct{})
var closeOnce sync.Once
numCalls := 0
medium := setupChannelMedium(t, options, &mockNode{
streamTopFunc: func(ch string, historyMetaTTL time.Duration) (StreamPosition, error) {
if numCalls == 0 {
numCalls++
return StreamPosition{}, errors.New("boom")
}
closeOnce.Do(func() {
close(doneCh)
})
return StreamPosition{}, nil
},
})
medium.nowFn = func() time.Time { return time.Now().Add(time.Hour) }
medium.CheckPosition(time.Second, StreamPosition{Offset: 1, Epoch: "test"}, time.Second)
select {
case <-doneCh:
case <-time.After(5 * time.Second):
require.Fail(t, "streamTopLatestPubFunc was not called")
}
}
func TestChannelMediumPositionSyncMap(t *testing.T) {
t.Parallel()
// Verify that SharedPositionSync for map channels calls mapStreamTop (not streamTop).
options := ChannelMediumOptions{
SharedPositionSync: true,
}
mapStreamTopCalled := make(chan struct{})
var closeOnce sync.Once
medium := setupChannelMedium(t, options, &mockNode{
streamTopFunc: func(ch string, historyMetaTTL time.Duration) (StreamPosition, error) {
require.Fail(t, "streamTop should not be called for map channel")
return StreamPosition{}, nil
},
mapStreamTopFunc: func(ch string) (StreamPosition, error) {
closeOnce.Do(func() {
close(mapStreamTopCalled)
})
return StreamPosition{Offset: 5, Epoch: "abc"}, nil
},
})
medium.isMap = true
medium.nowFn = func() time.Time { return time.Now().Add(time.Hour) }
// Position matches — should return true.
valid := medium.CheckPosition(time.Second, StreamPosition{Offset: 5, Epoch: "abc"}, time.Second)
require.True(t, valid)
select {
case <-mapStreamTopCalled:
case <-time.After(5 * time.Second):
require.Fail(t, "mapStreamTopFunc was not called")
}
}
func TestChannelMediumPositionSyncMapMismatch(t *testing.T) {
t.Parallel()
// Verify that SharedPositionSync for map channels detects position mismatch.
options := ChannelMediumOptions{
SharedPositionSync: true,
enableQueue: true,
}
insufficientCh := make(chan struct{})
var closeOnce sync.Once
medium := setupChannelMedium(t, options, &mockNode{
handlePublicationFunc: func(channel string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error {
if pub.Offset == math.MaxUint64 {
closeOnce.Do(func() {
close(insufficientCh)
})
}
return nil
},
mapStreamTopFunc: func(ch string) (StreamPosition, error) {
return StreamPosition{Offset: 10, Epoch: "xyz"}, nil
},
})
medium.isMap = true
medium.nowFn = func() time.Time { return time.Now().Add(time.Hour) }
// Client has stale position — should return false.
valid := medium.CheckPosition(time.Second, StreamPosition{Offset: 5, Epoch: "xyz"}, time.Second)
require.False(t, valid)
select {
case <-insufficientCh:
case <-time.After(5 * time.Second):
require.Fail(t, "insufficient state was not broadcast")
}
}
func TestChannelMediumNoLocalPrevPubForMapSubs(t *testing.T) {
t.Parallel()
// Test that channel medium does NOT provide localPrevPub for map publications
// (pub.Key != ""). Map keys are independent streams — a single latestPublication
// can't serve as a correct delta base across different keys. Only non-map
// publications get medium-level localPrevPub tracking.
type broadcastEvent struct {
pub *Publication
localPrevPub *Publication
}
var mu sync.Mutex
var events []broadcastEvent
options := ChannelMediumOptions{
KeepLatestPublication: true,
}
medium := setupChannelMedium(t, options, &mockNode{
handlePublicationFunc: func(channel string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error {
mu.Lock()
events = append(events, broadcastEvent{pub: pub, localPrevPub: localPrevPub})
mu.Unlock()
return nil
},
})
// Map publications always get nil localPrevPub.
pubA1 := &Publication{Data: []byte("a1"), Key: "a"}
medium.broadcastPublication(pubA1, StreamPosition{Offset: 1}, true, nil)
pubA2 := &Publication{Data: []byte("a2"), Key: "a"}
medium.broadcastPublication(pubA2, StreamPosition{Offset: 2}, true, nil)
pubB1 := &Publication{Data: []byte("b1"), Key: "b"}
medium.broadcastPublication(pubB1, StreamPosition{Offset: 3}, true, nil)
mu.Lock()
require.Len(t, events, 3)
require.Nil(t, events[0].localPrevPub, "map pub must not get localPrevPub")
require.Nil(t, events[1].localPrevPub, "map pub same key must not get localPrevPub")
require.Nil(t, events[2].localPrevPub, "map pub different key must not get localPrevPub")
mu.Unlock()
// Map publications must not pollute latestPublication used by non-map pubs.
pubNoKey1 := &Publication{Data: []byte("nokey1")}
medium.broadcastPublication(pubNoKey1, StreamPosition{Offset: 4}, true, nil)
mu.Lock()
require.Len(t, events, 4)
require.Nil(t, events[3].localPrevPub, "first non-map pub should have nil localPrevPub")
mu.Unlock()
pubNoKey2 := &Publication{Data: []byte("nokey2")}
medium.broadcastPublication(pubNoKey2, StreamPosition{Offset: 5}, true, nil)
mu.Lock()
require.Len(t, events, 5)
require.NotNil(t, events[4].localPrevPub)
require.Equal(t, []byte("nokey1"), events[4].localPrevPub.Data)
mu.Unlock()
}
// TestPublicationQueueCloseAndClosed covers the queue lifecycle helpers used by
// channel medium teardown.
func TestPublicationQueueCloseAndClosed(t *testing.T) {
t.Parallel()
q := newPublicationQueue(2)
require.False(t, q.Closed())
require.True(t, q.Add(queuedPublication{}))
q.Close()
require.True(t, q.Closed())
// Adding after Close must report failure and not panic.
require.False(t, q.Add(queuedPublication{}))
// Wait must report false on a closed queue without blocking.
require.False(t, q.Wait())
}
// TestChannelMediumClose verifies the medium close helper signals waiters via closeCh.
func TestChannelMediumClose(t *testing.T) {
t.Parallel()
medium, err := newChannelMedium("test-close", &mockNode{}, ChannelMediumOptions{})
require.NoError(t, err)
medium.close()
select {
case <-medium.closeCh:
default:
t.Fatal("close did not signal closeCh")
}
}
// TestChannelMediumCloseStopsWriterGoroutine asserts that when a queue-enabled
// channelMedium is closed, its writer goroutine actually exits — not just that
// closeCh is signalled.
//
// Bug: medium.close() closes c.closeCh but never closes c.messages. The
// writer goroutine sits in c.messages.Wait() (sync.Cond) on an empty queue,
// and Wait only returns when the queue itself is closed (which broadcasts the
// cond). closeCh is checked only INSIDE the broadcastDelay timer branch,
// which is unreachable until Wait returns. Result: every channelMedium that
// is closed with an empty queue leaks one writer goroutine — and each
// channelMedium is created per channel (addSubscription) and closed when the
// last subscriber leaves (removeSubscription), so this leaks one goroutine
// per channel lifecycle on any server using queue/delay channel-medium
// options.
//
// The test detects the leak by creating many mediums, closing them, and
// checking the goroutine count returns to baseline (with a small allowance
// for noise from runtime). Not t.Parallel because runtime.NumGoroutine is
// process-global.
func TestChannelMediumCloseStopsWriterGoroutine(t *testing.T) {
const numMediums = 50
// Settle to a stable baseline.
time.Sleep(20 * time.Millisecond)
before := runtime.NumGoroutine()
mediums := make([]*channelMedium, 0, numMediums)
for i := 0; i < numMediums; i++ {
m, err := newChannelMedium("test-close-writer-"+strconv.Itoa(i), &mockNode{}, ChannelMediumOptions{
enableQueue: true,
})
require.NoError(t, err)
mediums = append(mediums, m)
}
// Writer goroutines are scheduled; goroutine count should be well above baseline.
require.Eventually(t, func() bool {
return runtime.NumGoroutine() >= before+numMediums
}, time.Second, 10*time.Millisecond, "writer goroutines never started (got %d, expected ≥ %d)", runtime.NumGoroutine(), before+numMediums)
for _, m := range mediums {
m.close()
}
// All writer goroutines must exit. With the bug they sit in
// publicationQueue.Wait() on empty queues. Allow some slack (5) for
// unrelated runtime goroutines that may come and go.
require.Eventually(t, func() bool {
return runtime.NumGoroutine() <= before+5
}, 2*time.Second, 20*time.Millisecond,
"writer goroutines did not exit after close (got %d, baseline %d) — medium.close() does not close the queue, so writers are stuck in messages.Wait()", runtime.NumGoroutine(), before)
}
// TestNewChannelMediumBroadcastDelayWithoutQueueRejected ensures the constructor
// rejects an invalid options combination (broadcastDelay > 0 requires queue).
func TestNewChannelMediumBroadcastDelayWithoutQueueRejected(t *testing.T) {
t.Parallel()
_, err := newChannelMedium("ch", &mockNode{}, ChannelMediumOptions{
broadcastDelay: 10 * time.Millisecond,
})
require.Error(t, err)
require.Contains(t, err.Error(), "broadcast delay")
}
// TestChannelMediumQueuedBroadcast verifies the queued broadcast path:
// publications enqueued via broadcastPublication are delivered through the
// medium's writer goroutine to the underlying node's handlePublication.
func TestChannelMediumQueuedBroadcast(t *testing.T) {
t.Parallel()
delivered := make(chan *Publication, 1)
node := &mockNode{
handlePublicationFunc: func(_ string, _ StreamPosition, pub, _, _ *Publication) error {
delivered <- pub
return nil
},
}
medium, err := newChannelMedium("ch", node, ChannelMediumOptions{
enableQueue: true,
})
require.NoError(t, err)
pub := &Publication{Data: []byte(`{"value":"queued"}`), Offset: 1}
medium.broadcastPublication(pub, StreamPosition{Offset: 1, Epoch: "e"}, false, nil)
select {
case got := <-delivered:
require.Equal(t, pub.Data, got.Data)
case <-time.After(time.Second):
t.Fatal("publication not delivered through medium queue")
}
medium.close()
}
// TestChannelMediumCheckPositionAndInsufficientState exercises the position-check
// flow including the insufficient-state broadcast path. The mock node returns a
// stream top that does NOT match the client's position, forcing
// broadcastInsufficientState to fire.
func TestChannelMediumCheckPositionAndInsufficientState(t *testing.T) {
t.Parallel()
delivered := make(chan *Publication, 4)
node := &mockNode{
streamTopFunc: func(string, time.Duration) (StreamPosition, error) {
return StreamPosition{Offset: 99, Epoch: "e1"}, nil
},
mapStreamTopFunc: func(string) (StreamPosition, error) {
return StreamPosition{Offset: 99, Epoch: "e1"}, nil
},
handlePublicationFunc: func(_ string, _ StreamPosition, pub, _, _ *Publication) error {
delivered <- pub
return nil
},
}
medium, err := newChannelMedium("pos-check", node, ChannelMediumOptions{
SharedPositionSync: true,
})
require.NoError(t, err)
defer medium.close()
// Mismatch: client is at offset 5, stream top at 99. Position is invalid,
// medium must broadcast insufficient state.
clientPos := StreamPosition{Offset: 5, Epoch: "e1"}
valid := medium.CheckPosition(time.Minute, clientPos, 0)
require.False(t, valid)
// One publication delivered with the magic insufficient-state sentinel offset.
select {
case got := <-delivered:
require.Equal(t, uint64(0xFFFFFFFFFFFFFFFF), got.Offset)
case <-time.After(time.Second):
t.Fatal("insufficient state broadcast not delivered")
}
// Calling again immediately should skip the check (delay not elapsed).
valid = medium.CheckPosition(time.Minute, clientPos, time.Hour)
require.True(t, valid)
}
// TestChannelMediumOptionsIsMediumEnabled exercises isMediumEnabled across the
// enable conditions to keep the table-driven check covered.
func TestChannelMediumOptionsIsMediumEnabled(t *testing.T) {
t.Parallel()
require.False(t, (ChannelMediumOptions{}).isMediumEnabled())
require.True(t, (ChannelMediumOptions{SharedPositionSync: true}).isMediumEnabled())
require.True(t, (ChannelMediumOptions{KeepLatestPublication: true}).isMediumEnabled())
require.True(t, (ChannelMediumOptions{enableQueue: true}).isMediumEnabled())
require.True(t, (ChannelMediumOptions{broadcastDelay: time.Millisecond}).isMediumEnabled())
}
// TestChannelMediumWithBroadcastDelayCoalesces verifies the broadcastDelay path:
// multiple publications enqueued within a delay window get coalesced — only the
// last publication is broadcast.
func TestChannelMediumWithBroadcastDelayCoalesces(t *testing.T) {
t.Parallel()
delivered := make(chan *Publication, 8)
node := &mockNode{
handlePublicationFunc: func(_ string, _ StreamPosition, pub, _, _ *Publication) error {
delivered <- pub
return nil
},
}
medium, err := newChannelMedium("delay", node, ChannelMediumOptions{
enableQueue: true,
broadcastDelay: 50 * time.Millisecond,
})
require.NoError(t, err)
for i := uint64(1); i <= 5; i++ {
medium.broadcastPublication(
&Publication{Data: []byte(`x`), Offset: i},
StreamPosition{Offset: i, Epoch: "e"},
false, nil,
)
}
// Wait for first delivery — the 50ms broadcastDelay can stretch under -race
// load, so use Eventually rather than a fixed inner timeout that flakes.
var received []*Publication
require.Eventually(t, func() bool {
select {
case p := <-delivered:
received = append(received, p)
return true
default:
return false
}
}, 5*time.Second, 10*time.Millisecond, "expected at least one coalesced delivery")
// Drain any follow-ups arriving shortly after the first delivery. Use a
// long enough quiet window that all coalesced publications have a chance
// to arrive even under -race contention.
drainTimeout := time.After(500 * time.Millisecond)
drain:
for {
select {
case p := <-delivered:
received = append(received, p)
case <-drainTimeout:
break drain
}
}
// Coalescing: at minimum we should receive far fewer than 5 publications.
require.Less(t, len(received), 5)
// The last delivered publication must be the highest offset queued.
require.Equal(t, uint64(5), received[len(received)-1].Offset)
medium.close()
}
// TestChannelMediumKeepLatestPublicationDelta covers the
// KeepLatestPublication+delta path inside broadcast: when a delta-flagged pub
// arrives without a key, the medium uses its stored latestPublication as
// localPrevPub on the next broadcast.
func TestChannelMediumKeepLatestPublicationDelta(t *testing.T) {
t.Parallel()
delivered := make(chan struct{}, 4)
var captured struct {
first *Publication
second *Publication
}
idx := 0
node := &mockNode{
handlePublicationFunc: func(_ string, _ StreamPosition, pub, _, localPrev *Publication) error {
idx++
switch idx {
case 1:
captured.first = pub
case 2:
captured.second = localPrev
}
delivered <- struct{}{}
return nil
},
}
medium, err := newChannelMedium("keep-latest", node, ChannelMediumOptions{
KeepLatestPublication: true,
})
require.NoError(t, err)
defer medium.close()
medium.broadcastPublication(
&Publication{Data: []byte("first")},
StreamPosition{}, true, nil,
)
<-delivered
medium.broadcastPublication(
&Publication{Data: []byte("second")},
StreamPosition{}, true, nil,
)
<-delivered
require.NotNil(t, captured.first)
require.NotNil(t, captured.second, "second broadcast should see prior pub as localPrev")
require.Equal(t, []byte("first"), captured.second.Data)
}