github.com/evdatsion/aphelion-dpos-bft@v0.32.1/rpc/client/mock/client.go (about)

     1  package mock
     2  
     3  /*
     4  package mock returns a Client implementation that
     5  accepts various (mock) implementations of the various methods.
     6  
     7  This implementation is useful for using in tests, when you don't
     8  need a real server, but want a high-level of control about
     9  the server response you want to mock (eg. error handling),
    10  or if you just want to record the calls to verify in your tests.
    11  
    12  For real clients, you probably want the "http" package.  If you
    13  want to directly call a tendermint node in process, you can use the
    14  "local" package.
    15  */
    16  
    17  import (
    18  	"reflect"
    19  
    20  	cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common"
    21  	"github.com/evdatsion/aphelion-dpos-bft/rpc/client"
    22  	"github.com/evdatsion/aphelion-dpos-bft/rpc/core"
    23  	ctypes "github.com/evdatsion/aphelion-dpos-bft/rpc/core/types"
    24  	rpctypes "github.com/evdatsion/aphelion-dpos-bft/rpc/lib/types"
    25  	"github.com/evdatsion/aphelion-dpos-bft/types"
    26  )
    27  
    28  // Client wraps arbitrary implementations of the various interfaces.
    29  //
    30  // We provide a few choices to mock out each one in this package.
    31  // Nothing hidden here, so no New function, just construct it from
    32  // some parts, and swap them out them during the tests.
    33  type Client struct {
    34  	client.ABCIClient
    35  	client.SignClient
    36  	client.HistoryClient
    37  	client.StatusClient
    38  	client.EventsClient
    39  	cmn.Service
    40  }
    41  
    42  var _ client.Client = Client{}
    43  
    44  // Call is used by recorders to save a call and response.
    45  // It can also be used to configure mock responses.
    46  //
    47  type Call struct {
    48  	Name     string
    49  	Args     interface{}
    50  	Response interface{}
    51  	Error    error
    52  }
    53  
    54  // GetResponse will generate the apporiate response for us, when
    55  // using the Call struct to configure a Mock handler.
    56  //
    57  // When configuring a response, if only one of Response or Error is
    58  // set then that will always be returned. If both are set, then
    59  // we return Response if the Args match the set args, Error otherwise.
    60  func (c Call) GetResponse(args interface{}) (interface{}, error) {
    61  	// handle the case with no response
    62  	if c.Response == nil {
    63  		if c.Error == nil {
    64  			panic("Misconfigured call, you must set either Response or Error")
    65  		}
    66  		return nil, c.Error
    67  	}
    68  	// response without error
    69  	if c.Error == nil {
    70  		return c.Response, nil
    71  	}
    72  	// have both, we must check args....
    73  	if reflect.DeepEqual(args, c.Args) {
    74  		return c.Response, nil
    75  	}
    76  	return nil, c.Error
    77  }
    78  
    79  func (c Client) Status() (*ctypes.ResultStatus, error) {
    80  	return core.Status(&rpctypes.Context{})
    81  }
    82  
    83  func (c Client) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
    84  	return core.ABCIInfo(&rpctypes.Context{})
    85  }
    86  
    87  func (c Client) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQuery, error) {
    88  	return c.ABCIQueryWithOptions(path, data, client.DefaultABCIQueryOptions)
    89  }
    90  
    91  func (c Client) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
    92  	return core.ABCIQuery(&rpctypes.Context{}, path, data, opts.Height, opts.Prove)
    93  }
    94  
    95  func (c Client) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
    96  	return core.BroadcastTxCommit(&rpctypes.Context{}, tx)
    97  }
    98  
    99  func (c Client) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   100  	return core.BroadcastTxAsync(&rpctypes.Context{}, tx)
   101  }
   102  
   103  func (c Client) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   104  	return core.BroadcastTxSync(&rpctypes.Context{}, tx)
   105  }
   106  
   107  func (c Client) NetInfo() (*ctypes.ResultNetInfo, error) {
   108  	return core.NetInfo(&rpctypes.Context{})
   109  }
   110  
   111  func (c Client) ConsensusState() (*ctypes.ResultConsensusState, error) {
   112  	return core.ConsensusState(&rpctypes.Context{})
   113  }
   114  
   115  func (c Client) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) {
   116  	return core.DumpConsensusState(&rpctypes.Context{})
   117  }
   118  
   119  func (c Client) Health() (*ctypes.ResultHealth, error) {
   120  	return core.Health(&rpctypes.Context{})
   121  }
   122  
   123  func (c Client) DialSeeds(seeds []string) (*ctypes.ResultDialSeeds, error) {
   124  	return core.UnsafeDialSeeds(&rpctypes.Context{}, seeds)
   125  }
   126  
   127  func (c Client) DialPeers(peers []string, persistent bool) (*ctypes.ResultDialPeers, error) {
   128  	return core.UnsafeDialPeers(&rpctypes.Context{}, peers, persistent)
   129  }
   130  
   131  func (c Client) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
   132  	return core.BlockchainInfo(&rpctypes.Context{}, minHeight, maxHeight)
   133  }
   134  
   135  func (c Client) Genesis() (*ctypes.ResultGenesis, error) {
   136  	return core.Genesis(&rpctypes.Context{})
   137  }
   138  
   139  func (c Client) Block(height *int64) (*ctypes.ResultBlock, error) {
   140  	return core.Block(&rpctypes.Context{}, height)
   141  }
   142  
   143  func (c Client) Commit(height *int64) (*ctypes.ResultCommit, error) {
   144  	return core.Commit(&rpctypes.Context{}, height)
   145  }
   146  
   147  func (c Client) Validators(height *int64) (*ctypes.ResultValidators, error) {
   148  	return core.Validators(&rpctypes.Context{}, height)
   149  }