Files
gocryptotrader/exchanges/stream/stream_match_test.go
Ryan O'Hara-Reid 17c2ef2ec7 stream/match: Reduce complexity and limit locking when match occurs (#1581)
* stream match update

* update tests

* linter: fix

* glorious: nits + handle context cancellations

* glorious: whooops

* Websocket: Add SendMessageReturnResponses

* whooooooopsie

* gk: nitssssss

* Update exchanges/stream/stream_match.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/stream/stream_match_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* linter: appease the linter gods

* glorious: nits

* glorious: nits

* Update exchanges/stream/stream_match_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
2024-08-19 10:35:46 +10:00

54 lines
1.6 KiB
Go

package stream
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMatch(t *testing.T) {
t.Parallel()
load := []byte("42")
assert.False(t, new(Match).IncomingWithData("hello", load), "Should not match an uninitialised Match")
match := NewMatch()
assert.False(t, match.IncomingWithData("hello", load), "Should not match an empty signature")
_, err := match.Set("hello", 0)
require.ErrorIs(t, err, errInvalidBufferSize, "Must error on zero buffer size")
_, err = match.Set("hello", -1)
require.ErrorIs(t, err, errInvalidBufferSize, "Must error on negative buffer size")
ch, err := match.Set("hello", 2)
require.NoError(t, err, "Set must not error")
assert.True(t, match.IncomingWithData("hello", []byte("hello")))
assert.Equal(t, "hello", string(<-ch))
_, err = match.Set("hello", 2)
assert.ErrorIs(t, err, errSignatureCollision, "Should error on signature collision")
assert.True(t, match.IncomingWithData("hello", load), "Should match with matching message and signature")
assert.False(t, match.IncomingWithData("hello", load), "Should not match with matching message and signature")
assert.Len(t, ch, 1, "Channel should have 1 items, 1 was already read above")
}
func TestRemoveSignature(t *testing.T) {
t.Parallel()
match := NewMatch()
ch, err := match.Set("masterblaster", 1)
select {
case <-ch:
t.Fatal("Should not be able to read from an empty channel")
default:
}
require.NoError(t, err)
match.RemoveSignature("masterblaster")
select {
case garbage := <-ch:
require.Empty(t, garbage)
default:
t.Fatal("Should be able to read from a closed channel")
}
}