Skip to content

Commit

Permalink
Websocket: Improve test clarity for Sub/Unsub
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Sep 15, 2023
1 parent 6b52942 commit 780429a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 48 deletions.
7 changes: 3 additions & 4 deletions exchanges/stream/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var (
errClosedConnection = errors.New("use of closed network connection")
errSubscriptionNotFound = errors.New("subscription not found in list")
errAlreadySubscribed = errors.New("already subscribed")
errNoChannelsInArgs = errors.New("no channels in args")
)

var globalReporter Reporter
Expand Down Expand Up @@ -876,8 +877,7 @@ newsubs:
// UnsubscribeChannels unsubscribes from a websocket channel
func (w *Websocket) UnsubscribeChannels(channels []ChannelSubscription) error {
if len(channels) == 0 {
return fmt.Errorf("%s websocket: channels not populated cannot remove",
w.exchangeName)
return fmt.Errorf("%s websocket: %w", w.exchangeName, errNoChannelsInArgs)
}
w.subscriptionMutex.Lock()

Expand Down Expand Up @@ -910,8 +910,7 @@ func (w *Websocket) ResubscribeToChannel(subscribedChannel *ChannelSubscription)
// SubscribeToChannels appends supplied channels to channelsToSubscribe
func (w *Websocket) SubscribeToChannels(channels []ChannelSubscription) error {
if len(channels) == 0 {
return fmt.Errorf("%s websocket: cannot subscribe no channels supplied",
w.exchangeName)
return fmt.Errorf("%s websocket: %w", w.exchangeName, errNoChannelsInArgs)
}
w.subscriptionMutex.Lock()
for x := range channels {
Expand Down
54 changes: 10 additions & 44 deletions exchanges/stream/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,7 @@ func TestWebsocket(t *testing.T) {
func TestSubscribeUnsubscribe(t *testing.T) {
t.Parallel()
ws := *New()
err := ws.Setup(defaultSetup)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, ws.Setup(defaultSetup), "WS Setup should not error")

fnSub := func(subs []ChannelSubscription) error {
ws.AddSuccessfulSubscriptions(subs...)
Expand All @@ -510,46 +507,17 @@ func TestSubscribeUnsubscribe(t *testing.T) {
ws.Subscriber = fnSub
ws.Unsubscriber = fnUnsub

err = ws.UnsubscribeChannels(nil)
if err == nil {
t.Fatal("error cannot be nil")
}

// Generate test sub
subs, err := ws.GenerateSubs()
if err != nil {
t.Fatal(err)
}

// unsub when no subscribed channel
err = ws.UnsubscribeChannels(subs)
if err == nil {
t.Fatal("error cannot be nil")
}

err = ws.SubscribeToChannels(subs)
if err != nil {
t.Fatal(err)
}

// subscribe when already subscribed
err = ws.SubscribeToChannels(subs)
if err == nil {
t.Fatal("error cannot be nil")
}

// subscribe to nothing
err = ws.SubscribeToChannels(nil)
if err == nil {
t.Fatal("error cannot be nil")
}

err = ws.UnsubscribeChannels(subs)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err, "Generating test subscriptions should not error")
assert.ErrorIs(t, ws.UnsubscribeChannels(nil), errNoChannelsInArgs, "Unsubscribing from nil should error")
assert.ErrorIs(t, ws.UnsubscribeChannels(subs), errSubscriptionNotFound, "Unsubscribing should error when not subscribed")
assert.NoError(t, ws.SubscribeToChannels(subs), "Basic Subscribing should not error")
assert.ErrorIs(t, ws.SubscribeToChannels(subs), errAlreadySubscribed, "Subscribe should error when already subscribed")
assert.ErrorIs(t, ws.SubscribeToChannels(nil), errNoChannelsInArgs, "Subscribe to nil should error")
assert.NoError(t, ws.UnsubscribeChannels(subs), "Unsubscribing should not error")
}

// TestResubscribe tests Resubscribing to existing subscriptions
func TestResubscribe(t *testing.T) {
t.Parallel()
ws := *New()
Expand Down Expand Up @@ -606,9 +574,7 @@ func TestGetSubscriptions(t *testing.T) {
},
},
}
if !strings.EqualFold("hello3", w.GetSubscriptions()[0].Channel) {
t.Error("Subscriptions was not copied properly")
}
assert.Equal(t, "hello3", w.GetSubscriptions()[0].Channel, "GetSubscriptions should return the correct channel details")
}

// TestSetCanUseAuthenticatedEndpoints logic test
Expand Down

0 comments on commit 780429a

Please sign in to comment.