Skip to content

Commit

Permalink
Address linter complains
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Aug 17, 2024
1 parent 8301bd0 commit c610fc3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 151 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
)

// DerivAPI is the main struct for the DerivAPI client.
type DerivAPI struct {
type DerivAPI struct { //nolint:revive // don't want to change the name for now
reqChan chan APIReqest
Endpoint *url.URL
keepAliveOnDisconnect chan bool
Expand Down
4 changes: 4 additions & 0 deletions custom_subscription_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package deriv
import "github.com/ksysoev/deriv-api/schema"

// SubscribeTicksHistory Get historic tick data for a given symbol.
//
//nolint:gocritic // don't want to break backward compatibility for now
func (api *DerivAPI) SubscribeTicksHistory(r schema.TicksHistory) (rsp schema.TicksHistoryResp, s *Subsciption[schema.TicksHistoryResp, schema.TicksResp], err error) {
var f schema.TicksHistorySubscribe = 1

Expand All @@ -17,6 +19,8 @@ func (api *DerivAPI) SubscribeTicksHistory(r schema.TicksHistory) (rsp schema.Ti
}

// SubscribeTicksHistory Get historic candles data for a given symbol.
//
//nolint:gocritic // don't want to break backward compatibility for now
func (api *DerivAPI) SubscribeCandlesHistory(r schema.TicksHistory) (rsp schema.TicksHistoryResp, s *Subsciption[schema.TicksHistoryResp, schema.TicksHistoryResp], err error) {
var f schema.TicksHistorySubscribe = 1

Expand Down
202 changes: 52 additions & 150 deletions subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,171 +383,72 @@ func TestForgetFailed(t *testing.T) {
}
}

func TestStartAPIError(t *testing.T) {
testResp := `{
"echo_req": {
"subscribe": 1,
"ticks": "R_50"
func TestStartInvalidResponses(t *testing.T) {
tests := []struct{ name, resp string }{
{
name: "InvalidResponse",
resp: `{
"req_id": 1,
"msg_type": "tick",
"subscription": {
"id": "9ed45a5e-8f87-c735-2b63-36108719eadd"
},
"tick": 1
}`,
},
{
name: "APIError",
resp: `{
"echo_req": {
"subscribe": 1,
"ticks": "R_50"
},
"req_id": 1,
"msg_type": "tick",
"error": {
"code": "WrongRequest",
"message": "Invalid request"
}
}`,
},
"req_id": 1,
"msg_type": "tick",
"error": {
"code": "WrongRequest",
"message": "Invalid request"
}
}`

server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
if err := ws.Write(context.Background(), websocket.MessageText, []byte(testResp)); err != nil {
t.Errorf("Unexpected error: %v", err)
}

time.Sleep(time.Second) // to keep the connection open
}))

defer server.Close()

url := "ws://" + server.Listener.Addr().String()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")

if err := api.Connect(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

sub := NewSubcription[schema.TicksResp, schema.TicksResp](api)

if sub == nil {
t.Errorf("Expected a subscription, but got nil")
}

reqID := 1
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
if err := ws.Write(context.Background(), websocket.MessageText, []byte(test.resp)); err != nil {
t.Errorf("Unexpected error: %v", err)
}

var f schema.TicksSubscribe = 1
time.Sleep(time.Second) // to keep the connection open
}))

req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID}
defer server.Close()

if _, err := sub.Start(reqID, req); err == nil {
t.Errorf("Expected an error, but got nil")
}
}
url := "ws://" + server.Listener.Addr().String()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")

func TestStartInvalidResponse(t *testing.T) {
testResp := `{
"req_id": 1,
"msg_type": "tick",
"subscription": {
"id": "9ed45a5e-8f87-c735-2b63-36108719eadd"
},
"tick": 1
}`
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
if err := ws.Write(context.Background(), websocket.MessageText, []byte(testResp)); err != nil {
if err := api.Connect(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

time.Sleep(time.Second) // to keep the connection open
}))

defer server.Close()

url := "ws://" + server.Listener.Addr().String()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")

if err := api.Connect(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

sub := NewSubcription[schema.TicksResp, schema.TicksResp](api)

if sub == nil {
t.Errorf("Expected a subscription, but got nil")
}

reqID := 1

var f schema.TicksSubscribe = 1

req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID}

if _, err := sub.Start(reqID, req); err == nil {
t.Errorf("Expected an error, but got nil")
}
}

func TestStartInvalidResponseInSubscription(t *testing.T) {
responses := []string{`{
"echo_req": {
"subscribe": 1,
"ticks": "R_50"
},
"req_id": 1,
"msg_type": "tick",
"subscription": {
"id": "9ed45a5e-8f87-c735-2b63-36108719eadd"
},
"tick": {
"ask": 186.9688,
"bid": 186.9488,
"epoch": 1679722832,
"id": "9ed45a5e-8f87-c735-2b63-36108719eadd",
"pip_size": 4,
"quote": 186.9588,
"symbol": "R_50"
}
}`,
`{ "req_id": 1 }`}
sub := NewSubcription[schema.TicksResp, schema.TicksResp](api)

server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
for _, resp := range responses {
if err := ws.Write(context.Background(), websocket.MessageText, []byte(resp)); err != nil {
t.Errorf("Unexpected error: %v", err)
}
if sub == nil {
t.Errorf("Expected a subscription, but got nil")
}

time.Sleep(time.Second) // to keep the connection open
}))

defer server.Close()

url := "ws://" + server.Listener.Addr().String()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")

if err := api.Connect(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

sub := NewSubcription[schema.TicksResp, schema.TicksResp](api)

if sub == nil {
t.Errorf("Expected a subscription, but got nil")
}

reqID := 1

var f schema.TicksSubscribe = 1

req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID}
initResp, err := sub.Start(reqID, req)
reqID := 1

if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
var f schema.TicksSubscribe = 1

// First message
if *initResp.Tick.Quote != 186.9588 {
t.Errorf("Expected message to be %v, but got %v", 186.9588, *initResp.Tick.Quote)
}
req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID}

// Second message
select {
case tick, ok := <-sub.GetStream():
if ok {
t.Errorf("Expected to get noting, but got response: %v", tick)
}
case <-time.After(time.Millisecond):
if _, err := sub.Start(reqID, req); err == nil {
t.Errorf("Expected an error, but got nil")
}
})
}
}

Expand Down Expand Up @@ -583,7 +484,8 @@ func TestStartAPIErrorInSubscription(t *testing.T) {
"code": "InvalidSymbol",
"message": "Invalid symbol"
}
}`}
}`,
`{ "req_id": 1 }`}

server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
Expand Down

0 comments on commit c610fc3

Please sign in to comment.