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.

159 lines
3.9 KiB

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