-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcortical_test.go
150 lines (130 loc) · 3.65 KB
/
cortical_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
package cortical
import (
"github.com/gorilla/websocket"
"time"
"context"
"github.com/gorilla/mux"
"net/http/httptest"
"net/url"
"testing"
)
var (
testServer *httptest.Server
tsURL *url.URL
)
// echo is a dummy type that reads a message, wait for some time and sends ret back
type echo struct{}
func new() *echo {
return &echo{}
}
// NewCortex is filling the ...
func (e *echo) NewCortex(ctx context.Context) (GetInfoFromCortexFunc, SendInfoToCortex) {
c := make(chan []byte)
return func(ctx context.Context) chan []byte {
return c
}, func(ctx context.Context, b *[]byte) {
c <- *b
}
}
func init() {
router := mux.NewRouter().StrictSlash(true)
brain := &Cortical{
Upgrader: websocket.Upgrader{},
Cortexes: []Cortex{new()},
}
router.
Methods("GET").
Path("/ws").
Name("WebSocket").
HandlerFunc(brain.ServeWS)
testServer = httptest.NewServer(router) //Creating new server with the user handlers
tsURL, _ = url.Parse(testServer.URL)
}
func TestPingPong(t *testing.T) {
wsURL := url.URL{Scheme: "ws", Host: tsURL.Host, Path: "/ws"}
c, _, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
if err != nil {
t.Fatalf("Cannot connect to the websocket %v", err)
}
defer c.Close()
if err := c.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(2*time.Second)); err != nil {
t.Errorf("write close: %v", err)
}
err = c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""))
if err != nil {
t.Errorf("write close: %v", err)
}
}
func TestClosing(t *testing.T) {
// Now test the websocket
wsURL := url.URL{Scheme: "ws", Host: tsURL.Host, Path: "/ws"}
c, _, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
if err != nil {
t.Errorf("Cannot connect to the websocket %v", err)
}
defer c.Close()
}
func TestServeWS(t *testing.T) {
// Now test the websocket
test := []byte("test")
wsURL := url.URL{Scheme: "ws", Host: tsURL.Host, Path: "/ws"}
c, _, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
if err != nil {
t.Errorf("Cannot connect to the websocket %v", err)
}
defer c.Close()
done := make(chan bool)
go func() {
defer close(done)
tm, message, err := c.ReadMessage()
if err != nil {
t.Fatalf("Error in the message reception: %v (type %v)", err, tm)
}
if string(message) != string(test) {
t.Fatal("Message received should be the same as the message sent")
}
done <- true
}()
err = c.WriteMessage(websocket.TextMessage, test)
if err != nil {
t.Errorf("Cannot write %v to websocket: %v", test, err)
}
<-done
err = c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""))
if err != nil {
t.Errorf("write close: %v", err)
}
}
func BenchmarkServeWS(b *testing.B) {
// Now test the websocket
test := []byte("test")
wsURL := url.URL{Scheme: "ws", Host: tsURL.Host, Path: "/ws"}
for n := 0; n < b.N; n++ {
c, _, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
if err != nil {
b.Errorf("Cannot connect to the websocket %v", err)
}
defer c.Close()
done := make(chan bool)
go func() {
defer close(done)
tm, message, err := c.ReadMessage()
if err != nil {
b.Fatalf("Error in the message reception: %v (type %v)", err, tm)
}
if string(message) != string(test) {
b.Fatal("Message received should be the same as the message sent")
}
done <- true
}()
err = c.WriteMessage(websocket.TextMessage, test)
if err != nil {
b.Errorf("Cannot write %v to websocket: %v", test, err)
}
<-done
err = c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""))
if err != nil {
b.Errorf("write close: %v", err)
}
}
}