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.

261 lines
5.4 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
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
7 years ago
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "runtime"
  8. "sync"
  9. "testing"
  10. "time"
  11. "github.com/fortytw2/leaktest"
  12. "github.com/gorilla/websocket"
  13. "github.com/stretchr/testify/require"
  14. "github.com/tendermint/tendermint/libs/log"
  15. rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  16. )
  17. var wsCallTimeout = 5 * time.Second
  18. type myHandler struct {
  19. closeConnAfterRead bool
  20. mtx sync.RWMutex
  21. }
  22. var upgrader = websocket.Upgrader{
  23. ReadBufferSize: 1024,
  24. WriteBufferSize: 1024,
  25. }
  26. func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. conn, err := upgrader.Upgrade(w, r, nil)
  28. if err != nil {
  29. panic(err)
  30. }
  31. defer conn.Close()
  32. for {
  33. messageType, in, err := conn.ReadMessage()
  34. if err != nil {
  35. return
  36. }
  37. var req rpctypes.RPCRequest
  38. err = json.Unmarshal(in, &req)
  39. if err != nil {
  40. panic(err)
  41. }
  42. h.mtx.RLock()
  43. if h.closeConnAfterRead {
  44. if err := conn.Close(); err != nil {
  45. panic(err)
  46. }
  47. }
  48. h.mtx.RUnlock()
  49. res := json.RawMessage(`{}`)
  50. emptyRespBytes, _ := json.Marshal(rpctypes.RPCResponse{Result: res, ID: req.ID})
  51. if err := conn.WriteMessage(messageType, emptyRespBytes); err != nil {
  52. return
  53. }
  54. }
  55. }
  56. func TestWSClientReconnectsAfterReadFailure(t *testing.T) {
  57. t.Cleanup(leaktest.Check(t))
  58. // start server
  59. h := &myHandler{}
  60. s := httptest.NewServer(h)
  61. defer s.Close()
  62. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  63. defer cancel()
  64. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  65. go handleResponses(ctx, t, c)
  66. h.mtx.Lock()
  67. h.closeConnAfterRead = true
  68. h.mtx.Unlock()
  69. // results in WS read error, no send retry because write succeeded
  70. call(ctx, t, "a", c)
  71. // expect to reconnect almost immediately
  72. time.Sleep(10 * time.Millisecond)
  73. h.mtx.Lock()
  74. h.closeConnAfterRead = false
  75. h.mtx.Unlock()
  76. // should succeed
  77. call(ctx, t, "b", c)
  78. }
  79. func TestWSClientReconnectsAfterWriteFailure(t *testing.T) {
  80. t.Cleanup(leaktest.Check(t))
  81. // start server
  82. h := &myHandler{}
  83. s := httptest.NewServer(h)
  84. defer s.Close()
  85. ctx, cancel := context.WithCancel(context.Background())
  86. defer cancel()
  87. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  88. go handleResponses(ctx, t, c)
  89. // hacky way to abort the connection before write
  90. if err := c.conn.Close(); err != nil {
  91. t.Error(err)
  92. }
  93. // results in WS write error, the client should resend on reconnect
  94. call(ctx, t, "a", c)
  95. // expect to reconnect almost immediately
  96. time.Sleep(10 * time.Millisecond)
  97. // should succeed
  98. call(ctx, t, "b", c)
  99. }
  100. func TestWSClientReconnectFailure(t *testing.T) {
  101. t.Cleanup(leaktest.Check(t))
  102. // start server
  103. h := &myHandler{}
  104. s := httptest.NewServer(h)
  105. ctx, cancel := context.WithCancel(context.Background())
  106. defer cancel()
  107. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  108. go func() {
  109. for {
  110. select {
  111. case <-c.ResponsesCh:
  112. case <-ctx.Done():
  113. return
  114. }
  115. }
  116. }()
  117. // hacky way to abort the connection before write
  118. if err := c.conn.Close(); err != nil {
  119. t.Error(err)
  120. }
  121. s.Close()
  122. // results in WS write error
  123. // provide timeout to avoid blocking
  124. cctx, cancel := context.WithTimeout(ctx, wsCallTimeout)
  125. defer cancel()
  126. if err := c.Call(cctx, "a", make(map[string]interface{})); err != nil {
  127. t.Error(err)
  128. }
  129. // expect to reconnect almost immediately
  130. time.Sleep(10 * time.Millisecond)
  131. done := make(chan struct{})
  132. go func() {
  133. // client should block on this
  134. call(ctx, t, "b", c)
  135. close(done)
  136. }()
  137. // test that client blocks on the second send
  138. select {
  139. case <-done:
  140. t.Fatal("client should block on calling 'b' during reconnect")
  141. case <-time.After(5 * time.Second):
  142. t.Log("All good")
  143. }
  144. }
  145. func TestNotBlockingOnStop(t *testing.T) {
  146. t.Cleanup(leaktest.Check(t))
  147. timeout := 3 * time.Second
  148. s := httptest.NewServer(&myHandler{})
  149. defer s.Close()
  150. ctx, cancel := context.WithCancel(context.Background())
  151. defer cancel()
  152. c := startClient(ctx, t, "//"+s.Listener.Addr().String())
  153. c.Call(ctx, "a", make(map[string]interface{})) // nolint:errcheck // ignore for tests
  154. // Let the readRoutine get around to blocking
  155. time.Sleep(time.Second)
  156. passCh := make(chan struct{})
  157. go func() {
  158. // Unless we have a non-blocking write to ResponsesCh from readRoutine
  159. // this blocks forever ont the waitgroup
  160. cancel()
  161. require.NoError(t, c.Stop())
  162. select {
  163. case <-ctx.Done():
  164. case passCh <- struct{}{}:
  165. }
  166. }()
  167. runtime.Gosched() // hacks: force context switch
  168. select {
  169. case <-passCh:
  170. // Pass
  171. case <-time.After(timeout):
  172. if c.IsRunning() {
  173. t.Fatalf("WSClient did failed to stop within %v seconds - is one of the read/write routines blocking?",
  174. timeout.Seconds())
  175. }
  176. }
  177. }
  178. func startClient(ctx context.Context, t *testing.T, addr string) *WSClient {
  179. t.Helper()
  180. opts := DefaultWSOptions()
  181. opts.SkipMetrics = true
  182. c, err := NewWSWithOptions(addr, "/websocket", opts)
  183. require.Nil(t, err)
  184. err = c.Start(ctx)
  185. require.Nil(t, err)
  186. c.Logger = log.TestingLogger()
  187. return c
  188. }
  189. func call(ctx context.Context, t *testing.T, method string, c *WSClient) {
  190. t.Helper()
  191. err := c.Call(ctx, method, make(map[string]interface{}))
  192. if ctx.Err() == nil {
  193. require.NoError(t, err)
  194. }
  195. }
  196. func handleResponses(ctx context.Context, t *testing.T, c *WSClient) {
  197. t.Helper()
  198. for {
  199. select {
  200. case resp := <-c.ResponsesCh:
  201. if resp.Error != nil {
  202. t.Errorf("unexpected error: %v", resp.Error)
  203. return
  204. }
  205. if resp.Result != nil {
  206. return
  207. }
  208. case <-ctx.Done():
  209. return
  210. }
  211. }
  212. }