Skip to content

Commit

Permalink
Use mockery for eth.RPCClient mocks
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
  • Loading branch information
awrichar committed Sep 14, 2021
1 parent fb351d5 commit b4e6184
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 309 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ endef

$(eval $(call makemock, internal/contractregistry, ContractStore, contractregistrymocks))
$(eval $(call makemock, internal/contractregistry, RemoteRegistry, contractregistrymocks))
$(eval $(call makemock, internal/eth, RPCClient, ethmocks))
$(eval $(call makemock, $(SARAMA_PATH), Client, saramamocks))
$(eval $(call makemock, $(SARAMA_PATH), ConsumerGroup, saramamocks))
$(eval $(call makemock, $(SARAMA_PATH), ConsumerGroupSession, saramamocks))
Expand Down
200 changes: 105 additions & 95 deletions internal/contractgateway/rest2eth_test.go

Large diffs are not rendered by default.

58 changes: 27 additions & 31 deletions internal/eth/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ func (w *mockEthClient) Subscribe(ctx context.Context, namespace string, channel
}
func (w *mockEthClient) Close() {}

// mockRPCSubscription is used internally within this class for testing the wrapping layer itself
type mockRPCSubscription struct {
Namespace string
Args []interface{}
ErrChan chan error
MsgChan chan<- interface{}
Subscribed bool
}

// Err returns the configured mock error channel
func (ms *mockRPCSubscription) Err() <-chan error {
return ms.ErrChan
}

// Unsubscribe captures the unsubscribe call
func (ms *mockRPCSubscription) Unsubscribe() {
ms.Subscribed = false
}

func TestRPCWrapperConformsToInterface(t *testing.T) {
// This test doesn't verify any interesting function, simply that the rpcWrapper
// type mapper can be invoked without panics
Expand Down Expand Up @@ -96,37 +115,14 @@ func TestRPCConnectFail(t *testing.T) {

func TestSubscribeWrapper(t *testing.T) {
assert := assert.New(t)

mockRPC := NewMockRPCClientForAsync(nil)
var iRPC RPCClientAsync
iRPC = mockRPC
c := make(chan interface{})
sub, err := iRPC.Subscribe(context.Background(), "testns", c, "arg1")
assert.NoError(err)
assert.Equal("testns", mockRPC.SubResult.Namespace)
assert.Equal([]interface{}{"arg1"}, mockRPC.SubResult.Args)
assert.True(mockRPC.SubResult.Subscribed)
assert.NotNil(sub.Err())
sub.Unsubscribe()
assert.False(mockRPC.SubResult.Subscribed)
iRPC.(RPCClosable).Close()
assert.True(mockRPC.Closed)
}

func TestCallContextWrapper(t *testing.T) {
assert := assert.New(t)

mockRPC := NewMockRPCClientForSync(nil, func(method string, res interface{}, args ...interface{}) {
*(res.(*string)) = "mock result"
})
var iRPC RPCClient
iRPC = mockRPC
var strRetval string
err := iRPC.CallContext(context.Background(), &strRetval, "rpcmethod1", "arg1", "arg2")
assert.NoError(err)
assert.Equal("mock result", strRetval)
assert.Equal("rpcmethod1", mockRPC.MethodCapture)
assert.Equal([]interface{}{"arg1", "arg2"}, mockRPC.ArgsCapture)
ms := &mockRPCSubscription{
ErrChan: make(chan error),
Subscribed: true,
}
w := &subWrapper{s: ms}
assert.NotNil(w.Err())
w.Unsubscribe()
assert.Equal(false, ms.Subscribed)
}

func TestCallContextWrapperAuth(t *testing.T) {
Expand Down
95 changes: 0 additions & 95 deletions internal/eth/rpcmock.go

This file was deleted.

Loading

0 comments on commit b4e6184

Please sign in to comment.