-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathserver_test.go
90 lines (74 loc) · 1.55 KB
/
server_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
package wserver
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"testing"
"time"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
func Test_Server_1(t *testing.T) {
port := 12345
userID := uuid.New().String()
event := "e1"
count := 100
s := NewServer(":" + strconv.Itoa(port))
// run wserver
go runBasicWServer(s)
time.Sleep(time.Millisecond * 300)
// push message
registerCh := make(chan struct{})
pushCh := make(chan struct{})
go func() {
<-registerCh
for i := 0; i < count; i++ {
msg := fmt.Sprintf("hello -- %d", i)
_, err := s.Push(userID, event, msg)
if err != nil {
t.Errorf("push message fail: %v", err)
}
// time.Sleep(time.Microsecond)
}
if _, err := s.Drop(userID, ""); err != nil {
t.Errorf("drop connections fail: %v", err)
}
close(pushCh)
}()
// dial websocket
url := fmt.Sprintf("ws://127.0.0.1:%d/ws", port)
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
t.Fatalf("dial websocket url fail: %v", err)
}
// register
rm := RegisterMessage{
Token: userID,
Event: event,
}
if err := conn.WriteJSON(rm); err != nil {
t.Fatalf("registe fail: %v", err)
}
time.Sleep(100 * time.Millisecond)
close(registerCh)
// read
cnt := 0
for {
_, r, err := conn.NextReader()
if err != nil {
break
}
b, _ := ioutil.ReadAll(r)
t.Logf("msg: %s", string(b))
cnt++
}
if cnt != count {
t.Errorf("message received count: %d, expected %d", cnt, count)
}
}
func runBasicWServer(s *Server) {
if err := s.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}