Skip to content

Commit

Permalink
Refactors subscription tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Aug 17, 2024
1 parent cd5719c commit 9eeac6c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 70 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/ksysoev/deriv-api

go 1.20

require (
github.com/coder/websocket v1.8.12
golang.org/x/net v0.24.0
)
require github.com/coder/websocket v1.8.12
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
131 changes: 67 additions & 64 deletions subscriptions_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package deriv

import (
"context"
"errors"
"fmt"
"net/http/httptest"
"reflect"
"testing"
"time"

"github.com/coder/websocket"
"github.com/ksysoev/deriv-api/schema"
"golang.org/x/net/websocket"
)

func TestParseSubscription_ValidInput(t *testing.T) {
Expand Down Expand Up @@ -92,13 +92,12 @@ func TestStart(t *testing.T) {
"symbol": "R_50"
}
}`
server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
ws.Write([]byte(testResp))
ws.Write([]byte(testResp))
time.Sleep(time.Second) // to keep the connection open
}))
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
ws.Write(context.Background(), websocket.MessageText, []byte(testResp))
ws.Write(context.Background(), websocket.MessageText, []byte(testResp))
time.Sleep(time.Second) // to keep the connection open
}))
url := "ws://" + server.Listener.Addr().String()

defer server.Close()
Expand Down Expand Up @@ -148,12 +147,11 @@ func TestStart(t *testing.T) {
}

func TestStartFailed(t *testing.T) {
server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
ws.Write([]byte(""))
time.Sleep(time.Second) // to keep the connection open
}))
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
ws.Write(context.Background(), websocket.MessageText, []byte(""))
time.Sleep(time.Second) // to keep the connection open
}))
url := "ws://" + server.Listener.Addr().String()
server.Close()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")
Expand Down Expand Up @@ -193,14 +191,16 @@ func TestForget(t *testing.T) {
"symbol": "R_50"
}
}`
server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
for resp := range responses {
ws.Write([]byte(resp))
}
time.Sleep(time.Second) // to keep the connection open
}))

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

for resp := range responses {
ws.Write(context.Background(), websocket.MessageText, []byte(resp))
}

time.Sleep(time.Second) // to keep the connection open
}))
url := "ws://" + server.Listener.Addr().String()
defer server.Close()

Expand Down Expand Up @@ -280,14 +280,16 @@ func TestForgetFailed(t *testing.T) {
"symbol": "R_50"
}
}`
server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
for resp := range responses {
ws.Write([]byte(resp))
}
time.Sleep(time.Second) // to keep the connection open
}))

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

for resp := range responses {
ws.Write(context.Background(), websocket.MessageText, []byte(resp))
}

time.Sleep(time.Second) // to keep the connection open
}))
url := "ws://" + server.Listener.Addr().String()
defer server.Close()

Expand Down Expand Up @@ -358,12 +360,12 @@ func TestStartAPIError(t *testing.T) {
"message": "Invalid request"
}
}`
server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
ws.Write([]byte(testResp))
time.Sleep(time.Second) // to keep the connection open
}))

server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
ws.Write(context.Background(), websocket.MessageText, []byte(testResp))
time.Sleep(time.Second) // to keep the connection open
}))
url := "ws://" + server.Listener.Addr().String()
defer server.Close()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")
Expand Down Expand Up @@ -397,12 +399,12 @@ func TestStartInvalidResponse(t *testing.T) {
},
"tick": 1
}`
server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
ws.Write([]byte(testResp))
time.Sleep(time.Second) // to keep the connection open
}))
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
ws.Write(context.Background(), websocket.MessageText, []byte(testResp))
time.Sleep(time.Second) // to keep the connection open
}))

url := "ws://" + server.Listener.Addr().String()
defer server.Close()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")
Expand Down Expand Up @@ -450,14 +452,15 @@ func TestStartInvalidResponseInSubscription(t *testing.T) {
}`,
`{ "req_id": 1 }`}

server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
for _, resp := range responses {
ws.Write([]byte(resp))
}
time.Sleep(time.Second) // to keep the connection open
}))
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {

for _, resp := range responses {
ws.Write(context.Background(), websocket.MessageText, []byte(resp))
}

time.Sleep(time.Second) // to keep the connection open
}))
url := "ws://" + server.Listener.Addr().String()
defer server.Close()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")
Expand Down Expand Up @@ -531,14 +534,15 @@ func TestStartAPIErrorInSubscription(t *testing.T) {
}
}`}

server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
for _, resp := range responses {
ws.Write([]byte(resp))
}
time.Sleep(time.Second) // to keep the connection open
}))
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {

for _, resp := range responses {
ws.Write(context.Background(), websocket.MessageText, []byte(resp))
}

time.Sleep(time.Second) // to keep the connection open
}))
url := "ws://" + server.Listener.Addr().String()
defer server.Close()
api, _ := NewDerivAPI(url, 123, "en", "http://example.com")
Expand Down Expand Up @@ -579,11 +583,10 @@ func TestStartAPIErrorInSubscription(t *testing.T) {
}

func TestStartTimeout(t *testing.T) {
server := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var msg string
_ = websocket.Message.Receive(ws, &msg) // wait for request
time.Sleep(time.Second) // to keep the connection open
}))
server := newMockWSServer(
onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {
time.Sleep(time.Second) // to keep the connection open
}))
defer server.Close()
url := "ws://" + server.Listener.Addr().String()

Expand Down

0 comments on commit 9eeac6c

Please sign in to comment.