forked from centrifugal/centrifuge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_medium_test.go
194 lines (176 loc) · 5 KB
/
channel_medium_test.go
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
package centrifuge
import (
"errors"
"math"
"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)
}
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 TestChannelMediumHandlePublication(t *testing.T) {
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) {
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) {
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
},
})
originalGetter := channelMediumTimeNow
channelMediumTimeNow = func() time.Time {
return time.Now().Add(time.Hour)
}
medium.CheckPosition(time.Second, StreamPosition{Offset: 1, Epoch: "test"}, time.Second)
channelMediumTimeNow = originalGetter
select {
case <-doneCh:
case <-time.After(5 * time.Second):
require.Fail(t, "historyFunc was not called")
}
}
func TestChannelMediumPositionSyncRetry(t *testing.T) {
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
},
})
originalGetter := channelMediumTimeNow
channelMediumTimeNow = func() time.Time {
return time.Now().Add(time.Hour)
}
medium.CheckPosition(time.Second, StreamPosition{Offset: 1, Epoch: "test"}, time.Second)
channelMediumTimeNow = originalGetter
select {
case <-doneCh:
case <-time.After(5 * time.Second):
require.Fail(t, "streamTopLatestPubFunc was not called")
}
}