diff --git a/ethereum/client.go b/ethereum/client.go index e29e94e7..4493a676 100644 --- a/ethereum/client.go +++ b/ethereum/client.go @@ -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 } diff --git a/ethereum/client_test.go b/ethereum/client_test.go index f252e04b..a9d6a5b8 100644 --- a/ethereum/client_test.go +++ b/ethereum/client_test.go @@ -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) @@ -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)