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.

193 lines
3.7 KiB

7 years ago
  1. package rpcclient
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net"
  6. "net/http"
  7. "net/http/httptest"
  8. "sync"
  9. "testing"
  10. "time"
  11. "github.com/gorilla/websocket"
  12. "github.com/stretchr/testify/require"
  13. "github.com/tendermint/tmlibs/log"
  14. types "github.com/tendermint/tendermint/rpc/lib/types"
  15. )
  16. type myHandler struct {
  17. closeConnAfterRead bool
  18. mtx sync.RWMutex
  19. }
  20. var upgrader = websocket.Upgrader{
  21. ReadBufferSize: 1024,
  22. WriteBufferSize: 1024,
  23. }
  24. func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. conn, err := upgrader.Upgrade(w, r, nil)
  26. if err != nil {
  27. panic(err)
  28. }
  29. defer conn.Close()
  30. for {
  31. messageType, _, err := conn.ReadMessage()
  32. if err != nil {
  33. return
  34. }
  35. h.mtx.RLock()
  36. if h.closeConnAfterRead {
  37. conn.Close()
  38. }
  39. h.mtx.RUnlock()
  40. res := json.RawMessage(`{}`)
  41. emptyRespBytes, _ := json.Marshal(types.RPCResponse{Result: &res})
  42. if err := conn.WriteMessage(messageType, emptyRespBytes); err != nil {
  43. return
  44. }
  45. }
  46. }
  47. func TestWSClientReconnectsAfterReadFailure(t *testing.T) {
  48. var wg sync.WaitGroup
  49. // start server
  50. h := &myHandler{}
  51. s := httptest.NewServer(h)
  52. defer s.Close()
  53. c := startClient(t, s.Listener.Addr())
  54. defer c.Stop()
  55. wg.Add(1)
  56. go callWgDoneOnResult(t, c, &wg)
  57. h.mtx.Lock()
  58. h.closeConnAfterRead = true
  59. h.mtx.Unlock()
  60. // results in WS read error, no send retry because write succeeded
  61. call(t, "a", c)
  62. // expect to reconnect almost immediately
  63. time.Sleep(10 * time.Millisecond)
  64. h.mtx.Lock()
  65. h.closeConnAfterRead = false
  66. h.mtx.Unlock()
  67. // should succeed
  68. call(t, "b", c)
  69. wg.Wait()
  70. }
  71. func TestWSClientReconnectsAfterWriteFailure(t *testing.T) {
  72. var wg sync.WaitGroup
  73. // start server
  74. h := &myHandler{}
  75. s := httptest.NewServer(h)
  76. c := startClient(t, s.Listener.Addr())
  77. defer c.Stop()
  78. wg.Add(2)
  79. go callWgDoneOnResult(t, c, &wg)
  80. // hacky way to abort the connection before write
  81. c.conn.Close()
  82. // results in WS write error, the client should resend on reconnect
  83. call(t, "a", c)
  84. // expect to reconnect almost immediately
  85. time.Sleep(10 * time.Millisecond)
  86. // should succeed
  87. call(t, "b", c)
  88. wg.Wait()
  89. }
  90. func TestWSClientReconnectFailure(t *testing.T) {
  91. // start server
  92. h := &myHandler{}
  93. s := httptest.NewServer(h)
  94. c := startClient(t, s.Listener.Addr())
  95. defer c.Stop()
  96. go func() {
  97. for {
  98. select {
  99. case <-c.ResultsCh:
  100. case <-c.ErrorsCh:
  101. case <-c.Quit:
  102. return
  103. }
  104. }
  105. }()
  106. // hacky way to abort the connection before write
  107. c.conn.Close()
  108. s.Close()
  109. // results in WS write error
  110. // provide timeout to avoid blocking
  111. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  112. defer cancel()
  113. c.Call(ctx, "a", make(map[string]interface{}))
  114. // expect to reconnect almost immediately
  115. time.Sleep(10 * time.Millisecond)
  116. done := make(chan struct{})
  117. go func() {
  118. // client should block on this
  119. call(t, "b", c)
  120. close(done)
  121. }()
  122. // test that client blocks on the second send
  123. select {
  124. case <-done:
  125. t.Fatal("client should block on calling 'b' during reconnect")
  126. case <-time.After(5 * time.Second):
  127. t.Log("All good")
  128. }
  129. }
  130. func startClient(t *testing.T, addr net.Addr) *WSClient {
  131. c := NewWSClient(addr.String(), "/websocket")
  132. _, err := c.Start()
  133. require.Nil(t, err)
  134. c.SetLogger(log.TestingLogger())
  135. return c
  136. }
  137. func call(t *testing.T, method string, c *WSClient) {
  138. err := c.Call(context.Background(), method, make(map[string]interface{}))
  139. require.NoError(t, err)
  140. }
  141. func callWgDoneOnResult(t *testing.T, c *WSClient, wg *sync.WaitGroup) {
  142. for {
  143. select {
  144. case res := <-c.ResultsCh:
  145. if res != nil {
  146. wg.Done()
  147. }
  148. case err := <-c.ErrorsCh:
  149. if err != nil {
  150. t.Fatalf("unexpected error: %v", err)
  151. }
  152. case <-c.Quit:
  153. return
  154. }
  155. }
  156. }