Skip to content

Commit

Permalink
Bitfinex: Add tests for chanForSub
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Sep 13, 2023
1 parent 068dfea commit f2bcb78
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions exchanges/bitfinex/bitfinex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
Expand Down Expand Up @@ -1854,3 +1855,26 @@ func TestCancelMultipleOrdersV2(t *testing.T) {
t.Error(err)
}
}

func TestChanForSub(t *testing.T) {
t.Parallel()
p := currency.NewPairWithDelimiter("DOGE", "XLM", "-")
s, err := b.chanForSub(wsBook, asset.Spot, p)
assert.ErrorIs(t, err, errSubNotFound, "Correct error returned when stream when sub not found")
assert.Nil(t, s, "No stream returned when sub not found")

// Add a spare sub to ensure we don't get only-answer-is-right syndrome
b.Websocket.AddSuccessfulSubscriptions(stream.ChannelSubscription{Asset: asset.Spot, Currency: btcusdPair, Channel: wsTicker})

want := stream.ChannelSubscription{Asset: asset.Spot, Currency: p, Channel: wsBook}
b.Websocket.AddSuccessfulSubscriptions(want)
s, err = b.chanForSub(wsBook, asset.Spot, p)
assert.Nil(t, err, "No error returned when sub found")
assert.EqualValues(t, want, *s, "Correct Sub found")

dup := stream.ChannelSubscription{Asset: asset.Spot, Currency: p, Channel: wsBook, Params: map[string]interface{}{"muffins": "yummy"}}
b.Websocket.AddSuccessfulSubscriptions(dup)
s, err = b.chanForSub(wsBook, asset.Spot, p)
assert.ErrorIs(t, err, errTooManyMatchingSubs, "Correct error returns when too many subs found")
assert.Nil(t, s, "No stream returned when too many subs found")
}
2 changes: 1 addition & 1 deletion exchanges/bitfinex/bitfinex_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ func (b *Bitfinex) resubOrderbook(p currency.Pair, assetType asset.Item) error {
}

// chanForSub returns an existing channel subscription for a given channel/asset/pair
func (b *Bitfinex) chanForSub(cName string, assetType asset.Item, pair currency.Pair) (*stream.ChannelSubscription, error) {
func (b *Bitfinex) chanForSub(cName string, assetType asset.Item, pair currency.Pair) (*stream.ChannelSubscription, error) { //nolint:unparam // cName open for future uses
var c *stream.ChannelSubscription
want := &stream.ChannelSubscription{
Channel: cName,
Expand Down

0 comments on commit f2bcb78

Please sign in to comment.