Skip to content

Commit

Permalink
Merge pull request #274 from forta-network/caner/fix-nil-pointer-pani…
Browse files Browse the repository at this point in the history
…c-in-ethereum-client

Improve Ethereum client testing to include constructor
  • Loading branch information
canercidam authored Nov 3, 2023
2 parents 04557fe + 7f71250 commit e50e6af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
12 changes: 8 additions & 4 deletions ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,17 @@ func NewStreamEthClient(ctx context.Context, apiName, apiURL string) (*streamEth
}
rClient.SetHeader("Content-Type", "application/json")

client := &rpcClient{Client: rClient}
return NewStreamEthClientWithRPCClient(ctx, apiName, isWebsocket(apiURL), &rpcClient{Client: rClient})
}

// NewStreamEthClientWithRPCClient creates a new ethereum client
func NewStreamEthClientWithRPCClient(ctx context.Context, apiName string, isWs bool, rpcClient Subscriber) (*streamEthClient, error) {
return &streamEthClient{
apiName: apiName,
rpcClient: client,
subscriber: client,
rpcClient: rpcClient,
subscriber: rpcClient,
retryInterval: defaultRetryInterval,
isWebsocket: isWebsocket(apiURL),
isWebsocket: isWs,
}, nil
}

Expand Down
18 changes: 11 additions & 7 deletions ethereum/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ const testBlockHash = "0x4fc0862e76691f5312964883954d5c2db35e2b8f7a4f191775a4f50

var testErr = errors.New("test err")

func initClient(t *testing.T) (*streamEthClient, *mocks.MockRPCClient, *mocks.MockSubscriber, context.Context) {
func initClient(t *testing.T) (*streamEthClient, *mocks.MockSubscriber, context.Context) {
r := require.New(t)

minBackoff = 1 * time.Millisecond
maxBackoff = 1 * time.Millisecond
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mocks.NewMockRPCClient(ctrl)
subscriber := mocks.NewMockSubscriber(ctrl)
client := mocks.NewMockSubscriber(ctrl)

ethClient, err := NewStreamEthClientWithRPCClient(context.Background(), "", true, client)
r.NoError(err)

return &streamEthClient{rpcClient: client, subscriber: subscriber}, client, subscriber, ctx
return ethClient, client, ctx
}

func TestEthClient_BlockByHash(t *testing.T) {
r := require.New(t)

ethClient, client, _, ctx := initClient(t)
ethClient, client, ctx := initClient(t)
hash := testBlockHash
// verify retry
client.EXPECT().CallContext(gomock.Any(), gomock.Any(), blocksByHash, testBlockHash).Return(testErr).Times(1)
Expand All @@ -51,10 +55,10 @@ func TestEthClient_BlockByHash(t *testing.T) {
func TestEthClient_SubscribeToHeader_Err(t *testing.T) {
r := require.New(t)

ethClient, _, subscriber, ctx := initClient(t)
ethClient, client, ctx := initClient(t)
sub := mock_domain.NewMockClientSubscription(gomock.NewController(t))

subscriber.EXPECT().Subscribe(gomock.Any(), "eth", gomock.Any(), "newHeads").Return(sub, nil).Times(2)
client.EXPECT().Subscribe(gomock.Any(), "eth", gomock.Any(), "newHeads").Return(sub, nil).Times(2)
errCh := make(chan error, 1)
errCh <- errors.New("subscription encountered some error")
sub.EXPECT().Err().Return(errCh).Times(2)
Expand Down

0 comments on commit e50e6af

Please sign in to comment.