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.

181 lines
4.7 KiB

8 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
8 years ago
8 years ago
8 years ago
8 years ago
  1. package rpctest
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/stretchr/testify/require"
  12. logger "github.com/tendermint/tmlibs/logger"
  13. abci "github.com/tendermint/abci/types"
  14. cfg "github.com/tendermint/tendermint/config"
  15. nm "github.com/tendermint/tendermint/node"
  16. "github.com/tendermint/tendermint/proxy"
  17. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  18. core_grpc "github.com/tendermint/tendermint/rpc/grpc"
  19. client "github.com/tendermint/tendermint/rpc/lib/client"
  20. "github.com/tendermint/tendermint/types"
  21. )
  22. var config *cfg.Config
  23. const tmLogLevel = "error"
  24. // f**ing long, but unique for each test
  25. func makePathname() string {
  26. // get path
  27. p, err := os.Getwd()
  28. if err != nil {
  29. panic(err)
  30. }
  31. fmt.Println(p)
  32. sep := string(filepath.Separator)
  33. return strings.Replace(p, sep, "_", -1)
  34. }
  35. func randPort() int {
  36. // returns between base and base + spread
  37. base, spread := 20000, 20000
  38. return base + rand.Intn(spread)
  39. }
  40. func makeAddrs() (string, string, string) {
  41. start := randPort()
  42. return fmt.Sprintf("tcp://0.0.0.0:%d", start),
  43. fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
  44. fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
  45. }
  46. // GetConfig returns a config for the test cases as a singleton
  47. func GetConfig() *cfg.Config {
  48. if config == nil {
  49. pathname := makePathname()
  50. config = cfg.ResetTestRoot(pathname)
  51. // and we use random ports to run in parallel
  52. tm, rpc, grpc := makeAddrs()
  53. config.P2P.ListenAddress = tm
  54. config.RPCListenAddress = rpc
  55. config.GRPCListenAddress = grpc
  56. // Shut up the logging
  57. logger.SetLogLevel(tmLogLevel)
  58. }
  59. return config
  60. }
  61. // GetURIClient gets a uri client pointing to the test tendermint rpc
  62. func GetURIClient() *client.URIClient {
  63. rpcAddr := GetConfig().RPCListenAddress
  64. return client.NewURIClient(rpcAddr)
  65. }
  66. // GetJSONClient gets a http/json client pointing to the test tendermint rpc
  67. func GetJSONClient() *client.JSONRPCClient {
  68. rpcAddr := GetConfig().RPCListenAddress
  69. return client.NewJSONRPCClient(rpcAddr)
  70. }
  71. func GetGRPCClient() core_grpc.BroadcastAPIClient {
  72. grpcAddr := config.GRPCListenAddress
  73. return core_grpc.StartGRPCClient(grpcAddr)
  74. }
  75. func GetWSClient() *client.WSClient {
  76. rpcAddr := GetConfig().RPCListenAddress
  77. wsc := client.NewWSClient(rpcAddr, "/websocket")
  78. if _, err := wsc.Start(); err != nil {
  79. panic(err)
  80. }
  81. return wsc
  82. }
  83. // StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
  84. func StartTendermint(app abci.Application) *nm.Node {
  85. node := NewTendermint(app)
  86. node.Start()
  87. fmt.Println("Tendermint running!")
  88. return node
  89. }
  90. // NewTendermint creates a new tendermint server and sleeps forever
  91. func NewTendermint(app abci.Application) *nm.Node {
  92. // Create & start node
  93. config := GetConfig()
  94. privValidatorFile := config.PrivValidatorFile()
  95. privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
  96. papp := proxy.NewLocalClientCreator(app)
  97. node := nm.NewNode(config, privValidator, papp)
  98. return node
  99. }
  100. //--------------------------------------------------------------------------------
  101. // Utilities for testing the websocket service
  102. // wait for an event; do things that might trigger events, and check them when they are received
  103. // the check function takes an event id and the byte slice read off the ws
  104. func waitForEvent(t *testing.T, wsc *client.WSClient, eventid string, dieOnTimeout bool, f func(), check func(string, interface{}) error) {
  105. // go routine to wait for webscoket msg
  106. goodCh := make(chan interface{})
  107. errCh := make(chan error)
  108. // Read message
  109. go func() {
  110. var err error
  111. LOOP:
  112. for {
  113. select {
  114. case r := <-wsc.ResultsCh:
  115. result := new(ctypes.ResultEvent)
  116. err = json.Unmarshal(r, result)
  117. if err != nil {
  118. // cant distinguish between error and wrong type ...
  119. continue
  120. }
  121. if result.Name == eventid {
  122. goodCh <- result.Data
  123. break LOOP
  124. }
  125. case err := <-wsc.ErrorsCh:
  126. errCh <- err
  127. break LOOP
  128. case <-wsc.Quit:
  129. break LOOP
  130. }
  131. }
  132. }()
  133. // do stuff (transactions)
  134. f()
  135. // wait for an event or timeout
  136. timeout := time.NewTimer(10 * time.Second)
  137. select {
  138. case <-timeout.C:
  139. if dieOnTimeout {
  140. wsc.Stop()
  141. require.True(t, false, "%s event was not received in time", eventid)
  142. }
  143. // else that's great, we didn't hear the event
  144. // and we shouldn't have
  145. case eventData := <-goodCh:
  146. if dieOnTimeout {
  147. // message was received and expected
  148. // run the check
  149. require.Nil(t, check(eventid, eventData))
  150. } else {
  151. wsc.Stop()
  152. require.True(t, false, "%s event was not expected", eventid)
  153. }
  154. case err := <-errCh:
  155. panic(err) // Show the stack trace.
  156. }
  157. }
  158. //--------------------------------------------------------------------------------