From 43d04e22429920f73ddaef3682af5bf989d0eb86 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Wed, 18 Dec 2024 16:20:23 +1100 Subject: [PATCH] OKX: Fix TestGetAssetsFromInstrumentTypeOrID race --- exchanges/okx/okx_test.go | 85 +++++++++------------------------------ 1 file changed, 19 insertions(+), 66 deletions(-) diff --git a/exchanges/okx/okx_test.go b/exchanges/okx/okx_test.go index a5d86fde9f7..14138bbea33 100644 --- a/exchanges/okx/okx_test.go +++ b/exchanges/okx/okx_test.go @@ -3380,80 +3380,33 @@ func TestIsPerpetualFutureCurrency(t *testing.T) { func TestGetAssetsFromInstrumentTypeOrID(t *testing.T) { t.Parallel() - _, err := ok.GetAssetsFromInstrumentTypeOrID("", "") - if !errors.Is(err, errEmptyArgument) { - t.Error(err) - } - assets, err := ok.GetAssetsFromInstrumentTypeOrID("SPOT", "") - if !errors.Is(err, nil) { - t.Error(err) - } - if len(assets) != 1 { - t.Errorf("received %v expected %v", len(assets), 1) - } - if assets[0] != asset.Spot { - t.Errorf("received %v expected %v", assets[0], asset.Spot) - } + ok := new(Okx) //nolint:govet // Intentional shadow + require.NoError(t, testexch.Setup(ok), "Setup must not error") - assets, err = ok.GetAssetsFromInstrumentTypeOrID("", ok.CurrencyPairs.Pairs[asset.Futures].Enabled[0].String()) - if !errors.Is(err, nil) { - t.Error(err) - } - if len(assets) != 1 { - t.Errorf("received %v expected %v", len(assets), 1) - } - if assets[0] != asset.Futures { - t.Errorf("received %v expected %v", assets[0], asset.Futures) - } + _, err := ok.GetAssetsFromInstrumentTypeOrID("", "") + assert.ErrorIs(t, err, errEmptyArgument) - assets, err = ok.GetAssetsFromInstrumentTypeOrID("", ok.CurrencyPairs.Pairs[asset.PerpetualSwap].Enabled[0].String()) - if !errors.Is(err, nil) { - t.Error(err) - } - if len(assets) != 1 { - t.Errorf("received %v expected %v", len(assets), 1) - } - if assets[0] != asset.PerpetualSwap { - t.Errorf("received %v expected %v", assets[0], asset.PerpetualSwap) + for _, a := range []asset.Item{asset.Spot, asset.Futures, asset.PerpetualSwap, asset.Options} { + symbol := "" + if a != asset.Spot { + symbol = ok.CurrencyPairs.Pairs[a].Enabled[0].String() + } + assets, err2 := ok.GetAssetsFromInstrumentTypeOrID(a.String(), symbol) + require.NoErrorf(t, err2, "GetAssetsFromInstrumentTypeOrID must not error for asset: %s", a) + require.Len(t, assets, 1) + assert.Equalf(t, a, assets[0], "Should contain asset: %s", a) } _, err = ok.GetAssetsFromInstrumentTypeOrID("", "test") - if !errors.Is(err, currency.ErrCurrencyNotSupported) { - t.Error(err) - } - + assert.ErrorIs(t, err, currency.ErrCurrencyNotSupported) _, err = ok.GetAssetsFromInstrumentTypeOrID("", "test-test") - if !errors.Is(err, asset.ErrNotSupported) { - t.Error(err) - } - - assets, err = ok.GetAssetsFromInstrumentTypeOrID("", ok.CurrencyPairs.Pairs[asset.Margin].Enabled[0].String()) - if !errors.Is(err, nil) { - t.Error(err) - } - var found bool - for i := range assets { - if assets[i] == asset.Margin { - found = true - } - } - if !found { - t.Errorf("received %v expected %v", assets, asset.Margin) - } + assert.ErrorIs(t, err, asset.ErrNotSupported) - assets, err = ok.GetAssetsFromInstrumentTypeOrID("", ok.CurrencyPairs.Pairs[asset.Spot].Enabled[0].String()) - if !errors.Is(err, nil) { - t.Error(err) - } - found = false - for i := range assets { - if assets[i] == asset.Spot { - found = true - } - } - if !found { - t.Errorf("received %v expected %v", assets, asset.Spot) + for _, a := range []asset.Item{asset.Margin, asset.Spot} { + assets, err2 := ok.GetAssetsFromInstrumentTypeOrID("", ok.CurrencyPairs.Pairs[a].Enabled[0].String()) + require.NoErrorf(t, err2, "GetAssetsFromInstrumentTypeOrID must not error for asset: %s", a) + assert.Contains(t, assets, a) } }