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.

129 lines
3.6 KiB

  1. /*
  2. package mock returns a Client implementation that
  3. accepts various (mock) implementations of the various methods.
  4. This implementation is useful for using in tests, when you don't
  5. need a real server, but want a high-level of control about
  6. the server response you want to mock (eg. error handling),
  7. or if you just want to record the calls to verify in your tests.
  8. For real clients, you probably want the "http" package. If you
  9. want to directly call a tendermint node in process, you can use the
  10. "local" package.
  11. */
  12. package mock
  13. import (
  14. "reflect"
  15. data "github.com/tendermint/go-wire/data"
  16. "github.com/tendermint/tendermint/rpc/client"
  17. "github.com/tendermint/tendermint/rpc/core"
  18. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  19. "github.com/tendermint/tendermint/types"
  20. )
  21. // Client wraps arbitrary implementations of the various interfaces.
  22. //
  23. // We provide a few choices to mock out each one in this package.
  24. // Nothing hidden here, so no New function, just construct it from
  25. // some parts, and swap them out them during the tests.
  26. type Client struct {
  27. client.ABCIClient
  28. client.SignClient
  29. client.HistoryClient
  30. client.StatusClient
  31. // create a mock with types.NewEventSwitch()
  32. types.EventSwitch
  33. }
  34. func (c Client) _assertIsClient() client.Client {
  35. return c
  36. }
  37. // Call is used by recorders to save a call and response.
  38. // It can also be used to configure mock responses.
  39. //
  40. type Call struct {
  41. Name string
  42. Args interface{}
  43. Response interface{}
  44. Error error
  45. }
  46. // GetResponse will generate the apporiate response for us, when
  47. // using the Call struct to configure a Mock handler.
  48. //
  49. // When configuring a response, if only one of Response or Error is
  50. // set then that will always be returned. If both are set, then
  51. // we return Response if the Args match the set args, Error otherwise.
  52. func (c Call) GetResponse(args interface{}) (interface{}, error) {
  53. // handle the case with no response
  54. if c.Response == nil {
  55. if c.Error == nil {
  56. panic("Misconfigured call, you must set either Response or Error")
  57. }
  58. return nil, c.Error
  59. }
  60. // response without error
  61. if c.Error == nil {
  62. return c.Response, nil
  63. }
  64. // have both, we must check args....
  65. if reflect.DeepEqual(args, c.Args) {
  66. return c.Response, nil
  67. }
  68. return nil, c.Error
  69. }
  70. func (c Client) Status() (*ctypes.ResultStatus, error) {
  71. return core.Status()
  72. }
  73. func (c Client) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
  74. return core.ABCIInfo()
  75. }
  76. func (c Client) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) {
  77. return core.ABCIQuery(path, data, prove)
  78. }
  79. func (c Client) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
  80. return core.BroadcastTxCommit(tx)
  81. }
  82. func (c Client) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  83. return core.BroadcastTxAsync(tx)
  84. }
  85. func (c Client) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  86. return core.BroadcastTxSync(tx)
  87. }
  88. func (c Client) NetInfo() (*ctypes.ResultNetInfo, error) {
  89. return core.NetInfo()
  90. }
  91. func (c Client) DialSeeds(seeds []string) (*ctypes.ResultDialSeeds, error) {
  92. return core.UnsafeDialSeeds(seeds)
  93. }
  94. func (c Client) BlockchainInfo(minHeight, maxHeight int) (*ctypes.ResultBlockchainInfo, error) {
  95. return core.BlockchainInfo(minHeight, maxHeight)
  96. }
  97. func (c Client) Genesis() (*ctypes.ResultGenesis, error) {
  98. return core.Genesis()
  99. }
  100. func (c Client) Block(height *int) (*ctypes.ResultBlock, error) {
  101. return core.Block(height)
  102. }
  103. func (c Client) Commit(height *int) (*ctypes.ResultCommit, error) {
  104. return core.Commit(height)
  105. }
  106. func (c Client) Validators(height *int) (*ctypes.ResultValidators, error) {
  107. return core.Validators(height)
  108. }