Skip to content

Commit

Permalink
[CORE-715] fix bridge client tests and add test on empty eth rpc endp…
Browse files Browse the repository at this point in the history
…oint (#728) (#729)

* fix bridge client tests and add test on empty eth rpc endpoint

(cherry picked from commit 4d60e8d)

Co-authored-by: Tian <[email protected]>
  • Loading branch information
mergify[bot] and tqin7 authored Oct 31, 2023
1 parent d08c8e0 commit be49763
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions protocol/daemons/bridge/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Start(
"Starting bridge daemon with flags",
"BridgeFlags", flags.Bridge,
)

// Panic if EthRpcEndpoint is empty.
if flags.Bridge.EthRpcEndpoint == "" {
return fmt.Errorf("flag %s is not set", daemonflags.FlagBridgeDaemonEthRpcEndpoint)
Expand Down
30 changes: 27 additions & 3 deletions protocol/daemons/bridge/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
appflags "github.com/dydxprotocol/v4-chain/protocol/app/flags"
"github.com/dydxprotocol/v4-chain/protocol/daemons/bridge/client"
d_constants "github.com/dydxprotocol/v4-chain/protocol/daemons/constants"
"github.com/dydxprotocol/v4-chain/protocol/daemons/flags"
daemonflags "github.com/dydxprotocol/v4-chain/protocol/daemons/flags"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/dydxprotocol/v4-chain/protocol/testutil/appoptions"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
Expand All @@ -21,18 +21,38 @@ import (
"github.com/stretchr/testify/require"
)

func TestStart_EthRpcEndpointNotSet(t *testing.T) {
errorMsg := "flag bridge-daemon-eth-rpc-endpoint is not set"

require.EqualError(
t,
client.Start(
grpc.Ctx,
daemonflags.GetDefaultDaemonFlags(),
appflags.GetFlagValuesFromOptions(appoptions.GetDefaultTestAppOptions("", nil)),
log.NewNopLogger(),
&mocks.GrpcClient{},
),
errorMsg,
)
}

func TestStart_TcpConnectionFails(t *testing.T) {
errorMsg := "Failed to create connection"

// Mock the gRPC client to return an error when creating a TCP connection.
mockGrpcClient := &mocks.GrpcClient{}
mockGrpcClient.On("NewTcpConnection", grpc.Ctx, d_constants.DefaultGrpcEndpoint).Return(nil, errors.New(errorMsg))

// Override default daemon flags with a non-empty EthRpcEndpoint.
daemonFlagsWithEthRpcEndpoint := daemonflags.GetDefaultDaemonFlags()
daemonFlagsWithEthRpcEndpoint.Bridge.EthRpcEndpoint = "http://localhost:8545"

require.EqualError(
t,
client.Start(
grpc.Ctx,
flags.GetDefaultDaemonFlags(),
daemonFlagsWithEthRpcEndpoint,
appflags.GetFlagValuesFromOptions(appoptions.GetDefaultTestAppOptions("", nil)),
log.NewNopLogger(),
mockGrpcClient,
Expand All @@ -56,11 +76,15 @@ func TestStart_UnixSocketConnectionFails(t *testing.T) {
mockGrpcClient.On("NewGrpcConnection", grpc.Ctx, grpc.SocketPath).Return(nil, errors.New(errorMsg))
mockGrpcClient.On("CloseConnection", grpc.GrpcConn).Return(nil)

// Override default daemon flags with a non-empty EthRpcEndpoint.
daemonFlagsWithEthRpcEndpoint := daemonflags.GetDefaultDaemonFlags()
daemonFlagsWithEthRpcEndpoint.Bridge.EthRpcEndpoint = "http://localhost:8545"

require.EqualError(
t,
client.Start(
grpc.Ctx,
flags.GetDefaultDaemonFlags(),
daemonFlagsWithEthRpcEndpoint,
appflags.GetFlagValuesFromOptions(appoptions.GetDefaultTestAppOptions("", nil)),
log.NewNopLogger(),
mockGrpcClient,
Expand Down

0 comments on commit be49763

Please sign in to comment.