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.

375 lines
8.9 KiB

8 years ago
7 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package rpc
  2. import (
  3. "bytes"
  4. "context"
  5. crand "crypto/rand"
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "os"
  10. "os/exec"
  11. "testing"
  12. "time"
  13. "github.com/go-kit/kit/log/term"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. amino "github.com/tendermint/go-amino"
  17. cmn "github.com/tendermint/tendermint/libs/common"
  18. "github.com/tendermint/tendermint/libs/log"
  19. client "github.com/tendermint/tendermint/rpc/lib/client"
  20. server "github.com/tendermint/tendermint/rpc/lib/server"
  21. types "github.com/tendermint/tendermint/rpc/lib/types"
  22. )
  23. // Client and Server should work over tcp or unix sockets
  24. const (
  25. tcpAddr = "tcp://0.0.0.0:47768"
  26. unixSocket = "/tmp/rpc_test.sock"
  27. unixAddr = "unix://" + unixSocket
  28. websocketEndpoint = "/websocket/endpoint"
  29. )
  30. type ResultEcho struct {
  31. Value string `json:"value"`
  32. }
  33. type ResultEchoInt struct {
  34. Value int `json:"value"`
  35. }
  36. type ResultEchoBytes struct {
  37. Value []byte `json:"value"`
  38. }
  39. type ResultEchoDataBytes struct {
  40. Value cmn.HexBytes `json:"value"`
  41. }
  42. // Define some routes
  43. var Routes = map[string]*server.RPCFunc{
  44. "echo": server.NewRPCFunc(EchoResult, "arg"),
  45. "echo_ws": server.NewWSRPCFunc(EchoWSResult, "arg"),
  46. "echo_bytes": server.NewRPCFunc(EchoBytesResult, "arg"),
  47. "echo_data_bytes": server.NewRPCFunc(EchoDataBytesResult, "arg"),
  48. "echo_int": server.NewRPCFunc(EchoIntResult, "arg"),
  49. }
  50. // Amino codec required to encode/decode everything above.
  51. var RoutesCdc = amino.NewCodec()
  52. func EchoResult(ctx *types.Context, v string) (*ResultEcho, error) {
  53. return &ResultEcho{v}, nil
  54. }
  55. func EchoWSResult(ctx *types.Context, v string) (*ResultEcho, error) {
  56. return &ResultEcho{v}, nil
  57. }
  58. func EchoIntResult(ctx *types.Context, v int) (*ResultEchoInt, error) {
  59. return &ResultEchoInt{v}, nil
  60. }
  61. func EchoBytesResult(ctx *types.Context, v []byte) (*ResultEchoBytes, error) {
  62. return &ResultEchoBytes{v}, nil
  63. }
  64. func EchoDataBytesResult(ctx *types.Context, v cmn.HexBytes) (*ResultEchoDataBytes, error) {
  65. return &ResultEchoDataBytes{v}, nil
  66. }
  67. func TestMain(m *testing.M) {
  68. setup()
  69. code := m.Run()
  70. os.Exit(code)
  71. }
  72. var colorFn = func(keyvals ...interface{}) term.FgBgColor {
  73. for i := 0; i < len(keyvals)-1; i += 2 {
  74. if keyvals[i] == "socket" {
  75. if keyvals[i+1] == "tcp" {
  76. return term.FgBgColor{Fg: term.DarkBlue}
  77. } else if keyvals[i+1] == "unix" {
  78. return term.FgBgColor{Fg: term.DarkCyan}
  79. }
  80. }
  81. }
  82. return term.FgBgColor{}
  83. }
  84. // launch unix and tcp servers
  85. func setup() {
  86. logger := log.NewTMLoggerWithColorFn(log.NewSyncWriter(os.Stdout), colorFn)
  87. cmd := exec.Command("rm", "-f", unixSocket)
  88. err := cmd.Start()
  89. if err != nil {
  90. panic(err)
  91. }
  92. if err = cmd.Wait(); err != nil {
  93. panic(err)
  94. }
  95. tcpLogger := logger.With("socket", "tcp")
  96. mux := http.NewServeMux()
  97. server.RegisterRPCFuncs(mux, Routes, RoutesCdc, tcpLogger)
  98. wm := server.NewWebsocketManager(Routes, RoutesCdc, server.ReadWait(5*time.Second), server.PingPeriod(1*time.Second))
  99. wm.SetLogger(tcpLogger)
  100. mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
  101. listener1, err := server.Listen(tcpAddr, server.Config{})
  102. if err != nil {
  103. panic(err)
  104. }
  105. go server.StartHTTPServer(listener1, mux, tcpLogger)
  106. unixLogger := logger.With("socket", "unix")
  107. mux2 := http.NewServeMux()
  108. server.RegisterRPCFuncs(mux2, Routes, RoutesCdc, unixLogger)
  109. wm = server.NewWebsocketManager(Routes, RoutesCdc)
  110. wm.SetLogger(unixLogger)
  111. mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
  112. listener2, err := server.Listen(unixAddr, server.Config{})
  113. if err != nil {
  114. panic(err)
  115. }
  116. go server.StartHTTPServer(listener2, mux2, unixLogger)
  117. // wait for servers to start
  118. time.Sleep(time.Second * 2)
  119. }
  120. func echoViaHTTP(cl client.HTTPClient, val string) (string, error) {
  121. params := map[string]interface{}{
  122. "arg": val,
  123. }
  124. result := new(ResultEcho)
  125. if _, err := cl.Call("echo", params, result); err != nil {
  126. return "", err
  127. }
  128. return result.Value, nil
  129. }
  130. func echoIntViaHTTP(cl client.HTTPClient, val int) (int, error) {
  131. params := map[string]interface{}{
  132. "arg": val,
  133. }
  134. result := new(ResultEchoInt)
  135. if _, err := cl.Call("echo_int", params, result); err != nil {
  136. return 0, err
  137. }
  138. return result.Value, nil
  139. }
  140. func echoBytesViaHTTP(cl client.HTTPClient, bytes []byte) ([]byte, error) {
  141. params := map[string]interface{}{
  142. "arg": bytes,
  143. }
  144. result := new(ResultEchoBytes)
  145. if _, err := cl.Call("echo_bytes", params, result); err != nil {
  146. return []byte{}, err
  147. }
  148. return result.Value, nil
  149. }
  150. func echoDataBytesViaHTTP(cl client.HTTPClient, bytes cmn.HexBytes) (cmn.HexBytes, error) {
  151. params := map[string]interface{}{
  152. "arg": bytes,
  153. }
  154. result := new(ResultEchoDataBytes)
  155. if _, err := cl.Call("echo_data_bytes", params, result); err != nil {
  156. return []byte{}, err
  157. }
  158. return result.Value, nil
  159. }
  160. func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
  161. val := "acbd"
  162. got, err := echoViaHTTP(cl, val)
  163. require.Nil(t, err)
  164. assert.Equal(t, got, val)
  165. val2 := randBytes(t)
  166. got2, err := echoBytesViaHTTP(cl, val2)
  167. require.Nil(t, err)
  168. assert.Equal(t, got2, val2)
  169. val3 := cmn.HexBytes(randBytes(t))
  170. got3, err := echoDataBytesViaHTTP(cl, val3)
  171. require.Nil(t, err)
  172. assert.Equal(t, got3, val3)
  173. val4 := cmn.RandIntn(10000)
  174. got4, err := echoIntViaHTTP(cl, val4)
  175. require.Nil(t, err)
  176. assert.Equal(t, got4, val4)
  177. }
  178. func echoViaWS(cl *client.WSClient, val string) (string, error) {
  179. params := map[string]interface{}{
  180. "arg": val,
  181. }
  182. err := cl.Call(context.Background(), "echo", params)
  183. if err != nil {
  184. return "", err
  185. }
  186. msg := <-cl.ResponsesCh
  187. if msg.Error != nil {
  188. return "", err
  189. }
  190. result := new(ResultEcho)
  191. err = json.Unmarshal(msg.Result, result)
  192. if err != nil {
  193. return "", nil
  194. }
  195. return result.Value, nil
  196. }
  197. func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
  198. params := map[string]interface{}{
  199. "arg": bytes,
  200. }
  201. err := cl.Call(context.Background(), "echo_bytes", params)
  202. if err != nil {
  203. return []byte{}, err
  204. }
  205. msg := <-cl.ResponsesCh
  206. if msg.Error != nil {
  207. return []byte{}, msg.Error
  208. }
  209. result := new(ResultEchoBytes)
  210. err = json.Unmarshal(msg.Result, result)
  211. if err != nil {
  212. return []byte{}, nil
  213. }
  214. return result.Value, nil
  215. }
  216. func testWithWSClient(t *testing.T, cl *client.WSClient) {
  217. val := "acbd"
  218. got, err := echoViaWS(cl, val)
  219. require.Nil(t, err)
  220. assert.Equal(t, got, val)
  221. val2 := randBytes(t)
  222. got2, err := echoBytesViaWS(cl, val2)
  223. require.Nil(t, err)
  224. assert.Equal(t, got2, val2)
  225. }
  226. //-------------
  227. func TestServersAndClientsBasic(t *testing.T) {
  228. serverAddrs := [...]string{tcpAddr, unixAddr}
  229. for _, addr := range serverAddrs {
  230. cl1 := client.NewURIClient(addr)
  231. fmt.Printf("=== testing server on %s using URI client", addr)
  232. testWithHTTPClient(t, cl1)
  233. cl2 := client.NewJSONRPCClient(addr)
  234. fmt.Printf("=== testing server on %s using JSONRPC client", addr)
  235. testWithHTTPClient(t, cl2)
  236. cl3 := client.NewWSClient(addr, websocketEndpoint)
  237. cl3.SetLogger(log.TestingLogger())
  238. err := cl3.Start()
  239. require.Nil(t, err)
  240. fmt.Printf("=== testing server on %s using WS client", addr)
  241. testWithWSClient(t, cl3)
  242. cl3.Stop()
  243. }
  244. }
  245. func TestHexStringArg(t *testing.T) {
  246. cl := client.NewURIClient(tcpAddr)
  247. // should NOT be handled as hex
  248. val := "0xabc"
  249. got, err := echoViaHTTP(cl, val)
  250. require.Nil(t, err)
  251. assert.Equal(t, got, val)
  252. }
  253. func TestQuotedStringArg(t *testing.T) {
  254. cl := client.NewURIClient(tcpAddr)
  255. // should NOT be unquoted
  256. val := "\"abc\""
  257. got, err := echoViaHTTP(cl, val)
  258. require.Nil(t, err)
  259. assert.Equal(t, got, val)
  260. }
  261. func TestWSNewWSRPCFunc(t *testing.T) {
  262. cl := client.NewWSClient(tcpAddr, websocketEndpoint)
  263. cl.SetLogger(log.TestingLogger())
  264. err := cl.Start()
  265. require.Nil(t, err)
  266. defer cl.Stop()
  267. val := "acbd"
  268. params := map[string]interface{}{
  269. "arg": val,
  270. }
  271. err = cl.Call(context.Background(), "echo_ws", params)
  272. require.Nil(t, err)
  273. msg := <-cl.ResponsesCh
  274. if msg.Error != nil {
  275. t.Fatal(err)
  276. }
  277. result := new(ResultEcho)
  278. err = json.Unmarshal(msg.Result, result)
  279. require.Nil(t, err)
  280. got := result.Value
  281. assert.Equal(t, got, val)
  282. }
  283. func TestWSHandlesArrayParams(t *testing.T) {
  284. cl := client.NewWSClient(tcpAddr, websocketEndpoint)
  285. cl.SetLogger(log.TestingLogger())
  286. err := cl.Start()
  287. require.Nil(t, err)
  288. defer cl.Stop()
  289. val := "acbd"
  290. params := []interface{}{val}
  291. err = cl.CallWithArrayParams(context.Background(), "echo_ws", params)
  292. require.Nil(t, err)
  293. msg := <-cl.ResponsesCh
  294. if msg.Error != nil {
  295. t.Fatalf("%+v", err)
  296. }
  297. result := new(ResultEcho)
  298. err = json.Unmarshal(msg.Result, result)
  299. require.Nil(t, err)
  300. got := result.Value
  301. assert.Equal(t, got, val)
  302. }
  303. // TestWSClientPingPong checks that a client & server exchange pings
  304. // & pongs so connection stays alive.
  305. func TestWSClientPingPong(t *testing.T) {
  306. cl := client.NewWSClient(tcpAddr, websocketEndpoint)
  307. cl.SetLogger(log.TestingLogger())
  308. err := cl.Start()
  309. require.Nil(t, err)
  310. defer cl.Stop()
  311. time.Sleep(6 * time.Second)
  312. }
  313. func randBytes(t *testing.T) []byte {
  314. n := cmn.RandIntn(10) + 2
  315. buf := make([]byte, n)
  316. _, err := crand.Read(buf)
  317. require.Nil(t, err)
  318. return bytes.Replace(buf, []byte("="), []byte{100}, -1)
  319. }