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.

239 lines
4.4 KiB

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 := Default()
  16. m.HandleMessage(handler)
  17. return &TestServer{
  18. m: m,
  19. }
  20. }
  21. func NewTestServer() *TestServer {
  22. m := Default()
  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 TestBroadcast(t *testing.T) {
  65. broadcast := NewTestServer()
  66. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  67. broadcast.m.Broadcast(msg)
  68. })
  69. server := httptest.NewServer(broadcast)
  70. defer server.Close()
  71. n := 10
  72. fn := func(msg string) bool {
  73. conn, _ := NewDialer(server.URL)
  74. defer conn.Close()
  75. listeners := make([]*websocket.Conn, n)
  76. for i := 0; i < n; i++ {
  77. listener, _ := NewDialer(server.URL)
  78. listeners[i] = listener
  79. defer listeners[i].Close()
  80. }
  81. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  82. for i := 0; i < n; i++ {
  83. _, ret, err := listeners[i].ReadMessage()
  84. if err != nil {
  85. t.Error(err)
  86. return false
  87. }
  88. if msg != string(ret) {
  89. t.Errorf("%s should equal %s", msg, string(ret))
  90. return false
  91. }
  92. }
  93. return true
  94. }
  95. if err := quick.Check(fn, nil); err != nil {
  96. t.Error(err)
  97. }
  98. }
  99. func TestPingPong(t *testing.T) {
  100. noecho := NewTestServer()
  101. noecho.m.Config.PongWait = time.Second
  102. noecho.m.Config.PingPeriod = time.Second * 9 / 10
  103. server := httptest.NewServer(noecho)
  104. defer server.Close()
  105. conn, err := NewDialer(server.URL)
  106. conn.SetPingHandler(func(string) error {
  107. return nil
  108. })
  109. defer conn.Close()
  110. if err != nil {
  111. t.Error(err)
  112. }
  113. conn.WriteMessage(websocket.TextMessage, []byte("test"))
  114. _, _, err = conn.ReadMessage()
  115. if err == nil {
  116. t.Error("there should be an error")
  117. }
  118. }
  119. /*
  120. func TestBroadcastFilter(t *testing.T) {
  121. echo := NewTestServer()
  122. echo.m.HandleMessage(func(session *Session, msg []byte) {
  123. echo.m.BroadcastFilter(func(s *Session) bool {
  124. //return s == session
  125. return false
  126. }, msg)
  127. })
  128. server := httptest.NewServer(echo)
  129. defer server.Close()
  130. fn := func(msg string) bool {
  131. conn, err := NewDialer(server.URL)
  132. conn.SetPingHandler(func(string) error {
  133. return nil
  134. })
  135. defer conn.Close()
  136. if err != nil {
  137. t.Error(err)
  138. return false
  139. }
  140. conn.WriteMessage(websocket.TextMessage, []byte(msg))
  141. _, ret, err := conn.ReadMessage()
  142. if err != nil {
  143. t.Error(err)
  144. return false
  145. }
  146. if msg != string(ret) {
  147. t.Errorf("%s should equal %s", msg, string(ret))
  148. return false
  149. }
  150. return true
  151. }
  152. if err := quick.Check(fn, nil); err != nil {
  153. t.Error(err)
  154. }
  155. }
  156. */
  157. func BenchmarkEcho(b *testing.B) {
  158. echo := NewTestServerHandler(func(session *Session, msg []byte) {
  159. session.Write(msg)
  160. })
  161. server := httptest.NewServer(echo)
  162. defer server.Close()
  163. conn, _ := NewDialer(server.URL)
  164. defer conn.Close()
  165. for i := 0; i < b.N; i++ {
  166. conn.WriteMessage(websocket.TextMessage, []byte("test"))
  167. conn.ReadMessage()
  168. }
  169. }
  170. func BenchmarkBroadcast(b *testing.B) {
  171. broadcast := NewTestServer()
  172. broadcast.m.HandleMessage(func(session *Session, msg []byte) {
  173. broadcast.m.Broadcast(msg)
  174. })
  175. server := httptest.NewServer(broadcast)
  176. defer server.Close()
  177. conn, _ := NewDialer(server.URL)
  178. defer conn.Close()
  179. n := 10
  180. listeners := make([]*websocket.Conn, n)
  181. for i := 0; i < n; i++ {
  182. listener, _ := NewDialer(server.URL)
  183. listeners[i] = listener
  184. defer listeners[i].Close()
  185. }
  186. for i := 0; i < b.N; i++ {
  187. conn.WriteMessage(websocket.TextMessage, []byte("test"))
  188. for i := 0; i < n; i++ {
  189. listeners[i].ReadMessage()
  190. }
  191. }
  192. }