websocket 增加多分组 fork https://github.com/olahol/melody
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
6.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package melody
  2. import (
  3. "github.com/gorilla/websocket"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. "testing/quick"
  9. "time"
  10. )
  11. type TestServer struct {
  12. m *Melody
  13. }
  14. func NewTestServerHandler(handler handleMessageFunc) *TestServer {
  15. m := New()
  16. m.HandleMessage(handler)
  17. return &TestServer{
  18. m: m,
  19. }
  20. }
  21. func NewTestServer() *TestServer {
  22. m := New()
  23. return &TestServer{
  24. m: m,
  25. }
  26. }
  27. func (s *TestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. s.m.HandleRequest(w, r)
  29. }
  30. func NewDialer(url string) (*websocket.Conn, error) {
  31. dialer := &websocket.Dialer{}
  32. conn, _, err := dialer.Dial(strings.Replace(url, "http", "ws", 1), nil)
  33. return conn, err
  34. }
  35. func TestEcho(t *testing.T) {
  36. echo := NewTestServerHandler(func(session *Session, msg []byte) {
  37. session.Write(msg)
  38. })
  39. server := httptest.NewServer(echo)
  40. defer server.Close()
  41. fn := func(msg string) bool {
  42. conn, err := NewDialer(server.URL)
  43. defer conn.Close()
  44. if err != nil {
  45. t.Error(err)
  46. return false
  47. }
  48. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  49. _, ret, err := conn.ReadMessage()
  50. if err != nil {
  51. t.Error(err)
  52. return false
  53. }
  54. if msg != string(ret) {
  55. t.Errorf("%s should equal %s", msg, string(ret))
  56. return false
  57. }
  58. return true
  59. }
  60. if err := quick.Check(fn, nil); err != nil {
  61. t.Error(err)
  62. }
  63. }
  64. func TestEchoBinary(t *testing.T) {
  65. echo := NewTestServer()
  66. echo.m.HandleMessageBinary(func(session *Session, msg []byte) {
  67. session.WriteBinary(msg)
  68. })
  69. server := httptest.NewServer(echo)
  70. defer server.Close()
  71. fn := func(msg string) bool {
  72. conn, err := NewDialer(server.URL)
  73. defer conn.Close()
  74. if err != nil {
  75. t.Error(err)
  76. return false
  77. }
  78. conn.WriteMessage(websocket.BinaryMessage, []byte(msg))
  79. _, ret, err := conn.ReadMessage()
  80. if err != nil {
  81. t.Error(err)
  82. return false
  83. }
  84. if msg != string(ret) {
  85. t.Errorf("%s should equal %s", msg, string(ret))
  86. return false
  87. }
  88. return true
  89. }
  90. if err := quick.Check(fn, nil); err != nil {
  91. t.Error(err)
  92. }
  93. }
  94. func TestHandlers(t *testing.T) {
  95. echo := NewTestServer()
  96. echo.m.HandleMessage(func(session *Session, msg []byte) {
  97. session.Write(msg)
  98. })
  99. server := httptest.NewServer(echo)
  100. defer server.Close()
  101. var q *Session
  102. echo.m.HandleConnect(func(session *Session) {
  103. q = session
  104. session.Close()
  105. })
  106. echo.m.HandleDisconnect(func(session *Session) {
  107. if q != session {
  108. t.Error("disconnecting session should be the same as connecting")
  109. }
  110. })
  111. NewDialer(server.URL)
  112. }
  113. func TestUpgrader(t *testing.T) {
  114. broadcast := NewTestServer()
  115. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  116. session.Write(msg)
  117. })
  118. server := httptest.NewServer(broadcast)
  119. defer server.Close()
  120. broadcast.m.Upgrader = &websocket.Upgrader{
  121. ReadBufferSize: 1024,
  122. WriteBufferSize: 1024,
  123. CheckOrigin: func(r *http.Request) bool { return false },
  124. }
  125. broadcast.m.HandleError(func(session *Session, err error) {
  126. if err == nil || err.Error() != "websocket: origin not allowed" {
  127. t.Error("there should be a origin error")
  128. }
  129. })
  130. _, err := NewDialer(server.URL)
  131. if err == nil || err.Error() != "websocket: bad handshake" {
  132. t.Error("there should be a badhandshake error")
  133. }
  134. }
  135. func TestBroadcast(t *testing.T) {
  136. broadcast := NewTestServer()
  137. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  138. broadcast.m.Broadcast(msg)
  139. })
  140. server := httptest.NewServer(broadcast)
  141. defer server.Close()
  142. n := 10
  143. fn := func(msg string) bool {
  144. conn, _ := NewDialer(server.URL)
  145. defer conn.Close()
  146. listeners := make([]*websocket.Conn, n)
  147. for i := 0; i < n; i++ {
  148. listener, _ := NewDialer(server.URL)
  149. listeners[i] = listener
  150. defer listeners[i].Close()
  151. }
  152. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  153. for i := 0; i < n; i++ {
  154. _, ret, err := listeners[i].ReadMessage()
  155. if err != nil {
  156. t.Error(err)
  157. return false
  158. }
  159. if msg != string(ret) {
  160. t.Errorf("%s should equal %s", msg, string(ret))
  161. return false
  162. }
  163. }
  164. return true
  165. }
  166. if !fn("test") {
  167. t.Errorf("should not be false")
  168. }
  169. }
  170. func TestBroadcastOthers(t *testing.T) {
  171. broadcast := NewTestServer()
  172. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  173. broadcast.m.BroadcastOthers(msg, session)
  174. })
  175. broadcast.m.Config.PongWait = time.Second
  176. broadcast.m.Config.PingPeriod = time.Second * 9 / 10
  177. server := httptest.NewServer(broadcast)
  178. defer server.Close()
  179. n := 10
  180. fn := func(msg string) bool {
  181. conn, _ := NewDialer(server.URL)
  182. defer conn.Close()
  183. listeners := make([]*websocket.Conn, n)
  184. for i := 0; i < n; i++ {
  185. listener, _ := NewDialer(server.URL)
  186. listeners[i] = listener
  187. defer listeners[i].Close()
  188. }
  189. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  190. for i := 0; i < n; i++ {
  191. _, ret, err := listeners[i].ReadMessage()
  192. if err != nil {
  193. t.Error(err)
  194. return false
  195. }
  196. if msg != string(ret) {
  197. t.Errorf("%s should equal %s", msg, string(ret))
  198. return false
  199. }
  200. }
  201. return true
  202. }
  203. if !fn("test") {
  204. t.Errorf("should not be false")
  205. }
  206. }
  207. func TestPingPong(t *testing.T) {
  208. noecho := NewTestServer()
  209. noecho.m.Config.PongWait = time.Second
  210. noecho.m.Config.PingPeriod = time.Second * 9 / 10
  211. server := httptest.NewServer(noecho)
  212. defer server.Close()
  213. conn, err := NewDialer(server.URL)
  214. conn.SetPingHandler(func(string) error {
  215. return nil
  216. })
  217. defer conn.Close()
  218. if err != nil {
  219. t.Error(err)
  220. }
  221. conn.WriteMessage(websocket.TextMessage, []byte("test"))
  222. _, _, err = conn.ReadMessage()
  223. if err == nil {
  224. t.Error("there should be an error")
  225. }
  226. }
  227. func TestBroadcastFilter(t *testing.T) {
  228. broadcast := NewTestServer()
  229. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  230. broadcast.m.BroadcastFilter(msg, func(q *Session) bool {
  231. return session == q
  232. })
  233. })
  234. server := httptest.NewServer(broadcast)
  235. defer server.Close()
  236. fn := func(msg string) bool {
  237. conn, err := NewDialer(server.URL)
  238. defer conn.Close()
  239. if err != nil {
  240. t.Error(err)
  241. return false
  242. }
  243. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  244. _, ret, err := conn.ReadMessage()
  245. if err != nil {
  246. t.Error(err)
  247. return false
  248. }
  249. if msg != string(ret) {
  250. t.Errorf("%s should equal %s", msg, string(ret))
  251. return false
  252. }
  253. return true
  254. }
  255. if !fn("test") {
  256. t.Errorf("should not be false")
  257. }
  258. }
  259. func TestStop(t *testing.T) {
  260. noecho := NewTestServer()
  261. server := httptest.NewServer(noecho)
  262. defer server.Close()
  263. conn, err := NewDialer(server.URL)
  264. defer conn.Close()
  265. if err != nil {
  266. t.Error(err)
  267. }
  268. noecho.m.Close()
  269. }
  270. func TestSmallMessageBuffer(t *testing.T) {
  271. echo := NewTestServerHandler(func(session *Session, msg []byte) {
  272. session.Write(msg)
  273. })
  274. echo.m.Config.MessageBufferSize = 0
  275. echo.m.HandleError(func(s *Session, err error) {
  276. if err == nil {
  277. t.Error("there should be a buffer full error here")
  278. }
  279. })
  280. server := httptest.NewServer(echo)
  281. defer server.Close()
  282. conn, err := NewDialer(server.URL)
  283. defer conn.Close()
  284. if err != nil {
  285. t.Error(err)
  286. }
  287. conn.WriteMessage(websocket.TextMessage, []byte("12345"))
  288. }