-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbroadcaster_test.go
More file actions
144 lines (130 loc) · 3.33 KB
/
broadcaster_test.go
File metadata and controls
144 lines (130 loc) · 3.33 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
package arksdk
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestBroadcaster(t *testing.T) {
// All broadcaster operations are unconditionally valid — there are no
// invalid cases to exercise.
t.Run("valid", func(t *testing.T) {
t.Run("subscribe buffer", func(t *testing.T) {
fixtures := []struct {
name string
buf int
wantCap int
}{
{
name: "zero defaults to 64",
buf: 0,
wantCap: 64,
},
{
name: "explicit buffer is used as-is",
buf: 16,
wantCap: 16,
},
}
for _, f := range fixtures {
t.Run(f.name, func(t *testing.T) {
b := newBroadcaster[int]()
ch := b.subscribe(f.buf)
require.Equal(t, f.wantCap, cap(ch))
})
}
})
t.Run(
"subscribe on closed broadcaster returns already-closed channel", func(t *testing.T) {
b := newBroadcaster[int]()
b.close()
ch := b.subscribe(1)
_, open := <-ch
require.False(t, open)
},
)
t.Run("unsubscribe closes and removes the channel", func(t *testing.T) {
b := newBroadcaster[int]()
ch := b.subscribe(1)
b.unsubscribe(ch)
_, open := <-ch
require.False(t, open)
// after removal, publish no longer sends to the channel
b.publish(42)
require.Empty(t, ch)
})
t.Run("unsubscribe unknown channel is a no-op", func(t *testing.T) {
b := newBroadcaster[int]()
other := make(chan int, 1)
require.NotPanics(t, func() { b.unsubscribe(other) })
})
t.Run("publish", func(t *testing.T) {
fixtures := []struct {
name string
subscribers int
}{
{name: "no subscribers", subscribers: 0},
{name: "single subscriber", subscribers: 1},
{name: "multiple subscribers", subscribers: 3},
}
for _, f := range fixtures {
t.Run(f.name, func(t *testing.T) {
b := newBroadcaster[int]()
channels := make([]<-chan int, f.subscribers)
for i := range f.subscribers {
channels[i] = b.subscribe(1)
}
dropped := b.publish(42)
require.Equal(t, 0, dropped)
for _, ch := range channels {
require.Len(t, ch, 1)
require.Equal(t, 42, <-ch)
}
})
}
})
t.Run(
"publish returns dropped count and schedules removal of full-buffer listeners",
func(t *testing.T) {
b := newBroadcaster[int]()
ch := b.subscribe(1)
b.publish(1) // fills the buffer
dropped := b.publish(2) // buffer full → listener dropped
require.Equal(t, 1, dropped)
// wait for the async remove goroutine to close the channel
require.Eventually(t, func() bool {
select {
case _, open := <-ch:
return !open
default:
return false
}
}, 100*time.Millisecond, time.Millisecond)
},
)
t.Run("publish after close is a no-op", func(t *testing.T) {
b := newBroadcaster[int]()
_ = b.subscribe(1)
b.close()
dropped := b.publish(42)
require.Equal(t, 0, dropped)
})
t.Run("close closes all subscriber channels", func(t *testing.T) {
b := newBroadcaster[int]()
ch1 := b.subscribe(1)
ch2 := b.subscribe(1)
b.close()
_, open1 := <-ch1
_, open2 := <-ch2
require.False(t, open1)
require.False(t, open2)
})
t.Run("close is idempotent", func(t *testing.T) {
b := newBroadcaster[int]()
_ = b.subscribe(1)
require.NotPanics(t, func() {
b.close()
b.close()
})
})
})
}