Skip to content

Commit

Permalink
Bitfinex: Fix TestWsSubscribedResponse
Browse files Browse the repository at this point in the history
* Vastly simplifies what fe need to test
This test was working to ensure that the various fancy key parsing
mechanisms all worked. Now that we use subId, we just need a thorough
test of that
* Expose Match.Set in order to capture websocket incoming data
Can't see another way of doing this. Doesn't seem too bad
  • Loading branch information
gbjk committed Sep 17, 2023
1 parent b7d68ff commit 52358a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 41 deletions.
52 changes: 13 additions & 39 deletions exchanges/bitfinex/bitfinex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"testing"
"time"

"github.com/buger/jsonparser"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
Expand Down Expand Up @@ -1222,45 +1224,17 @@ func TestWsCancelOffer(t *testing.T) {
}

func TestWsSubscribedResponse(t *testing.T) {
b.Websocket.AddSuccessfulSubscriptions(stream.ChannelSubscription{Asset: asset.Spot, Currency: btcusdPair, Channel: wsTicker, Params: map[string]interface{}{"chanId": 224555}})
if err := b.wsHandleData([]byte(`{"event":"subscribed","channel":"ticker","chanId":224555,"symbol":"tBTCUSD","pair":"BTCUSD"}`)); err != nil {
t.Error(err)
}

// Spot Candles
b.Websocket.AddSuccessfulSubscriptions(stream.ChannelSubscription{Asset: asset.Spot, Currency: btcusdPair, Channel: wsCandles, Params: map[string]interface{}{"chanId": 224556}})
if err := b.wsHandleData([]byte(`{"event":"subscribed","channel":"candles","chanId":224556,"key":"trade:1m:tBTCUSD"}`)); err != nil {
t.Error(err)
}

pair, err := currency.NewPairFromString("BTC:CNHT")
if err != nil {
t.Error(err)
}
b.Websocket.AddSuccessfulSubscriptions(stream.ChannelSubscription{Asset: asset.Spot, Currency: pair, Channel: wsCandles, Params: map[string]interface{}{"chanId": 224557}})
pressXToJSON := `{"event":"subscribed","channel":"candles","chanId":224557,"key":"trade:1m:tBTC:CNHT"}`
if err = b.wsHandleData([]byte(pressXToJSON)); err != nil {
t.Error(err)
}

// Margin Candles
pair, err = currency.NewPairFromString("BTC")
if err != nil {
t.Error(err)
}
b.Websocket.AddSuccessfulSubscriptions(stream.ChannelSubscription{Asset: asset.MarginFunding, Currency: pair, Channel: wsCandles, Params: map[string]interface{}{"chanId": 224558}})
if e2 := b.wsHandleData([]byte(`{"event":"subscribed","channel":"candles","chanId":224558,"key":"trade:1m:fBTC:a30:p2:p30"}`)); e2 != nil {
t.Error(e2)
}

pair, err = currency.NewPairFromString("USD")
if err != nil {
t.Error(err)
}
b.Websocket.AddSuccessfulSubscriptions(stream.ChannelSubscription{Asset: asset.MarginFunding, Currency: pair, Channel: wsCandles, Params: map[string]interface{}{"chanId": 224559}})
if e2 := b.wsHandleData([]byte(`{"event":"subscribed","channel":"candles","chanId":224559,"key":"trade:1m:fUSD:p30"}`)); e2 != nil {
t.Error(e2)
}
m, err := b.Websocket.Match.Set("subscribe:waiter1")
assert.NoError(t, err, "Setting a matcher should not error")
err = b.wsHandleData([]byte(`{"event":"subscribed","channel":"ticker","chanId":224555,"subId":"waiter1","symbol":"tBTCUSD","pair":"BTCUSD"}`))
assert.NoError(t, err, "wsHandleData should not error")
if assert.NotEmpty(t, m.C, "Matcher should have received a sub notification") {
msg := <-m.C
cId, err := jsonparser.GetInt(msg, "chanId")

Check warning on line 1233 in exchanges/bitfinex/bitfinex_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var cId should be cID (revive)
assert.NoError(t, err, "Should get chanId from sub notification without error")
assert.EqualValues(t, 224555, cId, "Should get the correct chanId through the matcher notification")
}
m.Cleanup()
}

func TestWsTradingPairSnapshot(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exchanges/stream/stream_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (m *Match) IncomingWithData(signature interface{}, data []byte) bool {
}

// Sets the signature response channel for incoming data
func (m *Match) set(signature interface{}) (matcher, error) {
func (m *Match) Set(signature interface{}) (matcher, error) {
var ch chan []byte
m.mu.Lock()
if _, ok := m.m[signature]; ok {
Expand Down
2 changes: 1 addition & 1 deletion exchanges/stream/websocket_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// SendMessageReturnResponse will send a WS message to the connection and wait
// for response
func (w *WebsocketConnection) SendMessageReturnResponse(signature, request interface{}) ([]byte, error) {
m, err := w.Match.set(signature)
m, err := w.Match.Set(signature)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 52358a6

Please sign in to comment.