Tests: Abstract UpdatePairsOnce (#1503)

This commit is contained in:
Gareth Kirwan
2024-03-15 06:37:06 +01:00
committed by GitHub
parent 9d1476d4f1
commit 3e4b9becfe
5 changed files with 36 additions and 52 deletions

View File

@@ -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
}