Skip to content

Commit

Permalink
Huobi: Fix tests racing on updatePairsOnce
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Dec 14, 2024
1 parent ac7b14a commit de2044a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
39 changes: 24 additions & 15 deletions exchanges/huobi/huobi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestFCancelOrder(t *testing.T) {
func TestFCancelAllOrders(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, h, canManipulateRealOrders)
updatePairsOnce(t)
updatePairsOnce(t, h)
_, err := h.FCancelAllOrders(context.Background(), btcFutureDatedPair, "", "")
require.NoError(t, err)
}
Expand Down Expand Up @@ -483,7 +483,7 @@ func TestUpdateOrderbookFuture(t *testing.T) {
func TestGetOrderHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, h)
updatePairsOnce(t)
updatePairsOnce(t, h)
getOrdersRequest := order.MultiOrderRequest{
Type: order.AnyType,
Pairs: []currency.Pair{currency.NewPair(currency.BTC, currency.USDT)},
Expand Down Expand Up @@ -861,7 +861,9 @@ func TestGetSpotKline(t *testing.T) {
func TestGetHistoricCandles(t *testing.T) {
t.Parallel()

updatePairsOnce(t)
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(h), "Setup Instance must not error")
updatePairsOnce(t, h)

endTime := time.Now().Add(-time.Hour).Truncate(time.Hour)
_, err := h.GetHistoricCandles(context.Background(), btcusdtPair, asset.Spot, kline.OneMin, endTime.Add(-time.Hour), endTime)
Expand All @@ -880,7 +882,9 @@ func TestGetHistoricCandles(t *testing.T) {
func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()

updatePairsOnce(t)
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(h), "Setup Instance must not error")
updatePairsOnce(t, h)

endTime := time.Now().Add(-time.Hour).Truncate(time.Hour)
_, err := h.GetHistoricCandlesExtended(context.Background(), btcusdtPair, asset.Spot, kline.OneMin, endTime.Add(-time.Hour), endTime)
Expand Down Expand Up @@ -1615,7 +1619,8 @@ func TestGetAvailableTransferChains(t *testing.T) {
}

func TestFormatFuturesPair(t *testing.T) {
updatePairsOnce(t)
t.Parallel()
updatePairsOnce(t, h)

r, err := h.formatFuturesPair(btccwPair, false)
require.NoError(t, err)
Expand Down Expand Up @@ -1750,7 +1755,7 @@ func TestGetBatchFuturesContracts(t *testing.T) {

func TestUpdateTickers(t *testing.T) {
t.Parallel()
updatePairsOnce(t)
updatePairsOnce(t, h)
for _, a := range h.GetAssetTypes(false) {
err := h.UpdateTickers(context.Background(), a)
require.NoErrorf(t, err, "asset %s", a)
Expand Down Expand Up @@ -1803,7 +1808,7 @@ func TestPairFromContractExpiryCode(t *testing.T) {

func TestGetOpenInterest(t *testing.T) {
t.Parallel()
updatePairsOnce(t)
updatePairsOnce(t, h)

_, err := h.GetOpenInterest(context.Background(), key.PairAsset{
Base: currency.ETH.Item,
Expand Down Expand Up @@ -1859,7 +1864,7 @@ func TestContractOpenInterestUSDT(t *testing.T) {

func TestGetCurrencyTradeURL(t *testing.T) {
t.Parallel()
updatePairsOnce(t)
updatePairsOnce(t, h)
for _, a := range h.GetAssetTypes(false) {
pairs, err := h.CurrencyPairs.GetPairs(a, false)
require.NoError(t, err, "cannot get pairs for %s", a)
Expand Down Expand Up @@ -1962,7 +1967,7 @@ func TestSubscribe(t *testing.T) {
func TestAuthSubscribe(t *testing.T) {
t.Parallel()
subCfg := h.Features.Subscriptions
h := testexch.MockWsInstance[HUOBI](t, mockws.CurryWsMockUpgrader(t, wsFixture)) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
h := testexch.MockWsInstance[HUOBI](t, mockws.CurryWsMockUpgrader(t, wsFixture))
h.Websocket.SetCanUseAuthenticatedEndpoints(true)
subs, err := subCfg.ExpandTemplates(h)
require.NoError(t, err, "ExpandTemplates must not error")
Expand Down Expand Up @@ -2002,7 +2007,7 @@ func TestGetErrResp(t *testing.T) {

func TestBootstrap(t *testing.T) {
t.Parallel()
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
h := new(HUOBI)
require.NoError(t, testexch.Setup(h), "Test Instance Setup must not fail")

c, err := h.Bootstrap(context.Background())
Expand All @@ -2018,17 +2023,21 @@ func TestBootstrap(t *testing.T) {

var updatePairsMutex sync.Mutex

func updatePairsOnce(tb testing.TB) {
// updatePairsOnce updates the pairs once, and ensures a future dated contract is enabled
func updatePairsOnce(tb testing.TB, h *HUOBI) {
tb.Helper()

updatePairsMutex.Lock()
defer updatePairsMutex.Unlock()

testexch.UpdatePairsOnce(tb, h)

p, err := h.pairFromContractExpiryCode(btccwPair)
require.NoError(tb, err, "pairFromContractCode must not error")
err = h.CurrencyPairs.EnablePair(asset.Futures, p)
if btcFutureDatedPair == currency.EMPTYPAIR {
p, err := h.pairFromContractExpiryCode(btccwPair)
require.NoError(tb, err, "pairFromContractCode must not error")
btcFutureDatedPair = p
}

err := h.CurrencyPairs.EnablePair(asset.Futures, btcFutureDatedPair) // Must enable every time we refresh the CurrencyPairs from cache
require.NoError(tb, common.ExcludeError(err, currency.ErrPairAlreadyEnabled))
btcFutureDatedPair = p
}
1 change: 1 addition & 0 deletions internal/testing/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ var updatePairsOnce = make(map[string]*currency.PairsManager)

// UpdatePairsOnce ensures pairs are only updated once in parallel tests
// A clone of the cache of the updated pairs is used to populate duplicate requests
// Any pairs enabled after this is called will be lost on the next call
func UpdatePairsOnce(tb testing.TB, e exchange.IBotExchange) {
tb.Helper()

Expand Down

0 comments on commit de2044a

Please sign in to comment.