forked from madari/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketio_test.go
155 lines (129 loc) · 3.25 KB
/
socketio_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
package socketio
import (
"http"
"testing"
"time"
"fmt"
)
const (
serverAddr = "127.0.0.1:6060"
eventConnect = iota
eventDisconnect
eventMessage
eventCrash
)
var events chan *event
var server *SocketIO
type event struct {
conn *Conn
eventType uint8
msg Message
}
func echoServer(addr string, config *Config) <-chan *event {
events := make(chan *event)
server = NewSocketIO(config)
server.OnConnect(func(c *Conn) {
events <- &event{c, eventConnect, nil}
})
server.OnDisconnect(func(c *Conn) {
events <- &event{c, eventDisconnect, nil}
})
server.OnMessage(func(c *Conn, msg Message) {
if err := c.Send(msg.Data()); err != nil {
fmt.Println("server echo send error: ", err)
}
events <- &event{c, eventMessage, msg}
})
go func() {
http.ListenAndServe(addr, server.ServeMux())
events <- &event{nil, eventCrash, nil}
}()
return events
}
func TestWebsocket(t *testing.T) {
finished := make(chan bool, 1)
clientMessage := make(chan Message)
clientDisconnect := make(chan bool)
numMessages := 313
config := DefaultConfig
config.QueueLength = numMessages * 2
config.Codec = SIOStreamingCodec{}
config.Origins = []string{serverAddr}
serverEvents := echoServer(serverAddr, &config)
client := NewWebsocketClient(SIOStreamingCodec{})
client.OnMessage(func(msg Message) {
clientMessage <- msg
})
client.OnDisconnect(func() {
clientDisconnect <- true
})
time.Sleep(1e9)
/*
go func() {
time.Sleep(5e9)
if _, ok := <-finished; !ok {
t.Fatalf("timeout")
}
}()*/
err := client.Dial("ws://"+serverAddr+"/socket.io/websocket", "http://"+serverAddr+"/")
if err != nil {
t.Fatal(err)
}
// expect connection
serverEvent := <-serverEvents
if serverEvent.eventType != eventConnect || serverEvent.conn.sessionid != client.SessionID() {
t.Fatalf("Expected eventConnect but got %v", serverEvent)
}
iook := make(chan bool)
go func() {
for i := 0; i < numMessages; i++ {
if err = client.Send(i); err != nil {
t.Fatal("Send:", err)
}
}
iook <- true
}()
go func() {
for i := 0; i < numMessages; i++ {
serverEvent = <-serverEvents
t.Logf("Server event %q", serverEvent)
expect := fmt.Sprintf("%d", i)
if serverEvent.eventType != eventMessage || serverEvent.conn.sessionid != client.SessionID() {
t.Fatalf("Expected eventMessage but got %#v", serverEvent)
}
if serverEvent.msg.Data() != expect {
t.Fatalf("Server expected %s but received %s", expect, serverEvent.msg.Data())
} else {
t.Logf("Server received %s", serverEvent.msg.Data())
}
}
iook <- true
}()
go func() {
for i := 0; i < numMessages; i++ {
msg := <-clientMessage
t.Log("Client received", msg.Data())
expect := fmt.Sprintf("%d", i)
if msg.Data() != expect {
t.Fatalf("Client expected %s but received %s", expect, msg.Data())
}
}
iook <- true
}()
for i := 0; i < 3; i++ {
<-iook
}
go func() {
if err = client.Close(); err != nil {
t.Fatal("Close:", err)
}
}()
t.Log("Waiting for client disconnect")
<-clientDisconnect
t.Log("Waiting for server disconnect")
serverEvent = <-serverEvents
if serverEvent.eventType != eventDisconnect || serverEvent.conn.sessionid != client.SessionID() {
t.Fatalf("Expected disconnect event, but got %q", serverEvent)
}
finished <- true
}