-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8e5e90
commit e0a8c37
Showing
8 changed files
with
579 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package client_test | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"cosmossdk.io/log" | ||
"github.com/skip-mev/slinky/service/servers/oracle/types" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/suite" | ||
"google.golang.org/grpc" | ||
|
||
appflags "github.com/dydxprotocol/v4-chain/protocol/app/flags" | ||
daemonflags "github.com/dydxprotocol/v4-chain/protocol/daemons/flags" | ||
daemonserver "github.com/dydxprotocol/v4-chain/protocol/daemons/server" | ||
pricefeed_types "github.com/dydxprotocol/v4-chain/protocol/daemons/server/types/pricefeed" | ||
"github.com/dydxprotocol/v4-chain/protocol/daemons/slinky/client" | ||
daemontypes "github.com/dydxprotocol/v4-chain/protocol/daemons/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/mocks" | ||
"github.com/dydxprotocol/v4-chain/protocol/testutil/appoptions" | ||
pricetypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" | ||
) | ||
|
||
func TestClientTestSuite(t *testing.T) { | ||
suite.Run(t, &ClientTestSuite{}) | ||
} | ||
|
||
type ClientTestSuite struct { | ||
suite.Suite | ||
daemonFlags daemonflags.DaemonFlags | ||
appFlags appflags.Flags | ||
daemonServer *daemonserver.Server | ||
exchangePriceCache *pricefeed_types.MarketToExchangePrices | ||
grpcServer *grpc.Server | ||
pricesMockQueryServer *mocks.QueryServer | ||
wg sync.WaitGroup | ||
} | ||
|
||
func (c *ClientTestSuite) SetupTest() { | ||
// Setup daemon and grpc servers. | ||
c.daemonFlags = daemonflags.GetDefaultDaemonFlags() | ||
c.appFlags = appflags.GetFlagValuesFromOptions(appoptions.GetDefaultTestAppOptions("", nil)) | ||
c.grpcServer = grpc.NewServer() | ||
|
||
// Configure and run daemon server. | ||
c.daemonServer = daemonserver.NewServer( | ||
log.NewNopLogger(), | ||
c.grpcServer, | ||
&daemontypes.FileHandlerImpl{}, | ||
c.daemonFlags.Shared.SocketAddress, | ||
) | ||
|
||
c.pricesMockQueryServer = &mocks.QueryServer{} | ||
pricetypes.RegisterQueryServer(c.grpcServer, c.pricesMockQueryServer) | ||
c.daemonServer.WithPriceFeedMarketToExchangePrices( | ||
pricefeed_types.NewMarketToExchangePrices(5 * time.Second), | ||
) | ||
|
||
c.wg.Add(1) | ||
go func() { | ||
defer c.wg.Done() | ||
c.daemonServer.Start() | ||
}() | ||
|
||
c.wg.Add(1) | ||
go func() { | ||
defer c.wg.Done() | ||
ls, err := net.Listen("tcp", c.appFlags.GrpcAddress) | ||
c.Require().NoError(err) | ||
_ = c.grpcServer.Serve(ls) | ||
}() | ||
} | ||
|
||
func (c *ClientTestSuite) TearDownTest() { | ||
c.daemonServer.Stop() | ||
c.grpcServer.Stop() | ||
c.wg.Wait() | ||
} | ||
|
||
func (c *ClientTestSuite) TestClient() { | ||
var cli *client.Client | ||
slinky := mocks.NewOracleClient(c.T()) | ||
logger := log.NewTestLogger(c.T()) | ||
|
||
c.pricesMockQueryServer.On("AllMarketParams", mock.Anything, mock.Anything). | ||
Return( | ||
&pricetypes.QueryAllMarketParamsResponse{ | ||
MarketParams: []pricetypes.MarketParam{ | ||
{Id: 0, Pair: "FOO-BAR"}, | ||
{Id: 1, Pair: "BAR-FOO"}, | ||
}}, | ||
nil, | ||
) | ||
|
||
c.Run("services are all started and call their deps", func() { | ||
slinky.On("Stop").Return(nil) | ||
slinky.On("Start", mock.Anything).Return(nil).Once() | ||
slinky.On("Prices", mock.Anything, mock.Anything). | ||
Return(&types.QueryPricesResponse{ | ||
Prices: map[string]string{ | ||
"FOO/BAR": "100000000000", | ||
}, | ||
Timestamp: time.Now(), | ||
}, nil) | ||
client.SlinkyPriceFetchDelay = time.Millisecond | ||
client.SlinkyMarketParamFetchDelay = time.Millisecond | ||
cli = client.StartNewClient( | ||
context.Background(), | ||
slinky, | ||
&daemontypes.GrpcClientImpl{}, | ||
c.daemonFlags, | ||
c.appFlags, | ||
logger, | ||
) | ||
// Need to wait until a single cycle is done | ||
time.Sleep(time.Millisecond * 20) | ||
cli.Stop() | ||
c.Require().NoError(cli.HealthCheck()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
protocol/daemons/slinky/client/market_pair_fetcher_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package client_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"cosmossdk.io/log" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
slinkytypes "github.com/skip-mev/slinky/x/oracle/types" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/daemons/slinky/client" | ||
"github.com/dydxprotocol/v4-chain/protocol/mocks" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/prices/types" | ||
) | ||
|
||
func TestMarketPairFetcher(t *testing.T) { | ||
logger := log.NewTestLogger(t) | ||
queryClient := mocks.NewQueryClient(t) | ||
fetcher := client.MarketPairFetcherImpl{ | ||
Logger: logger, | ||
PricesQueryClient: queryClient, | ||
} | ||
asset0 := "FOO" | ||
asset1 := "BAR" | ||
pair0 := types.MarketParam{Id: 0, Pair: fmt.Sprintf("%s-%s", asset0, asset1)} | ||
pair1 := types.MarketParam{Id: 1, Pair: fmt.Sprintf("%s-%s", asset1, asset0)} | ||
invalidPair := types.MarketParam{Id: 2, Pair: "foobar"} | ||
|
||
t.Run("caches and returns valid pairs", func(t *testing.T) { | ||
queryClient. | ||
On("AllMarketParams", mock.Anything, mock.Anything). | ||
Return( | ||
&types.QueryAllMarketParamsResponse{ | ||
MarketParams: []types.MarketParam{ | ||
pair0, | ||
pair1, | ||
}}, | ||
nil, | ||
).Once() | ||
err := fetcher.FetchIdMappings(context.Background()) | ||
require.NoError(t, err) | ||
id, err := fetcher.GetIDForPair(slinkytypes.CurrencyPair{Base: asset0, Quote: asset1}) | ||
require.NoError(t, err) | ||
require.Equal(t, pair0.Id, id) | ||
id, err = fetcher.GetIDForPair(slinkytypes.CurrencyPair{Base: asset1, Quote: asset0}) | ||
require.NoError(t, err) | ||
require.Equal(t, pair1.Id, id) | ||
}) | ||
|
||
t.Run("errors on fetch non-cached pair", func(t *testing.T) { | ||
queryClient. | ||
On("AllMarketParams", mock.Anything, mock.Anything). | ||
Return( | ||
&types.QueryAllMarketParamsResponse{ | ||
MarketParams: []types.MarketParam{}}, | ||
nil, | ||
).Once() | ||
err := fetcher.FetchIdMappings(context.Background()) | ||
require.NoError(t, err) | ||
_, err = fetcher.GetIDForPair(slinkytypes.CurrencyPair{Base: asset0, Quote: asset1}) | ||
require.Error(t, err, fmt.Errorf("pair %s/%s not found in compatMappings", asset0, asset1)) | ||
}) | ||
|
||
t.Run("fails on fetching invalid pairs", func(t *testing.T) { | ||
queryClient. | ||
On("AllMarketParams", mock.Anything, mock.Anything). | ||
Return( | ||
&types.QueryAllMarketParamsResponse{ | ||
MarketParams: []types.MarketParam{ | ||
invalidPair, | ||
}}, | ||
nil, | ||
).Once() | ||
err := fetcher.FetchIdMappings(context.Background()) | ||
require.Error(t, err, "incorrectly formatted CurrencyPair: foobar") | ||
}) | ||
|
||
t.Run("fails on prices query error", func(t *testing.T) { | ||
queryClient. | ||
On("AllMarketParams", mock.Anything, mock.Anything). | ||
Return( | ||
&types.QueryAllMarketParamsResponse{}, | ||
fmt.Errorf("test error"), | ||
).Once() | ||
err := fetcher.FetchIdMappings(context.Background()) | ||
require.Error(t, err, "test error") | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.