This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxim_test.go
139 lines (109 loc) · 2.6 KB
/
maxim_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
package maxim
import (
"net"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWrite(t *testing.T) {
assert := assert.New(t)
l, err := net.Listen("tcp", ":8080")
assert.NoError(err)
m := NewDefault()
m.HandleMessage(func(s *Session, msg string) {
assert.Equal("Hello", msg)
err := s.Write(msg + ", world")
assert.NoError(err)
})
go func() {
http.HandleFunc("/ws", m.HandleRequest)
http.Serve(l, nil)
}()
c, _, err := NewClient(&ClientConfig{
Address: "ws://localhost:8080/ws",
})
assert.NoError(err)
err = c.Write("Hello")
assert.NoError(err)
msg, err := c.Read()
assert.NoError(err)
assert.Equal("Hello, world", msg)
err = c.Close()
assert.NoError(err)
err = l.Close()
assert.NoError(err)
}
type MyHandler struct {
a *assert.Assertions
hasMessage bool
hasMessageBinary bool
hasError bool
hasClose bool
hasDisconnect bool
hasConnect bool
}
func (h *MyHandler) HandleMessage(s *Session, msg string) {
h.hasMessage = true
h.a.Equal("Hello", msg)
err := s.Write(msg + ", world")
h.a.NoError(err)
}
func (h *MyHandler) HandleMessageBinary(s *Session, msg []byte) {
h.hasMessageBinary = true
h.a.Equal("Hello", string(msg))
err := s.WriteBinary([]byte(string(msg) + ", world"))
h.a.NoError(err)
}
func (h *MyHandler) HandleError(s *Session, err error) {
h.hasError = true
}
func (h *MyHandler) HandleClose(s *Session, c CloseStatus, msg string) error {
h.hasClose = true
return nil
}
func (h *MyHandler) HandleDisconnect(s *Session) {
h.hasDisconnect = true
}
func (h *MyHandler) HandleConnect(s *Session) {
h.hasConnect = true
}
func TestHandle(t *testing.T) {
assert := assert.New(t)
h := &MyHandler{a: assert}
l, err := net.Listen("tcp", ":8080")
assert.NoError(err)
m := NewDefault()
m.Handle(h)
go func() {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/ws", m.HandleRequest)
http.Serve(l, nil)
}()
c, _, err := NewClient(&ClientConfig{
Address: "ws://localhost:8080/ws",
})
assert.NoError(err)
err = c.Write("Hello")
assert.NoError(err)
msg, err := c.Read()
assert.NoError(err)
assert.Equal("Hello, world", msg)
err = c.WriteBinary([]byte("Hello"))
assert.NoError(err)
msgBin, err := c.ReadBinary()
assert.NoError(err)
assert.Equal([]byte("Hello, world"), msgBin)
err = c.Close()
assert.NoError(err)
// 等待 Close 與 Disconnect。
<-time.After(1 * time.Second)
assert.True(h.hasMessage)
assert.True(h.hasMessageBinary)
//assert.True(h.hasError)
assert.True(h.hasClose)
assert.True(h.hasDisconnect)
assert.True(h.hasConnect)
err = l.Close()
assert.NoError(err)
}