-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_test.go
83 lines (70 loc) · 2.06 KB
/
websocket_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
package websocket_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/brojonat/websocket"
gwebsocket "github.com/gorilla/websocket"
"github.com/matryer/is"
)
func TestWSHandler(t *testing.T) {
is := is.New(t)
testBytes := []byte("testing")
upgrader := gwebsocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
// synchronization helpers
doneReg := make(chan websocket.Client)
doneUnreg := make(chan websocket.Client)
var c websocket.Client
var cf context.CancelFunc
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
manager := websocket.NewManager()
go manager.Run(ctx)
h := websocket.ServeWS(
upgrader,
websocket.DefaultSetupConn,
websocket.NewClient,
func(ctx context.Context, _cf context.CancelFunc, _c websocket.Client) {
cf = _cf
c = _c
manager.RegisterClient(ctx, cf, c)
doneReg <- c
},
func(_c websocket.Client) {
manager.UnregisterClient(_c)
_c.Wait()
doneUnreg <- _c
},
50*time.Second,
[]websocket.MessageHandler{func(c websocket.Client, b []byte) { c.Write(b) }},
)
// setup and connect to the the test server using a basic websocket
s := httptest.NewServer(h)
defer s.Close()
rawWS, _, err := gwebsocket.DefaultDialer.Dial(
"ws"+strings.TrimPrefix(s.URL, "http"), nil)
is.NoErr(err)
defer rawWS.Close()
// once registration is done, the manager should have one client
<-doneReg
is.Equal(len(manager.Clients()), 1)
// write a message to the server; this will be echoed back
err = rawWS.WriteMessage(gwebsocket.TextMessage, testBytes)
is.NoErr(err)
_, msg, err := rawWS.ReadMessage()
is.NoErr(err)
is.Equal(msg, testBytes)
// close the connection, which should trigger the server to cleanup and
// unregister the client connection
rawWS.WriteControl(gwebsocket.CloseMessage, nil, time.Now().Add(1*time.Second))
_p := <-doneUnreg
is.Equal(len(manager.Clients()), 0)
is.Equal(_p, c)
time.Sleep(1 * time.Second)
//FIXME: seems to be leaking goroutines
}