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.

404 lines
9.3 KiB

8 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
8 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 jsonrpc
  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. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  17. "github.com/tendermint/tendermint/libs/log"
  18. tmrand "github.com/tendermint/tendermint/libs/rand"
  19. client "github.com/tendermint/tendermint/rpc/jsonrpc/client"
  20. server "github.com/tendermint/tendermint/rpc/jsonrpc/server"
  21. types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  22. )
  23. // Client and Server should work over tcp or unix sockets
  24. const (
  25. tcpAddr = "tcp://127.0.0.1:47768"
  26. unixSocket = "/tmp/rpc_test.sock"
  27. unixAddr = "unix://" + unixSocket
  28. websocketEndpoint = "/websocket/endpoint"
  29. testVal = "acbd"
  30. )
  31. type ResultEcho struct {
  32. Value string `json:"value"`
  33. }
  34. type ResultEchoInt struct {
  35. Value int `json:"value"`
  36. }
  37. type ResultEchoBytes struct {
  38. Value []byte `json:"value"`
  39. }
  40. type ResultEchoDataBytes struct {
  41. Value tmbytes.HexBytes `json:"value"`
  42. }
  43. // Define some routes
  44. var Routes = map[string]*server.RPCFunc{
  45. "echo": server.NewRPCFunc(EchoResult, "arg"),
  46. "echo_ws": server.NewWSRPCFunc(EchoWSResult, "arg"),
  47. "echo_bytes": server.NewRPCFunc(EchoBytesResult, "arg"),
  48. "echo_data_bytes": server.NewRPCFunc(EchoDataBytesResult, "arg"),
  49. "echo_int": server.NewRPCFunc(EchoIntResult, "arg"),
  50. }
  51. func EchoResult(ctx *types.Context, v string) (*ResultEcho, error) {
  52. return &ResultEcho{v}, nil
  53. }
  54. func EchoWSResult(ctx *types.Context, v string) (*ResultEcho, error) {
  55. return &ResultEcho{v}, nil
  56. }
  57. func EchoIntResult(ctx *types.Context, v int) (*ResultEchoInt, error) {
  58. return &ResultEchoInt{v}, nil
  59. }
  60. func EchoBytesResult(ctx *types.Context, v []byte) (*ResultEchoBytes, error) {
  61. return &ResultEchoBytes{v}, nil
  62. }
  63. func EchoDataBytesResult(ctx *types.Context, v tmbytes.HexBytes) (*ResultEchoDataBytes, error) {
  64. return &ResultEchoDataBytes{v}, nil
  65. }
  66. func TestMain(m *testing.M) {
  67. setup()
  68. code := m.Run()
  69. os.Exit(code)
  70. }
  71. var colorFn = func(keyvals ...interface{}) term.FgBgColor {
  72. for i := 0; i < len(keyvals)-1; i += 2 {
  73. if keyvals[i] == "socket" {
  74. if keyvals[i+1] == "tcp" {
  75. return term.FgBgColor{Fg: term.DarkBlue}
  76. } else if keyvals[i+1] == "unix" {
  77. return term.FgBgColor{Fg: term.DarkCyan}
  78. }
  79. }
  80. }
  81. return term.FgBgColor{}
  82. }
  83. // launch unix and tcp servers
  84. func setup() {
  85. logger := log.NewTMLoggerWithColorFn(log.NewSyncWriter(os.Stdout), colorFn)
  86. cmd := exec.Command("rm", "-f", unixSocket)
  87. err := cmd.Start()
  88. if err != nil {
  89. panic(err)
  90. }
  91. if err = cmd.Wait(); err != nil {
  92. panic(err)
  93. }
  94. tcpLogger := logger.With("socket", "tcp")
  95. mux := http.NewServeMux()
  96. server.RegisterRPCFuncs(mux, Routes, tcpLogger)
  97. wm := server.NewWebsocketManager(Routes, server.ReadWait(5*time.Second), server.PingPeriod(1*time.Second))
  98. wm.SetLogger(tcpLogger)
  99. mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
  100. config := server.DefaultConfig()
  101. listener1, err := server.Listen(tcpAddr, config)
  102. if err != nil {
  103. panic(err)
  104. }
  105. go func() {
  106. if err := server.Serve(listener1, mux, tcpLogger, config); err != nil {
  107. panic(err)
  108. }
  109. }()
  110. unixLogger := logger.With("socket", "unix")
  111. mux2 := http.NewServeMux()
  112. server.RegisterRPCFuncs(mux2, Routes, unixLogger)
  113. wm = server.NewWebsocketManager(Routes)
  114. wm.SetLogger(unixLogger)
  115. mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
  116. listener2, err := server.Listen(unixAddr, config)
  117. if err != nil {
  118. panic(err)
  119. }
  120. go func() {
  121. if err := server.Serve(listener2, mux2, unixLogger, config); err != nil {
  122. panic(err)
  123. }
  124. }()
  125. // wait for servers to start
  126. time.Sleep(time.Second * 2)
  127. }
  128. func echoViaHTTP(cl client.Caller, val string) (string, error) {
  129. params := map[string]interface{}{
  130. "arg": val,
  131. }
  132. result := new(ResultEcho)
  133. if _, err := cl.Call("echo", params, result); err != nil {
  134. return "", err
  135. }
  136. return result.Value, nil
  137. }
  138. func echoIntViaHTTP(cl client.Caller, val int) (int, error) {
  139. params := map[string]interface{}{
  140. "arg": val,
  141. }
  142. result := new(ResultEchoInt)
  143. if _, err := cl.Call("echo_int", params, result); err != nil {
  144. return 0, err
  145. }
  146. return result.Value, nil
  147. }
  148. func echoBytesViaHTTP(cl client.Caller, bytes []byte) ([]byte, error) {
  149. params := map[string]interface{}{
  150. "arg": bytes,
  151. }
  152. result := new(ResultEchoBytes)
  153. if _, err := cl.Call("echo_bytes", params, result); err != nil {
  154. return []byte{}, err
  155. }
  156. return result.Value, nil
  157. }
  158. func echoDataBytesViaHTTP(cl client.Caller, bytes tmbytes.HexBytes) (tmbytes.HexBytes, error) {
  159. params := map[string]interface{}{
  160. "arg": bytes,
  161. }
  162. result := new(ResultEchoDataBytes)
  163. if _, err := cl.Call("echo_data_bytes", params, result); err != nil {
  164. return []byte{}, err
  165. }
  166. return result.Value, nil
  167. }
  168. func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
  169. val := testVal
  170. got, err := echoViaHTTP(cl, val)
  171. require.Nil(t, err)
  172. assert.Equal(t, got, val)
  173. val2 := randBytes(t)
  174. got2, err := echoBytesViaHTTP(cl, val2)
  175. require.Nil(t, err)
  176. assert.Equal(t, got2, val2)
  177. val3 := tmbytes.HexBytes(randBytes(t))
  178. got3, err := echoDataBytesViaHTTP(cl, val3)
  179. require.Nil(t, err)
  180. assert.Equal(t, got3, val3)
  181. val4 := tmrand.Intn(10000)
  182. got4, err := echoIntViaHTTP(cl, val4)
  183. require.Nil(t, err)
  184. assert.Equal(t, got4, val4)
  185. }
  186. func echoViaWS(cl *client.WSClient, val string) (string, error) {
  187. params := map[string]interface{}{
  188. "arg": val,
  189. }
  190. err := cl.Call(context.Background(), "echo", params)
  191. if err != nil {
  192. return "", err
  193. }
  194. msg := <-cl.ResponsesCh
  195. if msg.Error != nil {
  196. return "", err
  197. }
  198. result := new(ResultEcho)
  199. err = json.Unmarshal(msg.Result, result)
  200. if err != nil {
  201. return "", nil
  202. }
  203. return result.Value, nil
  204. }
  205. func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
  206. params := map[string]interface{}{
  207. "arg": bytes,
  208. }
  209. err := cl.Call(context.Background(), "echo_bytes", params)
  210. if err != nil {
  211. return []byte{}, err
  212. }
  213. msg := <-cl.ResponsesCh
  214. if msg.Error != nil {
  215. return []byte{}, msg.Error
  216. }
  217. result := new(ResultEchoBytes)
  218. err = json.Unmarshal(msg.Result, result)
  219. if err != nil {
  220. return []byte{}, nil
  221. }
  222. return result.Value, nil
  223. }
  224. func testWithWSClient(t *testing.T, cl *client.WSClient) {
  225. val := testVal
  226. got, err := echoViaWS(cl, val)
  227. require.Nil(t, err)
  228. assert.Equal(t, got, val)
  229. val2 := randBytes(t)
  230. got2, err := echoBytesViaWS(cl, val2)
  231. require.Nil(t, err)
  232. assert.Equal(t, got2, val2)
  233. }
  234. //-------------
  235. func TestServersAndClientsBasic(t *testing.T) {
  236. serverAddrs := [...]string{tcpAddr, unixAddr}
  237. for _, addr := range serverAddrs {
  238. cl1, err := client.NewURI(addr)
  239. require.Nil(t, err)
  240. fmt.Printf("=== testing server on %s using URI client", addr)
  241. testWithHTTPClient(t, cl1)
  242. cl2, err := client.New(addr)
  243. require.Nil(t, err)
  244. fmt.Printf("=== testing server on %s using JSONRPC client", addr)
  245. testWithHTTPClient(t, cl2)
  246. cl3, err := client.NewWS(addr, websocketEndpoint)
  247. require.Nil(t, err)
  248. cl3.SetLogger(log.TestingLogger())
  249. err = cl3.Start()
  250. require.Nil(t, err)
  251. fmt.Printf("=== testing server on %s using WS client", addr)
  252. testWithWSClient(t, cl3)
  253. err = cl3.Stop()
  254. require.NoError(t, err)
  255. }
  256. }
  257. func TestHexStringArg(t *testing.T) {
  258. cl, err := client.NewURI(tcpAddr)
  259. require.Nil(t, err)
  260. // should NOT be handled as hex
  261. val := "0xabc"
  262. got, err := echoViaHTTP(cl, val)
  263. require.Nil(t, err)
  264. assert.Equal(t, got, val)
  265. }
  266. func TestQuotedStringArg(t *testing.T) {
  267. cl, err := client.NewURI(tcpAddr)
  268. require.Nil(t, err)
  269. // should NOT be unquoted
  270. val := "\"abc\""
  271. got, err := echoViaHTTP(cl, val)
  272. require.Nil(t, err)
  273. assert.Equal(t, got, val)
  274. }
  275. func TestWSNewWSRPCFunc(t *testing.T) {
  276. cl, err := client.NewWS(tcpAddr, websocketEndpoint)
  277. require.Nil(t, err)
  278. cl.SetLogger(log.TestingLogger())
  279. err = cl.Start()
  280. require.Nil(t, err)
  281. t.Cleanup(func() {
  282. if err := cl.Stop(); err != nil {
  283. t.Error(err)
  284. }
  285. })
  286. val := testVal
  287. params := map[string]interface{}{
  288. "arg": val,
  289. }
  290. err = cl.Call(context.Background(), "echo_ws", params)
  291. require.Nil(t, err)
  292. msg := <-cl.ResponsesCh
  293. if msg.Error != nil {
  294. t.Fatal(err)
  295. }
  296. result := new(ResultEcho)
  297. err = json.Unmarshal(msg.Result, result)
  298. require.Nil(t, err)
  299. got := result.Value
  300. assert.Equal(t, got, val)
  301. }
  302. func TestWSHandlesArrayParams(t *testing.T) {
  303. cl, err := client.NewWS(tcpAddr, websocketEndpoint)
  304. require.Nil(t, err)
  305. cl.SetLogger(log.TestingLogger())
  306. err = cl.Start()
  307. require.Nil(t, err)
  308. t.Cleanup(func() {
  309. if err := cl.Stop(); err != nil {
  310. t.Error(err)
  311. }
  312. })
  313. val := testVal
  314. params := []interface{}{val}
  315. err = cl.CallWithArrayParams(context.Background(), "echo_ws", params)
  316. require.Nil(t, err)
  317. msg := <-cl.ResponsesCh
  318. if msg.Error != nil {
  319. t.Fatalf("%+v", err)
  320. }
  321. result := new(ResultEcho)
  322. err = json.Unmarshal(msg.Result, result)
  323. require.Nil(t, err)
  324. got := result.Value
  325. assert.Equal(t, got, val)
  326. }
  327. // TestWSClientPingPong checks that a client & server exchange pings
  328. // & pongs so connection stays alive.
  329. func TestWSClientPingPong(t *testing.T) {
  330. cl, err := client.NewWS(tcpAddr, websocketEndpoint)
  331. require.Nil(t, err)
  332. cl.SetLogger(log.TestingLogger())
  333. err = cl.Start()
  334. require.Nil(t, err)
  335. t.Cleanup(func() {
  336. if err := cl.Stop(); err != nil {
  337. t.Error(err)
  338. }
  339. })
  340. time.Sleep(6 * time.Second)
  341. }
  342. func randBytes(t *testing.T) []byte {
  343. n := tmrand.Intn(10) + 2
  344. buf := make([]byte, n)
  345. _, err := crand.Read(buf)
  346. require.Nil(t, err)
  347. return bytes.Replace(buf, []byte("="), []byte{100}, -1)
  348. }