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.

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