accounts: Move to instance methods, fix races and isolate tests (#1923)

* Bybit: Fix race in TestUpdateAccountInfo and  TestWSHandleData

* DriveBy rename TestWSHandleData
* This doesn't address running with -race=2+ due to the singleton

* Accounts: Add account.GetService()

* exchange: Assertify TestSetupDefaults

* Exchanges: Add account.Service override for testing

* Exchanges: Remove duplicate IsWebsocketEnabled test from TestSetupDefaults

* Dispatch: Replace nil checks with NilGuard

* Engine: Remove deprecated printAccountHoldingsChangeSummary

* Dispatcher: Add EnsureRunning method

* Accounts: Move singleton accounts service to exchange Accounts

* Move singleton accounts service to exchange Accounts

This maintains the concept of a global store, whilst allowing exchanges
to override it when needed, particularly for testing.

APIServer:

* Remove getAllActiveAccounts from apiserver

Deprecated apiserver only thing using this, so remove it instead of
updating it

* Update comment for UpdateAccountBalances everywhere

* Docs: Add punctuation to function comments

* Bybit: Coverage for wsProcessWalletPushData Save
This commit is contained in:
Gareth Kirwan
2025-10-28 09:52:45 +07:00
committed by GitHub
parent bda9bbec66
commit 73e200e4e7
140 changed files with 3515 additions and 4025 deletions

View File

@@ -20,8 +20,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/fundingrate"
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
@@ -66,12 +66,35 @@ func TestUpdateTradablePairs(t *testing.T) {
testexch.UpdatePairsOnce(t, e)
}
func TestGetAccountInfo(t *testing.T) {
func TestCancelAllExchangeOrders(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
_, err := e.CancelAllOrders(t.Context(), nil)
require.ErrorIs(t, err, order.ErrCancelOrderIsNil)
r := &order.Cancel{
OrderID: "1",
AccountID: "1",
}
for _, a := range e.GetAssetTypes(false) {
r.AssetType = a
r.Pair = currency.EMPTYPAIR
_, err = e.CancelAllOrders(t.Context(), r)
assert.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
r.Pair = getPair(t, a)
_, err = e.CancelAllOrders(t.Context(), r)
require.NoError(t, err)
}
}
func TestGetAccountBalances(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
for _, a := range e.GetAssetTypes(false) {
_, err := e.UpdateAccountInfo(t.Context(), a)
assert.NoErrorf(t, err, "UpdateAccountInfo should not error for asset %s", a)
_, err := e.UpdateAccountBalances(t.Context(), a)
assert.NoErrorf(t, err, "UpdateAccountBalances should not error for asset %s", a)
}
}
@@ -2028,7 +2051,7 @@ const wsBalancesPushDataJSON = `{"time": 1605248616, "channel": "spot.balances",
func TestBalancesPushData(t *testing.T) {
t.Parallel()
ctx := account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "test", Secret: "test"})
ctx := accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "test", Secret: "test"})
if err := e.WsHandleSpotData(ctx, nil, []byte(wsBalancesPushDataJSON)); err != nil {
t.Errorf("%s websocket balances push data error: %v", e.Name, err)
}
@@ -2047,7 +2070,7 @@ const wsCrossMarginBalancePushDataJSON = `{"time": 1605248616,"channel": "spot.c
func TestCrossMarginBalancePushData(t *testing.T) {
t.Parallel()
ctx := account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "test", Secret: "test"})
ctx := accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "test", Secret: "test"})
if err := e.WsHandleSpotData(ctx, nil, []byte(wsCrossMarginBalancePushDataJSON)); err != nil {
t.Errorf("%s websocket cross margin balance push data error: %v", e.Name, err)
}
@@ -2069,7 +2092,7 @@ func TestFuturesDataHandler(t *testing.T) {
require.NoError(t, testexch.Setup(e), "Test instance Setup must not error")
testexch.FixtureToDataHandler(t, "testdata/wsFutures.json", func(ctx context.Context, m []byte) error {
if strings.Contains(string(m), "futures.balances") {
ctx = account.DeployCredentialsToContext(ctx, &account.Credentials{Key: "test", Secret: "test"})
ctx = accounts.DeployCredentialsToContext(ctx, &accounts.Credentials{Key: "test", Secret: "test"})
}
return e.WsHandleFuturesData(ctx, nil, m, asset.CoinMarginedFutures)
})
@@ -2236,7 +2259,7 @@ const optionsBalancePushDataJSON = `{ "channel": "options.balances", "event": "u
func TestOptionsBalancePushData(t *testing.T) {
t.Parallel()
ctx := account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "test", Secret: "test"})
ctx := accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "test", Secret: "test"})
if err := e.WsHandleOptionsData(ctx, nil, []byte(optionsBalancePushDataJSON)); err != nil {
t.Errorf("%s websocket options balance push data error: %v", e.Name, err)
}