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.

248 lines
5.1 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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/fortytw2/leaktest"
  11. "github.com/gorilla/websocket"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  15. )
  16. const wsCallTimeout = 5 * time.Second
  17. type myTestHandler struct {
  18. closeConnAfterRead bool
  19. mtx sync.RWMutex
  20. t *testing.T
  21. }
  22. var upgrader = websocket.Upgrader{
  23. ReadBufferSize: 1024,
  24. WriteBufferSize: 1024,
  25. }
  26. func (h *myTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. conn, err := upgrader.Upgrade(w, r, nil)
  28. require.NoError(h.t, err)
  29. defer conn.Close()
  30. for {
  31. messageType, in, err := conn.ReadMessage()
  32. if err != nil {
  33. return
  34. }
  35. var req rpctypes.RPCRequest
  36. err = json.Unmarshal(in, &req)
  37. require.NoError(h.t, err)
  38. func() {
  39. h.mtx.RLock()
  40. defer h.mtx.RUnlock()
  41. if h.closeConnAfterRead {
  42. require.NoError(h.t, conn.Close())
  43. }
  44. }()
  45. res := json.RawMessage(`{}`)
  46. emptyRespBytes, err := json.Marshal(req.MakeResponse(res))
  47. require.NoError(h.t, err)
  48. if err := conn.WriteMessage(messageType, emptyRespBytes); err != nil {
  49. return
  50. }
  51. }
  52. }
  53. func TestWSClientReconnectsAfterReadFailure(t *testing.T) {
  54. t.Cleanup(leaktest.Check(t))
  55. // start server
  56. h := &myTestHandler{t: t}
  57. s := httptest.NewServer(h)
  58. defer s.Close()
  59. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  60. defer cancel()
  61. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  62. go handleResponses(ctx, t, c)
  63. h.mtx.Lock()
  64. h.closeConnAfterRead = true
  65. h.mtx.Unlock()
  66. // results in WS read error, no send retry because write succeeded
  67. call(ctx, t, "a", c)
  68. // expect to reconnect almost immediately
  69. time.Sleep(10 * time.Millisecond)
  70. h.mtx.Lock()
  71. h.closeConnAfterRead = false
  72. h.mtx.Unlock()
  73. // should succeed
  74. call(ctx, t, "b", c)
  75. }
  76. func TestWSClientReconnectsAfterWriteFailure(t *testing.T) {
  77. t.Cleanup(leaktest.Check(t))
  78. // start server
  79. h := &myTestHandler{t: t}
  80. s := httptest.NewServer(h)
  81. defer s.Close()
  82. ctx, cancel := context.WithCancel(context.Background())
  83. defer cancel()
  84. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  85. go handleResponses(ctx, t, c)
  86. // hacky way to abort the connection before write
  87. if err := c.conn.Close(); err != nil {
  88. t.Error(err)
  89. }
  90. // results in WS write error, the client should resend on reconnect
  91. call(ctx, t, "a", c)
  92. // expect to reconnect almost immediately
  93. time.Sleep(10 * time.Millisecond)
  94. // should succeed
  95. call(ctx, t, "b", c)
  96. }
  97. func TestWSClientReconnectFailure(t *testing.T) {
  98. t.Cleanup(leaktest.Check(t))
  99. // start server
  100. h := &myTestHandler{t: t}
  101. s := httptest.NewServer(h)
  102. ctx, cancel := context.WithCancel(context.Background())
  103. defer cancel()
  104. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  105. go func() {
  106. for {
  107. select {
  108. case <-c.ResponsesCh:
  109. case <-ctx.Done():
  110. return
  111. }
  112. }
  113. }()
  114. // hacky way to abort the connection before write
  115. if err := c.conn.Close(); err != nil {
  116. t.Error(err)
  117. }
  118. s.Close()
  119. // results in WS write error
  120. // provide timeout to avoid blocking
  121. cctx, cancel := context.WithTimeout(ctx, wsCallTimeout)
  122. defer cancel()
  123. if err := c.Call(cctx, "a", make(map[string]interface{})); err != nil {
  124. t.Error(err)
  125. }
  126. // expect to reconnect almost immediately
  127. time.Sleep(10 * time.Millisecond)
  128. done := make(chan struct{})
  129. go func() {
  130. // client should block on this
  131. call(ctx, t, "b", c)
  132. close(done)
  133. }()
  134. // test that client blocks on the second send
  135. select {
  136. case <-done:
  137. t.Fatal("client should block on calling 'b' during reconnect")
  138. case <-time.After(5 * time.Second):
  139. t.Log("All good")
  140. }
  141. }
  142. func TestNotBlockingOnStop(t *testing.T) {
  143. t.Cleanup(leaktest.Check(t))
  144. s := httptest.NewServer(&myTestHandler{t: t})
  145. defer s.Close()
  146. ctx, cancel := context.WithCancel(context.Background())
  147. defer cancel()
  148. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  149. require.NoError(t, c.Call(ctx, "a", make(map[string]interface{})))
  150. time.Sleep(200 * time.Millisecond) // give service routines time to start ⚠️
  151. done := make(chan struct{})
  152. go func() {
  153. cancel()
  154. if assert.NoError(t, c.Stop()) {
  155. close(done)
  156. }
  157. }()
  158. select {
  159. case <-done:
  160. t.Log("Stopped client successfully")
  161. case <-time.After(2 * time.Second):
  162. t.Fatal("Timed out waiting for client to stop")
  163. }
  164. }
  165. func startClient(ctx context.Context, t *testing.T, addr string) *WSClient {
  166. t.Helper()
  167. t.Cleanup(leaktest.Check(t))
  168. c, err := NewWS(addr, "/websocket")
  169. require.NoError(t, err)
  170. require.NoError(t, c.Start(ctx))
  171. return c
  172. }
  173. func call(ctx context.Context, t *testing.T, method string, c *WSClient) {
  174. t.Helper()
  175. err := c.Call(ctx, method, make(map[string]interface{}))
  176. if ctx.Err() == nil {
  177. require.NoError(t, err)
  178. }
  179. }
  180. func handleResponses(ctx context.Context, t *testing.T, c *WSClient) {
  181. t.Helper()
  182. for {
  183. select {
  184. case resp := <-c.ResponsesCh:
  185. if resp.Error != nil {
  186. t.Errorf("unexpected error: %v", resp.Error)
  187. return
  188. }
  189. if resp.Result != nil {
  190. return
  191. }
  192. case <-ctx.Done():
  193. return
  194. }
  195. }
  196. }