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.

166 lines
4.1 KiB

  1. package rpctest
  2. import (
  3. "testing"
  4. "time"
  5. . "github.com/tendermint/go-common"
  6. cfg "github.com/tendermint/go-config"
  7. "github.com/tendermint/go-p2p"
  8. "github.com/tendermint/go-wire"
  9. client "github.com/tendermint/go-rpc/client"
  10. "github.com/tendermint/tendermint/config/tendermint_test"
  11. nm "github.com/tendermint/tendermint/node"
  12. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  13. "github.com/tendermint/tendermint/rpc/grpc"
  14. )
  15. // global variables for use across all tests
  16. var (
  17. config cfg.Config
  18. node *nm.Node
  19. chainID string
  20. rpcAddr string
  21. requestAddr string
  22. websocketAddr string
  23. websocketEndpoint string
  24. grpcAddr string
  25. clientURI *client.ClientURI
  26. clientJSON *client.ClientJSONRPC
  27. clientGRPC core_grpc.BroadcastAPIClient
  28. )
  29. // initialize config and create new node
  30. func init() {
  31. config = tendermint_test.ResetConfig("rpc_test_client_test")
  32. chainID = config.GetString("chain_id")
  33. rpcAddr = config.GetString("rpc_laddr")
  34. grpcAddr = config.GetString("grpc_laddr")
  35. requestAddr = rpcAddr
  36. websocketAddr = rpcAddr
  37. websocketEndpoint = "/websocket"
  38. clientURI = client.NewClientURI(requestAddr)
  39. clientJSON = client.NewClientJSONRPC(requestAddr)
  40. clientGRPC = core_grpc.StartGRPCClient(grpcAddr)
  41. // TODO: change consensus/state.go timeouts to be shorter
  42. // start a node
  43. ready := make(chan struct{})
  44. go newNode(ready)
  45. <-ready
  46. }
  47. // create a new node and sleep forever
  48. func newNode(ready chan struct{}) {
  49. // Create & start node
  50. node = nm.NewNodeDefault(config)
  51. protocol, address := nm.ProtocolAndAddress(config.GetString("node_laddr"))
  52. l := p2p.NewDefaultListener(protocol, address, true)
  53. node.AddListener(l)
  54. node.Start()
  55. // Run the RPC server.
  56. node.StartRPC()
  57. time.Sleep(time.Second)
  58. ready <- struct{}{}
  59. // Sleep forever
  60. ch := make(chan struct{})
  61. <-ch
  62. }
  63. //--------------------------------------------------------------------------------
  64. // Utilities for testing the websocket service
  65. // create a new connection
  66. func newWSClient(t *testing.T) *client.WSClient {
  67. wsc := client.NewWSClient(websocketAddr, websocketEndpoint)
  68. if _, err := wsc.Start(); err != nil {
  69. panic(err)
  70. }
  71. return wsc
  72. }
  73. // subscribe to an event
  74. func subscribe(t *testing.T, wsc *client.WSClient, eventid string) {
  75. if err := wsc.Subscribe(eventid); err != nil {
  76. panic(err)
  77. }
  78. }
  79. // unsubscribe from an event
  80. func unsubscribe(t *testing.T, wsc *client.WSClient, eventid string) {
  81. if err := wsc.Unsubscribe(eventid); err != nil {
  82. panic(err)
  83. }
  84. }
  85. // wait for an event; do things that might trigger events, and check them when they are received
  86. // the check function takes an event id and the byte slice read off the ws
  87. func waitForEvent(t *testing.T, wsc *client.WSClient, eventid string, dieOnTimeout bool, f func(), check func(string, interface{}) error) {
  88. // go routine to wait for webscoket msg
  89. goodCh := make(chan interface{})
  90. errCh := make(chan error)
  91. // Read message
  92. go func() {
  93. var err error
  94. LOOP:
  95. for {
  96. select {
  97. case r := <-wsc.ResultsCh:
  98. result := new(ctypes.TMResult)
  99. wire.ReadJSONPtr(result, r, &err)
  100. if err != nil {
  101. errCh <- err
  102. break LOOP
  103. }
  104. event, ok := (*result).(*ctypes.ResultEvent)
  105. if ok && event.Name == eventid {
  106. goodCh <- event.Data
  107. break LOOP
  108. }
  109. case err := <-wsc.ErrorsCh:
  110. errCh <- err
  111. break LOOP
  112. case <-wsc.Quit:
  113. break LOOP
  114. }
  115. }
  116. }()
  117. // do stuff (transactions)
  118. f()
  119. // wait for an event or timeout
  120. timeout := time.NewTimer(10 * time.Second)
  121. select {
  122. case <-timeout.C:
  123. if dieOnTimeout {
  124. wsc.Stop()
  125. panic(Fmt("%s event was not received in time", eventid))
  126. }
  127. // else that's great, we didn't hear the event
  128. // and we shouldn't have
  129. case eventData := <-goodCh:
  130. if dieOnTimeout {
  131. // message was received and expected
  132. // run the check
  133. if err := check(eventid, eventData); err != nil {
  134. panic(err) // Show the stack trace.
  135. }
  136. } else {
  137. wsc.Stop()
  138. panic(Fmt("%s event was not expected", eventid))
  139. }
  140. case err := <-errCh:
  141. panic(err) // Show the stack trace.
  142. }
  143. }
  144. //--------------------------------------------------------------------------------