forked from centrifugal/centrifuge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_test.go
297 lines (258 loc) · 7.12 KB
/
node_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
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
package centrifuge
import (
"context"
"encoding/json"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/centrifugal/centrifuge/internal/controlproto"
"github.com/centrifugal/protocol"
"github.com/stretchr/testify/require"
)
type TestEngine struct {
publishCount int32
publishJoinCount int32
publishLeaveCount int32
publishControlCount int32
}
func NewTestEngine() *TestEngine {
return &TestEngine{}
}
func (e *TestEngine) Run(_ BrokerEventHandler) error {
return nil
}
func (e *TestEngine) Publish(_ string, _ *protocol.Publication, _ *ChannelOptions) error {
atomic.AddInt32(&e.publishCount, 1)
return nil
}
func (e *TestEngine) PublishJoin(_ string, _ *protocol.Join, _ *ChannelOptions) error {
atomic.AddInt32(&e.publishJoinCount, 1)
return nil
}
func (e *TestEngine) PublishLeave(_ string, _ *protocol.Leave, _ *ChannelOptions) error {
atomic.AddInt32(&e.publishLeaveCount, 1)
return nil
}
func (e *TestEngine) PublishControl(_ []byte) error {
atomic.AddInt32(&e.publishControlCount, 1)
return nil
}
func (e *TestEngine) Subscribe(_ string) error {
return nil
}
func (e *TestEngine) Unsubscribe(_ string) error {
return nil
}
func (e *TestEngine) AddPresence(_ string, _ string, _ *protocol.ClientInfo, _ time.Duration) error {
return nil
}
func (e *TestEngine) RemovePresence(_ string, _ string) error {
return nil
}
func (e *TestEngine) Presence(_ string) (map[string]*protocol.ClientInfo, error) {
return map[string]*protocol.ClientInfo{}, nil
}
func (e *TestEngine) PresenceStats(_ string) (PresenceStats, error) {
return PresenceStats{}, nil
}
func (e *TestEngine) History(_ string, _ HistoryFilter) ([]*protocol.Publication, StreamPosition, error) {
return nil, StreamPosition{}, nil
}
func (e *TestEngine) AddHistory(_ string, _ *protocol.Publication, _ *ChannelOptions) (StreamPosition, bool, error) {
return StreamPosition{}, false, nil
}
func (e *TestEngine) RemoveHistory(_ string) error {
return nil
}
func (e *TestEngine) Channels() ([]string, error) {
return []string{}, nil
}
func nodeWithTestEngine() *Node {
c := DefaultConfig
n, err := New(c)
if err != nil {
panic(err)
}
n.SetEngine(NewTestEngine())
err = n.Run()
if err != nil {
panic(err)
}
return n
}
func nodeWithMemoryEngine() *Node {
c := DefaultConfig
n, err := New(c)
if err != nil {
panic(err)
}
err = n.Run()
if err != nil {
panic(err)
}
return n
}
func TestUserAllowed(t *testing.T) {
node := nodeWithTestEngine()
defer func() { _ = node.Shutdown(context.Background()) }()
require.True(t, node.userAllowed("channel#1", "1"))
require.True(t, node.userAllowed("channel", "1"))
require.False(t, node.userAllowed("channel#1", "2"))
require.True(t, node.userAllowed("channel#1,2", "1"))
require.True(t, node.userAllowed("channel#1,2", "2"))
require.False(t, node.userAllowed("channel#1,2", "3"))
}
func TestSetConfig(t *testing.T) {
node := nodeWithTestEngine()
defer func() { _ = node.Shutdown(context.Background()) }()
err := node.Reload(DefaultConfig)
require.NoError(t, err)
}
func TestNodeRegistry(t *testing.T) {
registry := newNodeRegistry("node1")
nodeInfo1 := controlproto.Node{UID: "node1"}
nodeInfo2 := controlproto.Node{UID: "node2"}
registry.add(&nodeInfo1)
registry.add(&nodeInfo2)
require.Equal(t, 2, len(registry.list()))
info := registry.get("node1")
require.Equal(t, "node1", info.UID)
registry.clean(10 * time.Second)
time.Sleep(2 * time.Second)
registry.clean(time.Second)
// Current node info should still be in node registry - we never delete it.
require.Equal(t, 1, len(registry.list()))
}
var testPayload = map[string]interface{}{
"_id": "5adece493c1a23736b037c52",
"index": 2,
"guid": "478a00f4-19b1-4567-8097-013b8cc846b8",
"isActive": false,
"balance": "$2,199.02",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "blue",
"name": "Swanson Walker",
"gender": "male",
"company": "SHADEASE",
"email": "[email protected]",
"phone": "+1 (885) 410-3991",
"address": "768 Paerdegat Avenue, Gouglersville, Oklahoma, 5380",
"registered": "2016-01-24T07:40:09 -03:00",
"latitude": -71.336378,
"longitude": -28.155956,
"tags": []string{
"magna",
"nostrud",
"irure",
"aliquip",
"culpa",
"sint",
},
"greeting": "Hello, Swanson Walker! You have 9 unread messages.",
"favoriteFruit": "apple",
}
func BenchmarkNodePublishWithNoopEngine(b *testing.B) {
node := nodeWithTestEngine()
payload, err := json.Marshal(testPayload)
if err != nil {
panic(err.Error())
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := node.Publish("bench", payload)
if err != nil {
b.Fatal(err)
}
}
b.StopTimer()
_ = node.Shutdown(context.Background())
}
func newFakeConn(b testing.TB, node *Node, channel string, protoType ProtocolType, sink chan []byte) {
transport := newTestTransport()
transport.setProtocolType(protoType)
transport.setSink(sink)
ctx := context.Background()
newCtx := SetCredentials(ctx, &Credentials{UserID: "42"})
client, _ := NewClient(newCtx, node, transport)
connectClient(b, client)
var replies []*protocol.Reply
rw := testReplyWriter(&replies)
subCtx := client.subscribeCmd(&protocol.SubscribeRequest{
Channel: channel,
}, rw, false)
require.Nil(b, subCtx.disconnect)
}
func newFakeConnJSON(b testing.TB, node *Node, channel string, sink chan []byte) {
newFakeConn(b, node, channel, ProtocolTypeJSON, sink)
}
func newFakeConnProtobuf(b testing.TB, node *Node, channel string, sink chan []byte) {
newFakeConn(b, node, channel, ProtocolTypeProtobuf, sink)
}
func BenchmarkBroadcastMemoryEngine(b *testing.B) {
benchmarks := []struct {
name string
getFakeConn func(b testing.TB, n *Node, channel string, sink chan []byte)
numSubscribers int
}{
{"JSON", newFakeConnJSON, 1},
{"Protobuf", newFakeConnProtobuf, 1},
{"JSON", newFakeConnJSON, 10000},
{"Protobuf", newFakeConnProtobuf, 10000},
}
for _, bm := range benchmarks {
b.Run(fmt.Sprintf("%s_%d_subscribers", bm.name, bm.numSubscribers), func(b *testing.B) {
n := nodeWithMemoryEngine()
c := n.Config()
c.ClientInsecure = true
err := n.Reload(c)
if err != nil {
b.Fatal(err)
}
payload := []byte(`{"input": "test"}`)
sink := make(chan []byte, bm.numSubscribers)
for i := 0; i < bm.numSubscribers; i++ {
bm.getFakeConn(b, n, "test", sink)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := n.Publish("test", payload)
if err != nil {
panic(err)
}
for j := 0; j < bm.numSubscribers; j++ {
<-sink
}
}
b.StopTimer()
_ = n.Shutdown(context.Background())
b.ReportAllocs()
})
}
}
func BenchmarkHistory(b *testing.B) {
e := testMemoryEngine()
conf := e.node.Config()
numMessages := 100
conf.HistorySize = numMessages
conf.HistoryLifetime = 60
conf.HistoryRecover = true
err := e.node.Reload(conf)
require.NoError(b, err)
channel := "test"
for i := 1; i <= numMessages; i++ {
_, err := e.node.Publish(channel, []byte(`{}`))
require.NoError(b, err)
}
b.ResetTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := e.node.History(channel)
if err != nil {
b.Fatal(err)
}
}
b.StopTimer()
b.ReportAllocs()
}