Skip to content

Commit

Permalink
Subscription: Fix Match Key no pairs candidates have pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Feb 12, 2024
1 parent 1b0637b commit f5f2b93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
13 changes: 12 additions & 1 deletion exchanges/kraken/kraken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,8 @@ func TestWsSubscribe(t *testing.T) {
func TestWsOrderbookSub(t *testing.T) {
t.Parallel()

k := new(Kraken)
require.NoError(t, testexch.TestInstance(k), "TestInstance must not error")
testexch.SetupWs(t, k)

err := k.Subscribe([]subscription.Subscription{{
Expand Down Expand Up @@ -1289,6 +1291,10 @@ func TestWsOrderbookSub(t *testing.T) {

// TestWsCandlesSub tests caPairss subscription for Timeframe params
func TestWsCandlesSub(t *testing.T) {
t.Parallel()

k := new(Kraken)
require.NoError(t, testexch.TestInstance(k), "TestInstance must not error")
testexch.SetupWs(t, k)

err := k.Subscribe([]subscription.Subscription{{
Expand Down Expand Up @@ -1322,9 +1328,14 @@ func TestWsCandlesSub(t *testing.T) {

// TestWsOwnTradesSub tests the authenticated WS subscription channel for trades
func TestWsOwnTradesSub(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, k)
t.Parallel()

k := new(Kraken)
require.NoError(t, testexch.TestInstance(k), "TestInstance must not error")
testexch.SetupWs(t, k)

sharedtestvalues.SkipTestIfCredentialsUnset(t, k)

err := k.Subscribe([]subscription.Subscription{{Channel: krakenWsOwnTrades}})
assert.NoError(t, err, "Subsrcibing to ownTrades should not error")

Expand Down
3 changes: 1 addition & 2 deletions exchanges/stream/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,7 @@ func (w *Websocket) AddSuccessfulSubscriptions(channels ...subscription.Subscrip
if w.subscriptions == nil {
w.subscriptions = subscription.Map{}
}
for _, cN := range channels {
c := cN // cN is an iteration var; Not safe to make a pointer to
for _, c := range channels {
key := c.EnsureKeyed()
c.State = subscription.SubscribedState
w.subscriptions[key] = &c
Expand Down
11 changes: 7 additions & 4 deletions exchanges/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (s *Subscription) EnsureKeyed() any {

// Match returns the first subscription which matches the Key's Asset, Channel and Pairs
// If the key provided has:
// * Empty pairs then only Subscriptions without pairs will be considered
// * >=1 pairs then Subscriptions which contain all the pairs will be considered
// 1) Empty pairs then only Subscriptions without pairs will be considered
// 2) >=1 pairs then Subscriptions which contain all the pairs will be considered
func (k Key) Match(m Map) *Subscription {
for anyKey, s := range m {
candidate, ok := anyKey.(Key)
Expand All @@ -103,8 +103,11 @@ func (k Key) Match(m Map) *Subscription {
if k.Asset != candidate.Asset {
continue
}
if (k.Pairs == nil || len(*k.Pairs) == 0) && (candidate.Pairs == nil || len(*candidate.Pairs) == 0) {
return s
if k.Pairs == nil || len(*k.Pairs) == 0 {
if candidate.Pairs == nil || len(*candidate.Pairs) == 0 {
return s
}
continue // Case (1) - key doesn't have any pairs but candidate does
}
if err := candidate.Pairs.ContainsAll(*k.Pairs, true); err == nil {
return s
Expand Down

0 comments on commit f5f2b93

Please sign in to comment.