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.

233 lines
4.9 KiB

rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
7 years ago
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/gorilla/websocket"
  11. "github.com/stretchr/testify/require"
  12. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  13. "github.com/tendermint/tendermint/libs/log"
  14. types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  15. )
  16. var wsCallTimeout = 5 * time.Second
  17. type myHandler struct {
  18. closeConnAfterRead bool
  19. mtx tmsync.RWMutex
  20. }
  21. var upgrader = websocket.Upgrader{
  22. ReadBufferSize: 1024,
  23. WriteBufferSize: 1024,
  24. }
  25. func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  26. conn, err := upgrader.Upgrade(w, r, nil)
  27. if err != nil {
  28. panic(err)
  29. }
  30. defer conn.Close()
  31. for {
  32. messageType, in, err := conn.ReadMessage()
  33. if err != nil {
  34. return
  35. }
  36. var req types.RPCRequest
  37. err = json.Unmarshal(in, &req)
  38. if err != nil {
  39. panic(err)
  40. }
  41. h.mtx.RLock()
  42. if h.closeConnAfterRead {
  43. if err := conn.Close(); err != nil {
  44. panic(err)
  45. }
  46. }
  47. h.mtx.RUnlock()
  48. res := json.RawMessage(`{}`)
  49. emptyRespBytes, _ := json.Marshal(types.RPCResponse{Result: res, ID: req.ID})
  50. if err := conn.WriteMessage(messageType, emptyRespBytes); err != nil {
  51. return
  52. }
  53. }
  54. }
  55. func TestWSClientReconnectsAfterReadFailure(t *testing.T) {
  56. var wg sync.WaitGroup
  57. // start server
  58. h := &myHandler{}
  59. s := httptest.NewServer(h)
  60. defer s.Close()
  61. c := startClient(t, "//"+s.Listener.Addr().String())
  62. defer c.Stop() // nolint:errcheck // ignore for tests
  63. wg.Add(1)
  64. go callWgDoneOnResult(t, c, &wg)
  65. h.mtx.Lock()
  66. h.closeConnAfterRead = true
  67. h.mtx.Unlock()
  68. // results in WS read error, no send retry because write succeeded
  69. call(t, "a", c)
  70. // expect to reconnect almost immediately
  71. time.Sleep(10 * time.Millisecond)
  72. h.mtx.Lock()
  73. h.closeConnAfterRead = false
  74. h.mtx.Unlock()
  75. // should succeed
  76. call(t, "b", c)
  77. wg.Wait()
  78. }
  79. func TestWSClientReconnectsAfterWriteFailure(t *testing.T) {
  80. var wg sync.WaitGroup
  81. // start server
  82. h := &myHandler{}
  83. s := httptest.NewServer(h)
  84. c := startClient(t, "//"+s.Listener.Addr().String())
  85. defer c.Stop() // nolint:errcheck // ignore for tests
  86. wg.Add(2)
  87. go callWgDoneOnResult(t, c, &wg)
  88. // hacky way to abort the connection before write
  89. if err := c.conn.Close(); err != nil {
  90. t.Error(err)
  91. }
  92. // results in WS write error, the client should resend on reconnect
  93. call(t, "a", c)
  94. // expect to reconnect almost immediately
  95. time.Sleep(10 * time.Millisecond)
  96. // should succeed
  97. call(t, "b", c)
  98. wg.Wait()
  99. }
  100. func TestWSClientReconnectFailure(t *testing.T) {
  101. // start server
  102. h := &myHandler{}
  103. s := httptest.NewServer(h)
  104. c := startClient(t, "//"+s.Listener.Addr().String())
  105. defer c.Stop() // nolint:errcheck // ignore for tests
  106. go func() {
  107. for {
  108. select {
  109. case <-c.ResponsesCh:
  110. case <-c.Quit():
  111. return
  112. }
  113. }
  114. }()
  115. // hacky way to abort the connection before write
  116. if err := c.conn.Close(); err != nil {
  117. t.Error(err)
  118. }
  119. s.Close()
  120. // results in WS write error
  121. // provide timeout to avoid blocking
  122. ctx, cancel := context.WithTimeout(context.Background(), wsCallTimeout)
  123. defer cancel()
  124. if err := c.Call(ctx, "a", make(map[string]interface{})); err != nil {
  125. t.Error(err)
  126. }
  127. // expect to reconnect almost immediately
  128. time.Sleep(10 * time.Millisecond)
  129. done := make(chan struct{})
  130. go func() {
  131. // client should block on this
  132. call(t, "b", c)
  133. close(done)
  134. }()
  135. // test that client blocks on the second send
  136. select {
  137. case <-done:
  138. t.Fatal("client should block on calling 'b' during reconnect")
  139. case <-time.After(5 * time.Second):
  140. t.Log("All good")
  141. }
  142. }
  143. func TestNotBlockingOnStop(t *testing.T) {
  144. timeout := 2 * time.Second
  145. s := httptest.NewServer(&myHandler{})
  146. c := startClient(t, "//"+s.Listener.Addr().String())
  147. c.Call(context.Background(), "a", make(map[string]interface{})) // nolint:errcheck // ignore for tests
  148. // Let the readRoutine get around to blocking
  149. time.Sleep(time.Second)
  150. passCh := make(chan struct{})
  151. go func() {
  152. // Unless we have a non-blocking write to ResponsesCh from readRoutine
  153. // this blocks forever ont the waitgroup
  154. err := c.Stop()
  155. require.NoError(t, err)
  156. passCh <- struct{}{}
  157. }()
  158. select {
  159. case <-passCh:
  160. // Pass
  161. case <-time.After(timeout):
  162. t.Fatalf("WSClient did failed to stop within %v seconds - is one of the read/write routines blocking?",
  163. timeout.Seconds())
  164. }
  165. }
  166. func startClient(t *testing.T, addr string) *WSClient {
  167. c, err := NewWS(addr, "/websocket")
  168. require.Nil(t, err)
  169. err = c.Start()
  170. require.Nil(t, err)
  171. c.SetLogger(log.TestingLogger())
  172. return c
  173. }
  174. func call(t *testing.T, method string, c *WSClient) {
  175. err := c.Call(context.Background(), method, make(map[string]interface{}))
  176. require.NoError(t, err)
  177. }
  178. func callWgDoneOnResult(t *testing.T, c *WSClient, wg *sync.WaitGroup) {
  179. for {
  180. select {
  181. case resp := <-c.ResponsesCh:
  182. if resp.Error != nil {
  183. t.Errorf("unexpected error: %v", resp.Error)
  184. return
  185. }
  186. if resp.Result != nil {
  187. wg.Done()
  188. }
  189. case <-c.Quit():
  190. return
  191. }
  192. }
  193. }