Skip to content

Commit

Permalink
fix bug add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan O'Hara-Reid committed Dec 12, 2024
1 parent 254ad2b commit e803f77
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion exchanges/stream/websocket_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,15 @@ func (w *WebsocketConnection) waitForResponses(ctx context.Context, signature an
defer timeout.Stop()

resps := make([][]byte, 0, expected)
inspection:
for range expected {
select {
case resp := <-ch:
resps = append(resps, resp)
// Checks recently received message to determine if this is in fact the final message in a sequence of messages.
if messageInspector != nil && messageInspector.IsFinal(resp) {
w.Match.RemoveSignature(signature)
return resps, nil
break inspection
}
case <-timeout.C:
w.Match.RemoveSignature(signature)
Expand Down
31 changes: 29 additions & 2 deletions exchanges/stream/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,36 @@ func TestSendMessageReturnResponse(t *testing.T) {
assert.ErrorIs(t, err, ErrSignatureTimeout, "SendMessageReturnResponse should error when request ID not found")
}

type inspection struct{}
func TestWaitForResponses(t *testing.T) {
t.Parallel()
dummy := &WebsocketConnection{
ResponseMaxLimit: time.Nanosecond,
Match: NewMatch(),
}
_, err := dummy.waitForResponses(context.Background(), "silly", nil, 1, inspection{})
require.ErrorIs(t, err, ErrSignatureTimeout)

ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = dummy.waitForResponses(ctx, "silly", nil, 1, inspection{})
require.ErrorIs(t, err, context.Canceled)

// test break early and hit verbose path
ch := make(chan []byte, 1)
ch <- []byte("hello")
ctx = request.WithVerbose(context.Background())
dummy.ResponseMaxLimit = time.Second
got, err := dummy.waitForResponses(ctx, "silly", ch, 2, inspection{breakEarly: true})
require.NoError(t, err, context.Canceled)

Check failure on line 792 in exchanges/stream/websocket_test.go

View workflow job for this annotation

GitHub Actions / lint

error-is-as: invalid usage of require.NoError, use require.NotErrorIs instead (testifylint)
require.Len(t, got, 1)
assert.Equal(t, "hello", string(got[0]))
}

type inspection struct {
breakEarly bool
}

func (inspection) IsFinal([]byte) bool { return false }
func (i inspection) IsFinal([]byte) bool { return i.breakEarly }

type reporter struct {
name string
Expand Down

0 comments on commit e803f77

Please sign in to comment.