Skip to content

Commit

Permalink
Bitfinex: Test coverage for Unsubscribe errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Oct 1, 2023
1 parent beb8140 commit 95ee32b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
15 changes: 12 additions & 3 deletions exchanges/bitfinex/bitfinex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"log"
"os"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -1090,7 +1091,7 @@ func TestWsAuth(t *testing.T) {
func TestWsSubscribe(t *testing.T) {
setupWs(t)
err := b.Subscribe([]stream.ChannelSubscription{{Channel: wsTicker, Currency: currency.NewPair(currency.BTC, currency.USD), Asset: asset.Spot}})
assert.NoError(t, err)
assert.NoError(t, err, "Subrcribe should not error")
catcher := func() (ok bool) {
i := <-b.Websocket.DataHandler
_, ok = i.(*ticker.Price)
Expand All @@ -1113,9 +1114,17 @@ func TestWsSubscribe(t *testing.T) {
assert.Eventually(t, catcher, sharedtestvalues.WebsocketResponseDefaultTimeout, time.Millisecond*10, "error response should arrive")

subs, err := b.GetSubscriptions()
assert.NoError(t, err)
assert.NoError(t, err, "GetSubscriptions should not error")
err = b.Unsubscribe(subs)
assert.NoError(t, err)
assert.NoError(t, err, "Unsubscribing should not error")

chanID, ok := subs[0].Key.(int)
assert.True(t, ok, "sub.Key should be an int")

err = b.Unsubscribe(subs)
assert.ErrorIs(t, err, stream.ErrUnsubscribeFailure, "Unsubscribe should error")
assert.ErrorContains(t, err, strconv.Itoa(chanID), "Unsubscribe should contain correct chanId")
assert.ErrorContains(t, err, "unsubscribe: invalid (code: 10400)", "Unsubscribe should contain correct upstream error")

err = b.Subscribe([]stream.ChannelSubscription{{
Channel: wsTicker,
Expand Down
2 changes: 1 addition & 1 deletion exchanges/bitfinex/bitfinex_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ func (b *Bitfinex) unsubscribeFromChan(c *stream.ChannelSubscription) error {
}

if err := b.getErrResp(respRaw); err != nil {
wErr := fmt.Errorf("error unsubscribing from ChanId: %v; %w", chanID, err)
wErr := fmt.Errorf("%w from ChanId: %v; %w", stream.ErrUnsubscribeFailure, chanID, err)
b.Websocket.DataHandler <- wErr
return wErr
}
Expand Down
2 changes: 2 additions & 0 deletions exchanges/stream/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
var (
// ErrSubscriptionFailure defines an error when a subscription fails
ErrSubscriptionFailure = errors.New("subscription failure")
// ErrUnsubscribeFailure defines an error when a unsubscribe fails
ErrUnsubscribeFailure = errors.New("unsubscribe failure")
// ErrAlreadyDisabled is returned when you double-disable the websocket
ErrAlreadyDisabled = errors.New("websocket already disabled")
// ErrNotConnected defines an error when websocket is not connected
Expand Down

0 comments on commit 95ee32b

Please sign in to comment.