-
Notifications
You must be signed in to change notification settings - Fork 1
/
hub_test.go
303 lines (222 loc) · 5.43 KB
/
hub_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
package ws
import (
"log"
"sync"
"testing"
"net/http"
"net/http/httptest"
"strings"
"github.com/gorilla/websocket"
)
// NOTES:
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
//
// or
//
// -- may be a good idea to change to output path to somewherelike /tmp
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
func TestSingleConnection(t *testing.T) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
hub := New(upgrader, nil)
defer hub.Shutdown()
mux := http.NewServeMux()
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
err := hub.Accept(w, r)
if err != nil {
t.Fatalf("accepting WebSocket connection '%s'\n", err)
}
})
server := httptest.NewServer(mux)
defer server.Close()
msg := "Test Message\n"
url := strings.Replace(server.URL, "http", "ws", 1) + "/ws"
var dialer *websocket.Dialer
conn, _, err := dialer.Dial(url, nil)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = conn.WriteMessage(websocket.TextMessage, []byte(msg))
if err != nil {
t.Fatal(err)
}
}()
wg.Wait()
_, message, err := conn.ReadMessage()
if err != nil {
t.Fatal(err)
}
resp := string(message)
if msg != resp {
log.Fatalf("Expected '%s' Got '%s'", msg, resp)
}
// explicitly calling before conn.Close() otherwise an
// unexpected error will print from closing conn
hub.Shutdown()
}
func TestMultipleConnections(t *testing.T) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
clientFn := func(h *Hub, conn *websocket.Conn, r *http.Request) Client {
fn := func(msg []byte) {
h.BroadcastTo(msg, func(c Client) bool {
return true
})
}
return NewClient(h, conn, fn)
}
hub := New(upgrader, clientFn)
defer hub.Shutdown()
mux := http.NewServeMux()
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
err := hub.Accept(w, r)
if err != nil {
t.Fatalf("accepting WebSocket connection '%s'\n", err)
}
})
server := httptest.NewServer(mux)
defer server.Close()
msg := "Test Message\n"
url := strings.Replace(server.URL, "http", "ws", 1) + "/ws"
var dialer1 *websocket.Dialer
var dialer2 *websocket.Dialer
conn1, _, err := dialer1.Dial(url, nil)
if err != nil {
t.Fatal(err)
}
defer conn1.Close()
conn2, _, err := dialer2.Dial(url, nil)
if err != nil {
t.Fatal(err)
}
defer conn2.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = conn1.WriteMessage(websocket.TextMessage, []byte(msg))
if err != nil {
t.Fatal(err)
}
}()
wg.Wait()
_, message1, err := conn1.ReadMessage()
if err != nil {
t.Fatal(err)
}
_, message2, err := conn2.ReadMessage()
if err != nil {
t.Fatal(err)
}
resp1 := string(message1)
resp2 := string(message2)
if msg != resp1 {
log.Fatalf("Expected '%s' Got '%s'", msg, resp1)
}
if msg != resp2 {
log.Fatalf("Expected '%s' Got '%s'", msg, resp2)
}
// explicitly calling before conn.Close() otherwise an
// unexpected error will print from closing conn
hub.Shutdown()
}
func TestCloseAndRemoveConnection(t *testing.T) {
type myCustomClient struct {
Client
id string
}
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
var c1, c2 Client
clientFn := func(h *Hub, conn *websocket.Conn, r *http.Request) Client {
var myClient *myCustomClient
fn := func(msg []byte) {
h.BroadcastTo(msg, func(c Client) bool {
return true
})
}
// create my custom client and add additional information
// in this case the id of the client as an example...most
// likely this would be a user ID to identify the user connected.
myClient = &myCustomClient{
Client: NewClient(h, conn, fn),
id: r.URL.Query().Get("id"),
}
if myClient.id == "1" {
c1 = myClient
} else {
c2 = myClient
}
return myClient
}
hub := New(upgrader, clientFn)
defer hub.Shutdown()
mux := http.NewServeMux()
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
err := hub.Accept(w, r)
if err != nil {
t.Fatalf("accepting WebSocket connection '%s'\n", err)
}
})
server := httptest.NewServer(mux)
defer server.Close()
msg := "Test Message\n"
url := strings.Replace(server.URL, "http", "ws", 1) + "/ws?id="
var dialer1 *websocket.Dialer
var dialer2 *websocket.Dialer
conn1, _, err := dialer1.Dial(url+"1", nil)
if err != nil {
t.Fatal(err)
}
defer conn1.Close()
conn2, _, err := dialer2.Dial(url+"2", nil)
if err != nil {
t.Fatal(err)
}
defer conn2.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = conn1.WriteMessage(websocket.TextMessage, []byte(msg))
if err != nil {
t.Fatal(err)
}
}()
wg.Wait()
_, message1, err := conn1.ReadMessage()
if err != nil {
t.Fatal(err)
}
_, message2, err := conn2.ReadMessage()
if err != nil {
t.Fatal(err)
}
resp1 := string(message1)
resp2 := string(message2)
if msg != resp1 {
log.Fatalf("Expected '%s' Got '%s'", msg, resp1)
}
if msg != resp2 {
log.Fatalf("Expected '%s' Got '%s'", msg, resp2)
}
c1.Close()
hub.Remove(c2)
c2.Close()
// explicitly calling before conn.Close() otherwise an
// unexpected error will print from closing conn
hub.Shutdown()
}