diff --git a/exchanges/bitfinex/bitfinex_test.go b/exchanges/bitfinex/bitfinex_test.go index a2c7520ac68..ccd7215e9f6 100644 --- a/exchanges/bitfinex/bitfinex_test.go +++ b/exchanges/bitfinex/bitfinex_test.go @@ -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" @@ -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") +} diff --git a/exchanges/bitfinex/bitfinex_websocket.go b/exchanges/bitfinex/bitfinex_websocket.go index 7e938229e82..036650246a9 100644 --- a/exchanges/bitfinex/bitfinex_websocket.go +++ b/exchanges/bitfinex/bitfinex_websocket.go @@ -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,