-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
conn_test.go
424 lines (361 loc) · 12 KB
/
conn_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
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
package neffos_test
import (
"bytes"
"context"
"errors"
"reflect"
"sync"
"testing"
"github.com/kataras/neffos"
)
func TestConnect(t *testing.T) {
// test valid and not valid namespace connection.
var (
namespace = "default"
onlyOnServer = "only_on_server"
onlyOnClient = "only_on_client"
namespaceThatShouldErrOnServer = "no_access_by_server"
namespaceThatShouldErrOnClient = "no_access_by_client"
emptyEvents = neffos.Events{}
)
teardownServer := runTestServer("localhost:8080", neffos.Namespaces{
"": emptyEvents,
namespace: emptyEvents,
onlyOnServer: emptyEvents,
namespaceThatShouldErrOnServer: neffos.Events{
neffos.OnNamespaceConnect: func(c *neffos.NSConn, msg neffos.Message) error {
return neffos.ErrBadNamespace
},
},
namespaceThatShouldErrOnClient: emptyEvents,
})
defer teardownServer()
err := runTestClient("localhost:8080", neffos.Namespaces{
"": emptyEvents,
namespace: emptyEvents,
onlyOnClient: emptyEvents,
namespaceThatShouldErrOnServer: neffos.Events{
neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error {
t.Fatalf("%s namespace shouldn't be accessible to the client to connect", namespaceThatShouldErrOnServer)
return nil
},
},
namespaceThatShouldErrOnClient: neffos.Events{
neffos.OnNamespaceConnect: func(c *neffos.NSConn, msg neffos.Message) error {
return neffos.ErrBadNamespace
},
},
},
func(dialer string, client *neffos.Client) {
defer client.Close()
// should success, empty namespace naming is allowed and it's defined on both server and client-side.
_, err := client.Connect(context.TODO(), "")
if err != nil {
t.Fatal(err)
}
// should success, namespace exists in server-side and it's defined on client-side.
_, err = client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
c, err := client.Connect(context.TODO(), onlyOnServer)
if err == nil || c != nil {
t.Fatalf("%s namespace connect should fail, namespace exists on server but not defined at client-side", onlyOnServer)
}
c, err = client.Connect(context.TODO(), onlyOnClient)
if err == nil || c != nil {
t.Fatalf("%s namespace connect should fail, namespace defined on client but not exists at server-side.", onlyOnClient)
}
_, err = client.Connect(context.TODO(), namespaceThatShouldErrOnServer)
if err != neffos.ErrBadNamespace {
t.Fatalf("%s namespace connect should give a remote error by the server of the neffos.ErrBadNamespace exactly (it's a typed error which its text is converted to error when deserialized) but got: %v", namespaceThatShouldErrOnServer, err)
}
_, err = client.Connect(context.TODO(), namespaceThatShouldErrOnClient)
if err != neffos.ErrBadNamespace {
t.Fatalf("%s namespace connect should give a local event's error by the client of the neffos.ErrBadNamespace but got: %v", namespaceThatShouldErrOnServer, err)
}
})()
if err != nil {
t.Fatal(err)
}
}
func TestAsk(t *testing.T) {
var (
namespace = "default"
pingEvent = "ping"
pongMessage = []byte("PONG MESSAGE")
)
testMessage := func(dialer string, i int, msg neffos.Message) {
if msg.Namespace != namespace {
t.Fatalf("[%s] [%d] expected namespace to be %s but got %s instead", dialer, i, namespace, msg.Namespace)
}
if msg.Event != pingEvent {
t.Fatalf("[%s] [%d] expected event to be %s but got %s instead", dialer, i, pingEvent, msg.Event)
}
if !bytes.Equal(msg.Body, pongMessage) {
t.Fatalf("[%s] [%d] from callback: expected %s but got %s", dialer, i, string(pongMessage), string(msg.Body))
}
}
teardownServer := runTestServer("localhost:8080", neffos.Namespaces{namespace: neffos.Events{
pingEvent: func(c *neffos.NSConn, msg neffos.Message) error {
// c.Emit("event", pongMessage)
return neffos.Reply(pongMessage) // changes only body; ns,event remains.
}}})
defer teardownServer()
err := runTestClient("localhost:8080", neffos.Namespaces{namespace: neffos.Events{}}, func(dialer string, client *neffos.Client) {
defer client.Close()
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
for i := 1; i <= 5; i++ {
msg, err := c.Ask(context.TODO(), pingEvent, nil)
if err != nil {
t.Fatal(err)
}
testMessage(dialer, i, msg)
}
msg, err := c.Ask(context.TODO(), pingEvent, nil)
if err != nil {
t.Fatal(err)
}
testMessage(dialer, -1, msg)
})()
if err != nil {
t.Fatal(err)
}
}
func TestOnAnyEvent(t *testing.T) {
var (
namespace = "default"
expectedMessage = neffos.Message{
Namespace: namespace,
Event: "an_event",
Body: []byte("a_body"),
}
wg sync.WaitGroup // a pure check for client's `Emit` to fire (`Ask` don't need this).
testMessage = func(msg neffos.Message) {
// if !reflect.DeepEqual(msg, expectedMessage) { no because of Ask.wait.
if msg.Namespace != expectedMessage.Namespace ||
msg.Event != expectedMessage.Event ||
!bytes.Equal(msg.Body, expectedMessage.Body) {
t.Fatalf("expected message to be:\n%#+v\n\tbut got:\n%#+v", expectedMessage, msg)
}
}
)
teardownServer := runTestServer("localhost:8080", neffos.Namespaces{namespace: neffos.Events{
neffos.OnAnyEvent: func(c *neffos.NSConn, msg neffos.Message) error {
if neffos.IsSystemEvent(msg.Event) { // skip connect/disconnect messages.
return nil
}
return neffos.Reply(msg.Body)
}}})
defer teardownServer()
err := runTestClient("localhost:8080", neffos.Namespaces{namespace: neffos.Events{
expectedMessage.Event: func(c *neffos.NSConn, msg neffos.Message) error {
defer wg.Done()
testMessage(msg)
return nil
},
}}, func(dialer string, client *neffos.Client) {
defer client.Close()
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
wg.Add(1)
c.Emit(expectedMessage.Event, expectedMessage.Body)
wg.Wait()
msg, err := c.Ask(context.TODO(), expectedMessage.Event, expectedMessage.Body)
if err != nil {
t.Fatal(err)
}
testMessage(msg)
})()
if err != nil {
t.Fatal(err)
}
}
func TestOnNativeMessageAndMessageError(t *testing.T) {
var (
wg sync.WaitGroup
namespace = "" // empty namespace and OnNativeMessage event defined to allow native websocket messages to come through.
eventThatWillGiveErrorByServer = "event_error_server"
eventErrorText = "this event will give error by server"
nativeMessage = []byte("this is a native/raw websocket message")
events = neffos.Events{
neffos.OnNativeMessage: func(c *neffos.NSConn, msg neffos.Message) error {
defer wg.Done()
expectedMessage := neffos.Message{
Event: neffos.OnNativeMessage,
Body: nativeMessage,
IsNative: true,
}
if !reflect.DeepEqual(expectedMessage, msg) {
t.Fatalf("expected a native message to be:\n%#+v\n\tbut got:\n%#+v", expectedMessage, msg)
}
return nil
},
}
)
serverHandler := neffos.JoinConnHandlers(neffos.Namespaces{namespace: events},
neffos.Events{
eventThatWillGiveErrorByServer: func(c *neffos.NSConn, msg neffos.Message) error {
return errors.New(eventErrorText)
},
})
teardownServer := runTestServer("localhost:8080", serverHandler)
defer teardownServer()
clientHandler := neffos.JoinConnHandlers(neffos.Namespaces{namespace: events},
neffos.Events{
eventThatWillGiveErrorByServer: func(c *neffos.NSConn, msg neffos.Message) error {
defer wg.Done()
if !c.Conn.IsClient() {
t.Fatalf("this should only be executed by client-side, if not then the JoinConnHandlers didn't work as expected")
}
if msg.Err == nil {
t.Fatalf("expected an error from event: %s", eventThatWillGiveErrorByServer)
}
if expected, got := eventErrorText, msg.Err.Error(); expected != got {
t.Fatalf("expected an error from event: %s to match: '%s' but got: '%s'", eventThatWillGiveErrorByServer, expected, got)
}
return nil
},
})
err := runTestClient("localhost:8080", clientHandler, func(dialer string, client *neffos.Client) {
defer client.Close()
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
// Ask is not available on native websocket messages of course.
wg.Add(1)
c.Conn.Write(neffos.Message{
Body: nativeMessage,
IsNative: true,
})
wg.Add(1)
c.Emit(eventThatWillGiveErrorByServer, []byte("doesn't matter"))
wg.Wait()
})()
if err != nil {
t.Fatal(err)
}
}
func TestOnNativeMessageOnly(t *testing.T) {
// when the only one namespace is "" and event OnNativeMessage.
var (
wg sync.WaitGroup
namespace = ""
nativeMessage = []byte("this is a native/raw websocket message")
events = neffos.Events{
neffos.OnNativeMessage: func(c *neffos.NSConn, msg neffos.Message) error {
defer wg.Done()
expectedMessage := neffos.Message{
Event: neffos.OnNativeMessage,
Body: nativeMessage,
IsNative: true,
}
if !reflect.DeepEqual(expectedMessage, msg) {
t.Fatalf("expected a native message to be:\n%#+v\n\tbut got:\n%#+v", expectedMessage, msg)
}
return nil
},
}
)
teardownServer := runTestServer("localhost:8080", events)
defer teardownServer()
err := runTestClient("localhost:8080", events, func(dialer string, client *neffos.Client) {
defer client.Close()
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
wg.Add(1)
c.Conn.Write(neffos.Message{
Body: nativeMessage,
IsNative: true,
})
wg.Wait()
})()
if err != nil {
t.Fatal(err)
}
}
// No need to encourage users to use go routines for event sending even if it's totally safe in neffos.
// It works but ^
// func TestSimultaneouslyEventsRoutines(t *testing.T) {
// // test multiple goroutines sending events, it should work because the lib it is designed to take care of these things.
// var (
// wg sync.WaitGroup
// namespace = "namespace1"
// event1 = "event1"
// event2 = "event2"
// event3 = "event3"
// expectedMessages = map[string]neffos.Message{
// event1: {
// Namespace: namespace,
// Event: event1,
// Body: []byte("body1"),
// },
// event2: {
// Namespace: namespace,
// Event: event2,
// Body: []byte("body2"),
// },
// event3: {
// Namespace: namespace,
// Event: event3,
// Body: []byte("body3"),
// },
// }
// events = neffos.Events{
// neffos.OnAnyEvent: func(c *neffos.NSConn, msg neffos.Message) error {
// if neffos.IsSystemEvent(msg.Event) {
// return nil
// }
// expectedMessage := expectedMessages[msg.Event]
// if !reflect.DeepEqual(msg, expectedMessage) {
// t.Fatalf("expected message:\n%#+v\n\tbut got:\n%#+v", expectedMessage, msg)
// }
// if c.Conn.IsClient() {
// // wait for server's reply to the client's send act before done with this event test.
// defer wg.Done()
// } else {
// // send back to the client the message as it's.
// return neffos.Reply(msg.Body)
// }
// return nil
// },
// }
// )
// teardownServer := runTestServer("localhost:8080", neffos.Namespaces{namespace: events})
// defer teardownServer()
// err := runTestClient("localhost:8080", neffos.Namespaces{namespace: events},
// func(dialer string, client *neffos.Client) {
// defer client.Close()
// c, err := client.Connect(nil, namespace)
// if err != nil {
// t.Fatal(err)
// }
// send := func(event string) {
// wg.Add(1)
// msg := expectedMessages[event]
// c.Emit(msg.Event, msg.Body)
// }
// for i := 0; i < 5; i++ {
// go send(event1)
// go send(event2)
// go send(event3)
// }
// for i := 0; i < 5; i++ {
// go send(event3)
// go send(event2)
// go send(event1)
// }
// wg.Wait()
// })()
// if err != nil {
// t.Fatal(err)
// }
// }