-
-
Notifications
You must be signed in to change notification settings - Fork 577
/
test_helpers.go
283 lines (242 loc) · 6.56 KB
/
test_helpers.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
package tools
import (
"context"
"errors"
"io"
"log"
"net/http"
"net/http/httptest"
"sync"
"github.com/centrifugal/centrifugo/v5/internal/proxyproto"
"github.com/centrifugal/centrifuge"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
)
// TestTransport - test transport
type TestTransport struct {
mu sync.Mutex
sink chan []byte
closed bool
closeCh chan struct{}
disconnect *centrifuge.Disconnect
protoType centrifuge.ProtocolType
}
// NewTestTransport - builder for TestTransport
func NewTestTransport() *TestTransport {
return &TestTransport{
protoType: centrifuge.ProtocolTypeJSON,
closeCh: make(chan struct{}),
}
}
// Write - ...
func (t *TestTransport) Write(message []byte) error {
return t.WriteMany(message)
}
// WriteMany - ...
func (t *TestTransport) WriteMany(messages ...[]byte) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.closed {
return io.EOF
}
for _, buf := range messages {
dataCopy := make([]byte, len(buf))
copy(dataCopy, buf)
if t.sink != nil {
t.sink <- dataCopy
}
}
return nil
}
// Name - ...
func (t *TestTransport) Name() string {
return "test_transport"
}
// Protocol - ...
func (t *TestTransport) Protocol() centrifuge.ProtocolType {
return t.protoType
}
// ProtocolVersion returns transport protocol version.
func (t *TestTransport) ProtocolVersion() centrifuge.ProtocolVersion {
return centrifuge.ProtocolVersion2
}
// Unidirectional - ...
func (t *TestTransport) Unidirectional() bool {
return false
}
// DisabledPushFlags - ...
func (t *TestTransport) DisabledPushFlags() uint64 {
return centrifuge.PushFlagDisconnect
}
// PingPongConfig ...
func (t *TestTransport) PingPongConfig() centrifuge.PingPongConfig {
return centrifuge.PingPongConfig{}
}
// Emulation ...
func (t *TestTransport) Emulation() bool {
return false
}
// Close - ...
func (t *TestTransport) Close(disconnect centrifuge.Disconnect) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.closed {
return nil
}
t.disconnect = &disconnect
t.closed = true
close(t.closeCh)
return nil
}
// NodeWithMemoryEngineNoHandlers - builder for centrifuge node with memory engine
func NodeWithMemoryEngineNoHandlers() *centrifuge.Node {
n, err := centrifuge.New(centrifuge.Config{})
if err != nil {
panic(err)
}
err = n.Run()
if err != nil {
panic(err)
}
return n
}
// NodeWithMemoryEngine - builder for centrifuge node with memory engine
func NodeWithMemoryEngine() *centrifuge.Node {
n := NodeWithMemoryEngineNoHandlers()
n.OnConnect(func(client *centrifuge.Client) {
client.OnSubscribe(func(_ centrifuge.SubscribeEvent, cb centrifuge.SubscribeCallback) {
cb(centrifuge.SubscribeReply{}, nil)
})
client.OnPublish(func(_ centrifuge.PublishEvent, cb centrifuge.PublishCallback) {
cb(centrifuge.PublishReply{}, nil)
})
})
return n
}
type CommonHTTPProxyTestCase struct {
Node *centrifuge.Node
Client *centrifuge.Client
ClientCloseFunc centrifuge.ClientCloseFunc
Server *httptest.Server
Mux *http.ServeMux
}
func NewCommonHTTPProxyTestCase(ctx context.Context) *CommonHTTPProxyTestCase {
node := NodeWithMemoryEngineNoHandlers()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client, closeFn, err := centrifuge.NewClient(ctx, node, NewTestTransport())
if err != nil {
log.Fatalf("could not create centrifuge client: %v", err)
}
return &CommonHTTPProxyTestCase{
Node: node,
Client: client,
ClientCloseFunc: closeFn,
Server: server,
Mux: mux,
}
}
func (c *CommonHTTPProxyTestCase) Teardown() {
defer func() { _ = c.Node.Shutdown(context.Background()) }()
defer func() { _ = c.ClientCloseFunc() }()
c.Server.Close()
}
type CommonGRPCProxyTestCase struct {
Node *centrifuge.Node
Client *centrifuge.Client
ClientCloseFunc centrifuge.ClientCloseFunc
Server *grpc.Server
Listener *bufconn.Listener
}
func NewCommonGRPCProxyTestCase(ctx context.Context, srv proxyproto.CentrifugoProxyServer) *CommonGRPCProxyTestCase {
node := NodeWithMemoryEngineNoHandlers()
client, closeFn, err := centrifuge.NewClient(ctx, node, NewTestTransport())
if err != nil {
log.Fatalf("could not create centrifuge client: %v", err)
}
listener := bufconn.Listen(1024)
server := grpc.NewServer()
proxyproto.RegisterCentrifugoProxyServer(server, srv)
go func() {
if err := server.Serve(listener); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
log.Fatalf("GRPC server exited with error: %v", err)
}
}()
return &CommonGRPCProxyTestCase{
Node: node,
Client: client,
ClientCloseFunc: closeFn,
Server: server,
Listener: listener,
}
}
func (c *CommonGRPCProxyTestCase) Teardown() {
defer func() { _ = c.Node.Shutdown(context.Background()) }()
defer func() { _ = c.ClientCloseFunc() }()
c.Server.Stop()
}
type TestClientMock struct {
IDFunc func() string
UserIDFunc func() string
IsSubscribedFunc func(string) bool
ContextFunc func() context.Context
TransportFunc func() centrifuge.TransportInfo
storageMu sync.Mutex
storage map[string]any
}
func (m *TestClientMock) Send(bytes []byte) error {
//TODO implement me
panic("implement me")
}
func (m *TestClientMock) Disconnect(disconnect ...centrifuge.Disconnect) {
//TODO implement me
panic("implement me")
}
func (m *TestClientMock) WritePublication(channel string, publication *centrifuge.Publication, sp centrifuge.StreamPosition) error {
//TODO implement me
panic("implement me")
}
func (m *TestClientMock) ID() string {
if m.IDFunc != nil {
return m.IDFunc()
}
panic("not implemented")
}
func (m *TestClientMock) UserID() string {
if m.UserIDFunc != nil {
return m.UserIDFunc()
}
panic("not implemented")
}
func (m *TestClientMock) IsSubscribed(s string) bool {
if m.IsSubscribedFunc != nil {
return m.IsSubscribedFunc(s)
}
panic("not implemented")
}
func (m *TestClientMock) Context() context.Context {
if m.ContextFunc != nil {
return m.ContextFunc()
}
panic("not implemented")
}
func (m *TestClientMock) Transport() centrifuge.TransportInfo {
if m.TransportFunc != nil {
return m.TransportFunc()
}
panic("not implemented")
}
func (m *TestClientMock) AcquireStorage() (map[string]any, func(map[string]any)) {
m.storageMu.Lock()
if m.storage == nil {
m.storage = map[string]any{}
}
return m.storage, func(updatedStorage map[string]any) {
m.storage = updatedStorage
m.storageMu.Unlock()
}
}
func (m *TestClientMock) Unsubscribe(ch string, unsubscribe ...centrifuge.Unsubscribe) {
//TODO implement me
panic("implement me")
}