-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
conn_namespace_test.go
165 lines (139 loc) · 3.72 KB
/
conn_namespace_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
package neffos_test
import (
"bytes"
"context"
"sync"
"testing"
"github.com/kataras/neffos"
)
func TestJoinAndLeaveRoom(t *testing.T) {
var (
wg sync.WaitGroup
namespace = "default"
roomName = "room1"
body = []byte("data")
events = neffos.Namespaces{
namespace: neffos.Events{
"event": func(c *neffos.NSConn, msg neffos.Message) error {
if c.Conn.IsClient() {
if !bytes.Equal(msg.Body, body) {
t.Fatalf("expected event's incoming data to be: %s but got: %s", string(body), string(msg.Body))
}
room := c.Room(roomName)
if room == nil {
t.Fatal("expected a non-nil room")
}
if room.Name != msg.Room {
t.Fatalf("expected Message's room name to be: %s==%s but it's: %s", roomName, room.Name, msg.Room)
}
room.Leave(context.TODO())
wg.Done()
} else {
c.Conn.Server().Broadcast(nil, msg)
}
return nil
},
neffos.OnRoomLeft: func(c *neffos.NSConn, msg neffos.Message) error {
if c.Conn.IsClient() {
if msg.Room != roomName {
t.Fatalf("expected left room name to be %s but got %s", roomName, msg.Room)
}
wg.Done()
}
return nil
},
},
}
)
teardownServer := runTestServer("localhost:8080", events)
defer teardownServer()
err := runTestClient("localhost:8080", events,
func(dialer string, client *neffos.Client) {
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
room, err := c.JoinRoom(context.TODO(), roomName)
if err != nil {
t.Fatal(err)
}
if room == nil {
t.Fatal("expected a non-nil room")
}
if room.Name != roomName {
t.Fatalf("expected joined room name to be: %s but got: %s", roomName, room.Name)
}
// 1 -> to catch its own event and
// 2 -> to notify about room leave inside the event itself for both clients ofc.
wg.Add(2)
ok := room.Emit("event", body)
if !ok {
t.Fatalf("expected true")
}
wg.Wait()
})()
if err != nil {
t.Fatal(err)
}
}
func TestJoinAndLeaveRoomInsideHandler(t *testing.T) {
var (
wg sync.WaitGroup
namespace = "default"
roomName = "room1"
body = []byte("data")
events = neffos.Namespaces{
namespace: neffos.Events{
"event": func(c *neffos.NSConn, msg neffos.Message) error {
if c.Conn.IsClient() {
if !bytes.Equal(msg.Body, body) {
t.Fatalf("expected event's incoming data to be: %s but got: %s", string(body), string(msg.Body))
}
room := c.Room(roomName)
if room == nil {
t.Fatal("expected a non-nil room")
}
if room.Name != msg.Room {
t.Fatalf("expected Message's room name to be: %s==%s but it's: %s", roomName, room.Name, msg.Room)
}
room.Leave(context.TODO())
wg.Done()
} else {
room, err := c.JoinRoom(context.TODO(), roomName)
if err != nil {
return err
}
room.Emit(msg.Event, msg.Body)
}
return nil
},
neffos.OnRoomLeft: func(c *neffos.NSConn, msg neffos.Message) error {
if c.Conn.IsClient() {
if msg.Room != roomName {
t.Fatalf("expected left room name to be %s but got %s", roomName, msg.Room)
}
wg.Done()
}
return nil
},
},
}
)
teardownServer := runTestServer("localhost:8080", events)
defer teardownServer()
err := runTestClient("localhost:8080", events,
func(dialer string, client *neffos.Client) {
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
// 1 -> to catch its own event and
// 2 -> to notify about room leave inside the event itself for both clients ofc.
wg.Add(2)
c.Emit("event", body)
wg.Wait()
})()
if err != nil {
t.Fatal(err)
}
}