exchanges: Initial context propagation (#744)

* gct: phase one context awareness pass

* exchanges: context propagation pass

* common/requester: force context requirement

* gctcli/exchanges: linter fix

* rpcserver: fix test using dummy rpc server

* backtester: fix comments

* grpc: add correct cancel and timeout for commands

* rpcserver_test: add comment on dummy server

* common: deprecated SendHTTPGetRequest

* linter: fix

* linter: turn on no context check

* apichecker: fix context linter issue

* binance: use param context

* common: remove checks as this gets executed before main

* common: change mutex to RW as clients can be used by multiple go routines.

* common: remove init and JIT default client. Unexport global variables and add protection.

* common: Add comments

* bithumb: after dinner mints fix
This commit is contained in:
Ryan O'Hara-Reid
2021-09-11 13:52:07 +10:00
committed by GitHub
parent 72516f7268
commit d636049fb2
168 changed files with 8085 additions and 6996 deletions

View File

@@ -1,6 +1,7 @@
package validator
import (
"context"
"math/rand"
"time"
@@ -43,7 +44,7 @@ func (w Wrapper) IsEnabled(exch string) (v bool) {
}
// Orderbook validator for test execution/scripts
func (w Wrapper) Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
func (w Wrapper) Orderbook(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
if exch == exchError.String() {
return nil, errTestFailed
}
@@ -68,7 +69,7 @@ func (w Wrapper) Orderbook(exch string, pair currency.Pair, item asset.Item) (*o
}
// Ticker validator for test execution/scripts
func (w Wrapper) Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
func (w Wrapper) Ticker(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
if exch == exchError.String() {
return nil, errTestFailed
}
@@ -106,7 +107,7 @@ func (w Wrapper) Pairs(exch string, _ bool, _ asset.Item) (*currency.Pairs, erro
}
// QueryOrder validator for test execution/scripts
func (w Wrapper) QueryOrder(exch, _ string, _ currency.Pair, _ asset.Item) (*order.Detail, error) {
func (w Wrapper) QueryOrder(ctx context.Context, exch, _ string, _ currency.Pair, _ asset.Item) (*order.Detail, error) {
if exch == exchError.String() {
return nil, errTestFailed
}
@@ -146,7 +147,7 @@ func (w Wrapper) QueryOrder(exch, _ string, _ currency.Pair, _ asset.Item) (*ord
}
// SubmitOrder validator for test execution/scripts
func (w Wrapper) SubmitOrder(o *order.Submit) (*order.SubmitResponse, error) {
func (w Wrapper) SubmitOrder(ctx context.Context, o *order.Submit) (*order.SubmitResponse, error) {
if o == nil {
return nil, errTestFailed
}
@@ -167,7 +168,7 @@ func (w Wrapper) SubmitOrder(o *order.Submit) (*order.SubmitResponse, error) {
}
// CancelOrder validator for test execution/scripts
func (w Wrapper) CancelOrder(exch, orderid string, cp currency.Pair, a asset.Item) (bool, error) {
func (w Wrapper) CancelOrder(ctx context.Context, exch, orderid string, cp currency.Pair, a asset.Item) (bool, error) {
if exch == exchError.String() {
return false, errTestFailed
}
@@ -184,7 +185,7 @@ func (w Wrapper) CancelOrder(exch, orderid string, cp currency.Pair, a asset.Ite
}
// AccountInformation validator for test execution/scripts
func (w Wrapper) AccountInformation(exch string, assetType asset.Item) (account.Holdings, error) {
func (w Wrapper) AccountInformation(ctx context.Context, exch string, assetType asset.Item) (account.Holdings, error) {
if exch == exchError.String() {
return account.Holdings{}, errTestFailed
}
@@ -224,7 +225,7 @@ func (w Wrapper) DepositAddress(exch string, _ currency.Code) (string, error) {
}
// WithdrawalCryptoFunds validator for test execution/scripts
func (w Wrapper) WithdrawalCryptoFunds(r *withdraw.Request) (out string, err error) {
func (w Wrapper) WithdrawalCryptoFunds(ctx context.Context, r *withdraw.Request) (out string, err error) {
if r.Exchange == exchError.String() {
return r.Exchange, errTestFailed
}
@@ -233,7 +234,7 @@ func (w Wrapper) WithdrawalCryptoFunds(r *withdraw.Request) (out string, err err
}
// WithdrawalFiatFunds validator for test execution/scripts
func (w Wrapper) WithdrawalFiatFunds(_ string, r *withdraw.Request) (out string, err error) {
func (w Wrapper) WithdrawalFiatFunds(ctx context.Context, _ string, r *withdraw.Request) (out string, err error) {
if r.Exchange == exchError.String() {
return r.Exchange, errTestFailed
}
@@ -242,7 +243,7 @@ func (w Wrapper) WithdrawalFiatFunds(_ string, r *withdraw.Request) (out string,
}
// OHLCV returns open high low close volume candles for requested exchange/pair/asset/start & end time
func (w Wrapper) OHLCV(exch string, p currency.Pair, a asset.Item, start, end time.Time, i kline.Interval) (kline.Item, error) {
func (w Wrapper) OHLCV(ctx context.Context, exch string, p currency.Pair, a asset.Item, start, end time.Time, i kline.Interval) (kline.Item, error) {
if exch == exchError.String() {
return kline.Item{}, errTestFailed
}

View File

@@ -1,6 +1,7 @@
package validator
import (
"context"
"testing"
"time"
@@ -63,12 +64,14 @@ func TestWrapper_IsEnabled(t *testing.T) {
func TestWrapper_AccountInformation(t *testing.T) {
t.Parallel()
_, err := testWrapper.AccountInformation(exchName, asset.Spot)
_, err := testWrapper.AccountInformation(context.Background(),
exchName, asset.Spot)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.AccountInformation(exchError.String(), asset.Spot)
_, err = testWrapper.AccountInformation(context.Background(),
exchError.String(), asset.Spot)
if err == nil {
t.Fatal("expected AccountInformation to return error on invalid name")
}
@@ -77,27 +80,32 @@ func TestWrapper_AccountInformation(t *testing.T) {
func TestWrapper_CancelOrder(t *testing.T) {
t.Parallel()
cp := currency.NewPair(currency.BTC, currency.USD)
_, err := testWrapper.CancelOrder(exchName, orderID, cp, assetType)
_, err := testWrapper.CancelOrder(context.Background(),
exchName, orderID, cp, assetType)
if err != nil {
t.Error(err)
}
_, err = testWrapper.CancelOrder(exchError.String(), orderID, cp, assetType)
_, err = testWrapper.CancelOrder(context.Background(),
exchError.String(), orderID, cp, assetType)
if err == nil {
t.Error("expected CancelOrder to return error on invalid name")
}
_, err = testWrapper.CancelOrder(exchName, "", cp, assetType)
_, err = testWrapper.CancelOrder(context.Background(),
exchName, "", cp, assetType)
if err == nil {
t.Error("expected CancelOrder to return error on invalid name")
}
_, err = testWrapper.CancelOrder(exchName, orderID, currency.Pair{}, assetType)
_, err = testWrapper.CancelOrder(context.Background(),
exchName, orderID, currency.Pair{}, assetType)
if err != nil {
t.Error(err)
}
_, err = testWrapper.CancelOrder(exchName, orderID, cp, "")
_, err = testWrapper.CancelOrder(context.Background(),
exchName, orderID, cp, "")
if err != nil {
t.Error(err)
}
@@ -121,12 +129,14 @@ func TestWrapper_Orderbook(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Orderbook(exchName, c, assetType)
_, err = testWrapper.Orderbook(context.Background(),
exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Orderbook(exchError.String(), currencyPair, asset.Spot)
_, err = testWrapper.Orderbook(context.Background(),
exchError.String(), currencyPair, asset.Spot)
if err == nil {
t.Fatal("expected Orderbook to return error with invalid name")
}
@@ -152,12 +162,14 @@ func TestWrapper_Pairs(t *testing.T) {
func TestWrapper_QueryOrder(t *testing.T) {
t.Parallel()
_, err := testWrapper.QueryOrder(exchName, orderID, currency.Pair{}, assetType)
_, err := testWrapper.QueryOrder(context.Background(),
exchName, orderID, currency.Pair{}, assetType)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.QueryOrder(exchError.String(), "", currency.Pair{}, assetType)
_, err = testWrapper.QueryOrder(context.Background(),
exchError.String(), "", currency.Pair{}, assetType)
if err == nil {
t.Fatal("expected QueryOrder to return error on invalid name")
}
@@ -181,12 +193,12 @@ func TestWrapper_SubmitOrder(t *testing.T) {
Exchange: "true",
AssetType: asset.Spot,
}
_, err = testWrapper.SubmitOrder(tempOrder)
_, err = testWrapper.SubmitOrder(context.Background(), tempOrder)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.SubmitOrder(nil)
_, err = testWrapper.SubmitOrder(context.Background(), nil)
if err == nil {
t.Fatal("expected SubmitOrder to return error with invalid name")
}
@@ -198,36 +210,40 @@ func TestWrapper_Ticker(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Ticker(exchName, c, assetType)
_, err = testWrapper.Ticker(context.Background(), exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Ticker(exchError.String(), currencyPair, asset.Spot)
_, err = testWrapper.Ticker(context.Background(), exchError.String(), currencyPair, asset.Spot)
if err == nil {
t.Fatal("expected Ticker to return error with invalid name")
}
}
func TestWrapper_WithdrawalCryptoFunds(t *testing.T) {
_, err := testWrapper.WithdrawalCryptoFunds(&withdraw.Request{Exchange: exchError.String()})
_, err := testWrapper.WithdrawalCryptoFunds(context.Background(),
&withdraw.Request{Exchange: exchError.String()})
if err == nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
_, err = testWrapper.WithdrawalCryptoFunds(&withdraw.Request{Exchange: exchName})
_, err = testWrapper.WithdrawalCryptoFunds(context.Background(),
&withdraw.Request{Exchange: exchName})
if err != nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
}
func TestWrapper_WithdrawalFiatFunds(t *testing.T) {
_, err := testWrapper.WithdrawalFiatFunds("", &withdraw.Request{Exchange: exchError.String()})
_, err := testWrapper.WithdrawalFiatFunds(context.Background(),
"", &withdraw.Request{Exchange: exchError.String()})
if err == nil {
t.Fatal("expected WithdrawalFiatFunds to return error with invalid name")
}
_, err = testWrapper.WithdrawalFiatFunds("", &withdraw.Request{Exchange: exchName})
_, err = testWrapper.WithdrawalFiatFunds(context.Background(),
"", &withdraw.Request{Exchange: exchName})
if err != nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
@@ -238,11 +254,15 @@ func TestWrapper_OHLCV(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.OHLCV("test", c, asset.Spot, time.Now().Add(-24*time.Hour), time.Now(), kline.OneDay)
_, err = testWrapper.OHLCV(context.Background(),
"test", c, asset.Spot, time.Now().Add(-24*time.Hour), time.Now(), kline.OneDay)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.OHLCV(exchError.String(), c, asset.Spot, time.Now().Add(-24*time.Hour), time.Now(), kline.OneDay)
_, err = testWrapper.OHLCV(context.Background(),
exchError.String(), c, asset.Spot,
time.Now().Add(-24*time.Hour),
time.Now(), kline.OneDay)
if err == nil {
t.Fatal("expected OHLCV to return error with invalid name")
}