GateIO: Update websocket orderbook manager (#1989)

* gateio: websocket ob manager fix (cherry-pick me)

* fix(gateio): update websocket orderbook manager to support delay and deadline parameters
    feat(subscriptions): mv ChannelKey type for subscription management from gateio to subscriptions
    test(gateio): enhance tests for orderbook update manager and subscription keys

* ai: nits

* linter: fix

* Fix asset typo and add in case test

* cranktakular: nits

* cranktakular: nits after merge master

* bump time delay for cache

* fix bug where on error it never initiates another orderbook fetch

* lint: fix

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* linter: fix

* Update exchanges/gateio/gateio_wrapper_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/gateio_wrapper.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/gateio_wrapper_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits

* Update exchanges/gateio/gateio_wrapper.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits

* bossking: nits

* Update exchanges/gateio/ws_ob_update_manager_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits

* apply patch

* rm error state as this was on the same thread as cacheStateQueuing and had the potential to drop messages, add tests.

* linter: fix

* mock live request

* misc: fix

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/ws_ob_update_manager.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits

* field name from mtx -> m

* lint: fix

* race: check fix

* thrasher-: patch adams

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
This commit is contained in:
Ryan O'Hara-Reid
2025-11-27 12:21:17 +11:00
committed by GitHub
parent 719e6bebfe
commit cf54764cb7
11 changed files with 757 additions and 251 deletions

View File

@@ -1,6 +1,7 @@
package gateio
import (
"fmt"
"testing"
"github.com/gofrs/uuid"
@@ -10,6 +11,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
)
func TestCancelAllOrders(t *testing.T) {
@@ -89,3 +91,63 @@ func BenchmarkMessageID(b *testing.B) {
_ = e.MessageID()
}
}
func TestFetchOrderbook(t *testing.T) {
t.Parallel()
testexch.UpdatePairsOnce(t, e)
availSpot, err := e.GetAvailablePairs(asset.Spot)
require.NoError(t, err, "GetAvailablePairs must not error")
availMargin, err := e.GetAvailablePairs(asset.Margin)
require.NoError(t, err, "GetAvailablePairs must not error")
marginPairNotInSpot, err := availMargin.Remove(availSpot...).GetRandomPair()
require.NoError(t, err, "GetRandomPair must not error")
availOptions, err := e.GetAvailablePairs(asset.Options)
require.NoError(t, err, "GetAvailablePairs must not error")
optionsPair, err := availOptions.GetRandomPair()
require.NoError(t, err, "GetRandomPair must not error")
availDelivery, err := e.GetAvailablePairs(asset.DeliveryFutures)
require.NoError(t, err, "GetAvailablePairs must not error")
deliveryPair, err := availDelivery.GetRandomPair()
require.NoError(t, err, "GetRandomPair must not error")
for _, tc := range []struct {
pair currency.Pair
a asset.Item
err error
}{
{pair: currency.EMPTYPAIR, a: asset.Spot, err: currency.ErrCurrencyPairEmpty},
{pair: marginPairNotInSpot, a: asset.Margin, err: errNoSpotInstrument},
{pair: marginPairNotInSpot, a: asset.Binary, err: asset.ErrNotSupported},
{pair: currency.NewBTCUSDT(), a: asset.Spot},
{pair: currency.NewBTCUSDT(), a: asset.USDTMarginedFutures},
{pair: deliveryPair, a: asset.DeliveryFutures},
{pair: optionsPair, a: asset.Options},
} {
t.Run(fmt.Sprintf("%s-%s: expected err:%v", tc.pair, tc.a, tc.err), func(t *testing.T) {
t.Parallel()
got, err := e.fetchOrderbook(t.Context(), tc.pair, tc.a, 1)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
return
}
require.NoError(t, err)
assert.Equal(t, e.Name, got.Exchange, "Exchange name should be correct")
assert.True(t, tc.pair.Equal(got.Pair), "Pair should be correct")
assert.Equal(t, tc.a, got.Asset, "Asset should be correct")
assert.LessOrEqual(t, len(got.Asks), 1, "Asks count should not exceed limit, but may be empty especially for options")
assert.LessOrEqual(t, len(got.Bids), 1, "Bids count should not exceed limit, but may be empty especially for options")
assert.NotZero(t, got.LastUpdated, "Last updated timestamp should be set")
assert.NotZero(t, got.LastUpdateID, "Last update ID should be set")
assert.NotZero(t, got.LastPushed, "Last pushed timestamp should be set")
assert.LessOrEqual(t, got.LastUpdated, got.LastPushed, "Last updated timestamp should be before last pushed timestamp")
})
}
}