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.

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