Skip to content

Commit

Permalink
Subscriptions: Add IgnoringAssetsKey
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Jul 23, 2024
1 parent f9f27f3 commit 6e00563
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
35 changes: 35 additions & 0 deletions exchanges/subscription/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 33 additions & 0 deletions exchanges/subscription/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6e00563

Please sign in to comment.