From 6e00563009f29375def893cd39cf3bc10679d569 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Tue, 23 Jul 2024 17:29:12 +0700 Subject: [PATCH] Subscriptions: Add IgnoringAssetsKey --- exchanges/subscription/keys.go | 35 +++++++++++++++++++++++++++++ exchanges/subscription/keys_test.go | 33 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/exchanges/subscription/keys.go b/exchanges/subscription/keys.go index 9cc31f964c1..ea29431bdc9 100644 --- a/exchanges/subscription/keys.go +++ b/exchanges/subscription/keys.go @@ -87,3 +87,38 @@ func (k IgnoringPairsKey) Match(eachKey MatchableKey) bool { eachSub.Levels == k.Levels && eachSub.Interval == k.Interval } + +// IgnoringAssetKey is a key type for finding subscriptions to group together for requests +type IgnoringAssetKey struct { + *Subscription +} + +var _ MatchableKey = IgnoringAssetKey{} // Enforce IgnoringAssetKey must implement MatchableKey + +// GetSubscription returns the underlying subscription +func (k IgnoringAssetKey) GetSubscription() *Subscription { + return k.Subscription +} + +// String implements Stringer; returns the asset and Channel name but no pairs +func (k IgnoringAssetKey) String() string { + s := k.Subscription + if s == nil { + return "Uninitialised IgnoringAssetKey" + } + return fmt.Sprintf("%s %s", s.Channel, s.Pairs) +} + +// Match implements MatchableKey +func (k IgnoringAssetKey) Match(eachKey MatchableKey) bool { + if eachKey == nil { + return false + } + eachSub := eachKey.GetSubscription() + + return eachSub != nil && + eachSub.Channel == k.Channel && + eachSub.Pairs.Equal(k.Pairs) && + eachSub.Levels == k.Levels && + eachSub.Interval == k.Interval +} diff --git a/exchanges/subscription/keys_test.go b/exchanges/subscription/keys_test.go index 4a4054b2cf9..bd940ccba5d 100644 --- a/exchanges/subscription/keys_test.go +++ b/exchanges/subscription/keys_test.go @@ -110,6 +110,39 @@ func TestIgnoringPairsKeyString(t *testing.T) { assert.Equal(t, "ticker spot", key.String()) } +// TestIgnoringAssetKeyMatch exercises IgnoringAssetKey.Match +func TestIgnoringAssetKeyMatch(t *testing.T) { + t.Parallel() + + key := &IgnoringAssetKey{&Subscription{Channel: TickerChannel, Asset: asset.Spot}} + try := &DummyKey{&Subscription{Channel: OrderbookChannel}, t} + + require.False(t, key.Match(nil), "Match on a nil must return false") + require.False(t, key.Match(try), "Gate 1: Match must reject a bad Channel") + try.Channel = TickerChannel + require.True(t, key.Match(try), "Gate 1: Match must accept a good Channel") + key.Pairs = currency.Pairs{btcusdtPair} + require.False(t, key.Match(try), "Gate 2: Match must reject bad Pairs") + try.Pairs = currency.Pairs{btcusdtPair} + require.True(t, key.Match(try), "Gate 2: Match must accept a good Pairs") + key.Levels = 4 + require.False(t, key.Match(try), "Gate 3: Match must reject a bad Level") + try.Levels = 4 + require.True(t, key.Match(try), "Gate 3: Match must accept a good Level") + key.Interval = kline.FiveMin + require.False(t, key.Match(try), "Gate 4: Match must reject a bad Interval") + try.Interval = kline.FiveMin + require.True(t, key.Match(try), "Gate 4: Match must accept a good Interval") +} + +// TestIgnoringAssetKeyString exercises IgnoringAssetKey.String +func TestIgnoringAssetKeyString(t *testing.T) { + t.Parallel() + assert.Equal(t, "Uninitialised IgnoringAssetKey", IgnoringAssetKey{}.String()) + key := &IgnoringAssetKey{&Subscription{Asset: asset.Spot, Channel: TickerChannel, Pairs: currency.Pairs{ethusdcPair, btcusdtPair}}} + assert.Equal(t, "ticker [ETHUSDC BTCUSDT]", key.String()) +} + // TestGetSubscription exercises GetSubscription func TestGetSubscription(t *testing.T) { t.Parallel()