mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Tests: Abstract UpdatePairsOnce (#1503)
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -27,6 +26,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
||||
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
)
|
||||
|
||||
@@ -152,17 +152,7 @@ func TestGetPairs(t *testing.T) {
|
||||
|
||||
func TestUpdateTradablePairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
updatePairsOnce(t)
|
||||
}
|
||||
|
||||
var updatePairsGuard sync.Once
|
||||
|
||||
func updatePairsOnce(tb testing.TB) {
|
||||
tb.Helper()
|
||||
updatePairsGuard.Do(func() {
|
||||
err := b.UpdateTradablePairs(context.Background(), true)
|
||||
assert.NoError(tb, err, "UpdateTradablePairs should not error")
|
||||
})
|
||||
testexch.UpdatePairsOnce(t, b)
|
||||
}
|
||||
|
||||
func TestUpdateOrderExecutionLimits(t *testing.T) {
|
||||
@@ -557,7 +547,7 @@ func TestUpdateTicker(t *testing.T) {
|
||||
func TestUpdateTickers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
updatePairsOnce(t)
|
||||
testexch.UpdatePairsOnce(t, b)
|
||||
|
||||
assets := b.GetAssetTypes(false)
|
||||
for _, a := range assets {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -23,6 +22,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
||||
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
||||
)
|
||||
|
||||
// Please supply your own keys here to do better tests
|
||||
@@ -60,7 +60,7 @@ func TestMain(m *testing.M) {
|
||||
|
||||
func TestUpdateTradablePairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
updatePairsOnce(t)
|
||||
testexch.UpdatePairsOnce(t, b)
|
||||
expected := map[asset.Item][]string{
|
||||
asset.Spot: {"BTCUSD", "BTCUSDT", "ETHBTC"},
|
||||
asset.Futures: {"BTCPFC", "ETHPFC"},
|
||||
@@ -600,6 +600,7 @@ func seedOrderSizeLimitMap() {
|
||||
|
||||
func TestWithinLimits(t *testing.T) {
|
||||
t.Parallel()
|
||||
testexch.UpdatePairsOnce(t, b)
|
||||
seedOrderSizeLimitMap()
|
||||
p, _ := currency.NewPairDelimiter("XRP-USD", "-")
|
||||
assert.NoError(t, b.withinLimits(p, 1.0), "withinLimits should not error")
|
||||
@@ -714,18 +715,9 @@ func TestIsPerpetualFutureCurrency(t *testing.T) {
|
||||
assert.False(t, isPerp, "IsPerpetualFutureCurrency should return false for a spot pair")
|
||||
}
|
||||
|
||||
var updatePairsGuard sync.Once
|
||||
|
||||
func updatePairsOnce(tb testing.TB) {
|
||||
tb.Helper()
|
||||
updatePairsGuard.Do(func() {
|
||||
err := b.UpdateTradablePairs(context.Background(), true)
|
||||
assert.NoError(tb, err, "UpdateTradablePairs should not error")
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetOpenInterest(t *testing.T) {
|
||||
t.Parallel()
|
||||
testexch.UpdatePairsOnce(t, b)
|
||||
cp1 := currency.NewPair(currency.BTC, currency.PFC)
|
||||
cp2 := currency.NewPair(currency.ETH, currency.PFC)
|
||||
sharedtestvalues.SetupCurrencyPairsForExchangeAsset(t, b, asset.Futures, futuresPair, cp1, cp2)
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
|
||||
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
)
|
||||
|
||||
@@ -64,7 +65,7 @@ func TestMain(m *testing.M) {
|
||||
|
||||
func TestUpdateTradablePairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
updatePairsOnce(t)
|
||||
testexch.UpdatePairsOnce(t, g)
|
||||
}
|
||||
|
||||
func TestCancelAllExchangeOrders(t *testing.T) {
|
||||
@@ -3229,7 +3230,7 @@ func TestParseGateioTimeUnmarshal(t *testing.T) {
|
||||
|
||||
func TestUpdateOrderExecutionLimits(t *testing.T) {
|
||||
t.Parallel()
|
||||
updatePairsOnce(t)
|
||||
testexch.UpdatePairsOnce(t, g)
|
||||
|
||||
err := g.UpdateOrderExecutionLimits(context.Background(), 1336)
|
||||
if !errors.Is(err, asset.ErrNotSupported) {
|
||||
@@ -3442,16 +3443,6 @@ func TestGetOpenInterest(t *testing.T) {
|
||||
assert.NotEmpty(t, resp, "GetOpenInterest should return some items")
|
||||
}
|
||||
|
||||
var updatePairsGuard sync.Once
|
||||
|
||||
func updatePairsOnce(tb testing.TB) {
|
||||
tb.Helper()
|
||||
updatePairsGuard.Do(func() {
|
||||
err := g.UpdateTradablePairs(context.Background(), true)
|
||||
assert.NoError(tb, err, "UpdateTradablePairs should not error")
|
||||
})
|
||||
}
|
||||
|
||||
var pairs = map[asset.Item]currency.Pair{
|
||||
asset.Spot: currency.NewPairWithDelimiter("BTC", "USDT", "_"),
|
||||
}
|
||||
@@ -3473,7 +3464,7 @@ func getPair(tb testing.TB, a asset.Item) currency.Pair {
|
||||
return p
|
||||
}
|
||||
|
||||
updatePairsOnce(tb)
|
||||
testexch.UpdatePairsOnce(tb, g)
|
||||
enabledPairs, err := g.GetEnabledPairs(a)
|
||||
assert.NoErrorf(tb, err, "%s GetEnabledPairs should not error", a)
|
||||
if !assert.NotEmpty(tb, enabledPairs, "%s GetEnabledPairs should not be empty", a) {
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -28,6 +27,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
|
||||
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
)
|
||||
|
||||
@@ -207,7 +207,7 @@ func TestGetBlockTrade(t *testing.T) {
|
||||
assert.WithinRange(t, trade.Timestamp.Time(), time.Now().Add(time.Hour*-24*7), time.Now(), "Timestamp should be within last 7 days")
|
||||
}
|
||||
|
||||
updatePairsOnce(t)
|
||||
testexch.UpdatePairsOnce(t, ok)
|
||||
|
||||
pairs, err := ok.GetAvailablePairs(asset.Options)
|
||||
assert.NoError(t, err, "GetAvailablePairs should not error")
|
||||
@@ -1920,17 +1920,7 @@ func TestFetchTradablePairs(t *testing.T) {
|
||||
|
||||
func TestUpdateTradablePairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
updatePairsOnce(t)
|
||||
}
|
||||
|
||||
var updatePairsGuard sync.Once
|
||||
|
||||
func updatePairsOnce(tb testing.TB) {
|
||||
tb.Helper()
|
||||
updatePairsGuard.Do(func() {
|
||||
err := ok.UpdateTradablePairs(context.Background(), true)
|
||||
assert.NoError(tb, err, "UpdateTradablePairs should not error")
|
||||
})
|
||||
testexch.UpdatePairsOnce(t, ok)
|
||||
}
|
||||
|
||||
func TestUpdateOrderExecutionLimits(t *testing.T) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -141,7 +142,7 @@ func SetupWs(tb testing.TB, e exchange.IBotExchange) {
|
||||
setupWsMutex.Lock()
|
||||
defer setupWsMutex.Unlock()
|
||||
|
||||
if _, ok := setupWsOnce[e]; ok {
|
||||
if setupWsOnce[e] {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -157,3 +158,23 @@ func SetupWs(tb testing.TB, e exchange.IBotExchange) {
|
||||
|
||||
setupWsOnce[e] = true
|
||||
}
|
||||
|
||||
var updatePairsMutex sync.Mutex
|
||||
var updatePairsOnce = make(map[exchange.IBotExchange]bool)
|
||||
|
||||
// UpdatePairsOnce ensures pairs are only updated once in parallel tests
|
||||
func UpdatePairsOnce(tb testing.TB, e exchange.IBotExchange) {
|
||||
tb.Helper()
|
||||
|
||||
updatePairsMutex.Lock()
|
||||
defer updatePairsMutex.Unlock()
|
||||
|
||||
if updatePairsOnce[e] {
|
||||
return
|
||||
}
|
||||
|
||||
err := e.UpdateTradablePairs(context.Background(), true)
|
||||
require.NoError(tb, err, "UpdateTradablePairs must not error")
|
||||
|
||||
updatePairsOnce[e] = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user