mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-18 15:10:03 +00:00
* Initial plan * Fix TestMatchReturnResponses race condition panic Co-authored-by: thrasher- <4685270+thrasher-@users.noreply.github.com> * Add require.NotEmpty check on resp.Responses to prevent panic Co-authored-by: shazbert <9391715+shazbert@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: thrasher- <4685270+thrasher-@users.noreply.github.com> Co-authored-by: shazbert <9391715+shazbert@users.noreply.github.com>
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package websocket
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMatchReturnResponses(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
conn := connection{Match: NewMatch()}
|
|
_, err := conn.MatchReturnResponses(t.Context(), nil, 0)
|
|
require.ErrorIs(t, err, errInvalidBufferSize)
|
|
|
|
ch, err := conn.MatchReturnResponses(t.Context(), nil, 1)
|
|
require.NoError(t, err)
|
|
|
|
require.ErrorIs(t, (<-ch).Err, ErrSignatureTimeout)
|
|
conn.ResponseMaxLimit = time.Second
|
|
|
|
ch, err = conn.MatchReturnResponses(t.Context(), nil, 1)
|
|
require.NoError(t, err)
|
|
|
|
exp := []byte("test")
|
|
require.True(t, conn.Match.IncomingWithData(nil, exp))
|
|
resp := <-ch
|
|
require.NoError(t, resp.Err)
|
|
require.NotEmpty(t, resp.Responses, "must have response data")
|
|
assert.Equal(t, exp, resp.Responses[0])
|
|
}
|
|
|
|
func TestWebsocketConnectionRequireMatchWithData(t *testing.T) {
|
|
t.Parallel()
|
|
ws := connection{Match: NewMatch()}
|
|
err := ws.RequireMatchWithData(0, nil)
|
|
require.ErrorIs(t, err, ErrSignatureNotMatched)
|
|
|
|
ch, err := ws.Match.Set(0, 1)
|
|
require.NoError(t, err)
|
|
|
|
err = ws.RequireMatchWithData(0, []byte("test"))
|
|
require.NoError(t, err)
|
|
require.Len(t, ch, 1, "must have one item in channel")
|
|
assert.Equal(t, []byte("test"), <-ch)
|
|
}
|
|
|
|
func TestIncomingWithData(t *testing.T) {
|
|
t.Parallel()
|
|
ws := connection{Match: NewMatch()}
|
|
require.False(t, ws.IncomingWithData(0, nil))
|
|
|
|
ch, err := ws.Match.Set(0, 1)
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ws.IncomingWithData(0, []byte("test")))
|
|
require.Len(t, ch, 1, "must have one item in channel")
|
|
assert.Equal(t, []byte("test"), <-ch)
|
|
}
|