Files
gocryptotrader/exchanges/exchange_websocket_test.go
Scott 6a15ecc65c OKEX/OKCoin API combine via OKGroup (#252)
* Initial commit

* Successful authenticated request implementation.

* Removes data from okcoin, okex. Implements some account okgroup endpoints. Adds tests

* Finishes account API endpoint implementations.

* Implements and adds tests for the following okgroup v3 API endpoints: GetSpotTradingAccounts, GetSpotTradingAccountForCurrency, GetSpotBillDetailsForCurrency, PlaceSpotOrder, PlaceMultipleSpotOrders, CancelSpotOrder, CancelMultipleSpotOrders, GetSpotOrders, GetSpotOpenOrders, GetSpotOrder, GetSpotTransactionDetails, GetSpotTokenPairDetails, GetSpotOrderBook, GetSpotAllTokenPairsInformation, GetSpotAllTokenPairsInformationForCurrency, GetSpotFilledOrdersInformation, GetSpotMarketData

* Implements and adds tests for all margin api endpoints: GetMarginTradingAccounts, GetMarginTradingAccountsForCurrency, GetMarginBillDetails, GetMarginAccountSettings, GetMarginLoanHistory, OpenMarginLoan, RepayMarginLoan, PlaceMarginOrder, PlaceMultipleMarginOrders, CancelMarginOrder, CancelMultipleMarginOrders, GetMarginOrders, GetMarginOpenOrders, GetMarginOrder, GetMarginTransactionDetails. Simplifies some Spot endpoints combining ForCurrency funcs where possible

* Adds all 29 Futures endpoints with tests. Updates comments and naming conventions. Adds standard realordertest func. Adds ability to make public API requests. Adds test purpose comments

* Adds 29 endpoints for SWAP API support. Adds tests for each endpoint. Declares response variables in function declaration. Simplifies URL parameter formatting

* Adds all ETT endpoints with tests

* Creates OKCoin and OKEX struct types. Moves okgroup tests to okcoin and okex exchanges. Updates withdraw fee calculation. Updates exchange.go exchange declaration to point to new types. Streamlines wrapper tests. Begins websocket integration

* Rebase fixes

* Deletes okcoin_types.go, okcoin_wrapper.go, okex_types.go, okex_wrapper.go. Combines okex,okcoin wrappers in okgroup_wrapper.go. Removes boilerplate url.values with new request struct type parsing. Adds github.com/google/go-querystring to go modules. Replaces USDT with USD for OKCoin tests. Moves OKEX specific endpoints (futures, swap & ett) to okex.go. Fixes recieving receiving again

* Maps the wrapper

* Parses json into time.Time instead of string + conversion

* Renames websocket.SetEnabled to websocket.SetWsStatusAndConnection. Maps main spot websocket functions for okgroup. Adds some basic ws tests

* Updates websocket default URLS, adds checksum tests, removes setdefaults from okgroup, adds WebsocketDataWrapper to wrap all okgroup websocket data responses, handles spot, swap, index and futures ticker, candle, trade, orderbook, funding fee websocket responses. Partially implements checksum validation on orderbook data. Fixes all linting issues

* Handles the calculation of okgroup websocket checksums. Adds tests

* Now all orderbook checksums are validated. Cleans up implementations and removes invalid checksum calculator functions. Adds function to parse ordertype. Puts verbose logs behind verbose check

* Removes parallel from okgroup tests. Removes unused code. Adds GetWsChannelWithoutOrderType. Improves handling of WS data types. Adds types for more ws channels. Simplifies update orderbook handling

* updates btse func name

* linting

* Fixes websocket connection issue with tests. Removes test verbosity

* Updates checksum calculation to handle more than 7 decimal places. Adds rate limiters. Uses != "" instead of len > 0. Adds new test to handle checksum calculation with 8 decimal places.

* Removes logging. Fixes orderbook wrapper references

* Adds slightly more robust resubscribe func. Adds websocket tests that can detect websocket errors

* Fixes linting issues

* Adds new WS func IsConnected() to expose ws status. Tests protect against channel timeout

* Adds test comments. Fixes parallel issues for futures tests

* Fixes error output for wrapper function
2019-03-18 15:18:36 +11:00

337 lines
8.4 KiB
Go

package exchange
import (
"testing"
"time"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
)
var wsTest Base
func TestWebsocketInit(t *testing.T) {
if wsTest.Websocket != nil {
t.Error("test failed - WebsocketInit() error")
}
wsTest.WebsocketInit()
if wsTest.Websocket == nil {
t.Error("test failed - WebsocketInit() error")
}
}
func TestWebsocket(t *testing.T) {
if err := wsTest.Websocket.SetProxyAddress("testProxy"); err != nil {
t.Error("test failed - SetProxyAddress", err)
}
wsTest.WebsocketSetup(func() error { return nil },
"testName",
true,
"testDefaultURL",
"testRunningURL")
// Test variable setting and retreival
if wsTest.Websocket.GetName() != "testName" {
t.Error("test failed - WebsocketSetup")
}
if !wsTest.Websocket.IsEnabled() {
t.Error("test failed - WebsocketSetup")
}
if wsTest.Websocket.GetProxyAddress() != "testProxy" {
t.Error("test failed - WebsocketSetup")
}
if wsTest.Websocket.GetDefaultURL() != "testDefaultURL" {
t.Error("test failed - WebsocketSetup")
}
if wsTest.Websocket.GetWebsocketURL() != "testRunningURL" {
t.Error("test failed - WebsocketSetup")
}
// Test websocket connect and shutdown functions
comms := make(chan struct{}, 1)
go func() {
var count int
for {
if count == 4 {
close(comms)
return
}
select {
case <-wsTest.Websocket.Connected:
count++
case <-wsTest.Websocket.Disconnected:
count++
}
}
}()
// -- Not connected shutdown
err := wsTest.Websocket.Shutdown()
if err == nil {
t.Fatal("test failed - should not be connected to able to shut down")
}
// -- Normal connect
err = wsTest.Websocket.Connect()
if err != nil {
t.Fatal("test failed - WebsocketSetup", err)
}
// -- Already connected connect
err = wsTest.Websocket.Connect()
if err == nil {
t.Fatal("test failed - should not connect, already connected")
}
wsTest.Websocket.SetWebsocketURL("")
// -- Set true when already true
err = wsTest.Websocket.SetWsStatusAndConnection(true)
if err == nil {
t.Fatal("test failed - setting enabled should not work")
}
// -- Set false normal
err = wsTest.Websocket.SetWsStatusAndConnection(false)
if err != nil {
t.Fatal("test failed - setting enabled should not work")
}
// -- Set true normal
err = wsTest.Websocket.SetWsStatusAndConnection(true)
if err != nil {
t.Fatal("test failed - setting enabled should not work")
}
// -- Normal shutdown
err = wsTest.Websocket.Shutdown()
if err != nil {
t.Fatal("test failed - WebsocketSetup", err)
}
timer := time.NewTimer(5 * time.Second)
select {
case <-comms:
case <-timer.C:
t.Fatal("test failed - WebsocketSetup - timeout")
}
}
func TestInsertingSnapShots(t *testing.T) {
var snapShot1 orderbook.Base
asks := []orderbook.Item{
{Price: 6000, Amount: 1, ID: 1},
{Price: 6001, Amount: 0.5, ID: 2},
{Price: 6002, Amount: 2, ID: 3},
{Price: 6003, Amount: 3, ID: 4},
{Price: 6004, Amount: 5, ID: 5},
{Price: 6005, Amount: 2, ID: 6},
{Price: 6006, Amount: 1.5, ID: 7},
{Price: 6007, Amount: 0.5, ID: 8},
{Price: 6008, Amount: 23, ID: 9},
{Price: 6009, Amount: 9, ID: 10},
{Price: 6010, Amount: 7, ID: 11},
}
bids := []orderbook.Item{
{Price: 5999, Amount: 1, ID: 12},
{Price: 5998, Amount: 0.5, ID: 13},
{Price: 5997, Amount: 2, ID: 14},
{Price: 5996, Amount: 3, ID: 15},
{Price: 5995, Amount: 5, ID: 16},
{Price: 5994, Amount: 2, ID: 17},
{Price: 5993, Amount: 1.5, ID: 18},
{Price: 5992, Amount: 0.5, ID: 19},
{Price: 5991, Amount: 23, ID: 20},
{Price: 5990, Amount: 9, ID: 21},
{Price: 5989, Amount: 7, ID: 22},
}
snapShot1.Asks = asks
snapShot1.Bids = bids
snapShot1.AssetType = "SPOT"
snapShot1.CurrencyPair = "BTCUSD"
snapShot1.LastUpdated = time.Now()
snapShot1.Pair = pair.NewCurrencyPairFromString("BTCUSD")
wsTest.Websocket.Orderbook.LoadSnapshot(snapShot1, "ExchangeTest", false)
var snapShot2 orderbook.Base
asks = []orderbook.Item{
{Price: 51, Amount: 1, ID: 1},
{Price: 52, Amount: 0.5, ID: 2},
{Price: 53, Amount: 2, ID: 3},
{Price: 54, Amount: 3, ID: 4},
{Price: 55, Amount: 5, ID: 5},
{Price: 56, Amount: 2, ID: 6},
{Price: 57, Amount: 1.5, ID: 7},
{Price: 58, Amount: 0.5, ID: 8},
{Price: 59, Amount: 23, ID: 9},
{Price: 50, Amount: 9, ID: 10},
{Price: 60, Amount: 7, ID: 11},
}
bids = []orderbook.Item{
{Price: 49, Amount: 1, ID: 12},
{Price: 48, Amount: 0.5, ID: 13},
{Price: 47, Amount: 2, ID: 14},
{Price: 46, Amount: 3, ID: 15},
{Price: 45, Amount: 5, ID: 16},
{Price: 44, Amount: 2, ID: 17},
{Price: 43, Amount: 1.5, ID: 18},
{Price: 42, Amount: 0.5, ID: 19},
{Price: 41, Amount: 23, ID: 20},
{Price: 40, Amount: 9, ID: 21},
{Price: 39, Amount: 7, ID: 22},
}
snapShot2.Asks = asks
snapShot2.Bids = bids
snapShot2.AssetType = "SPOT"
snapShot2.CurrencyPair = "LTCUSD"
snapShot2.LastUpdated = time.Now()
snapShot2.Pair = pair.NewCurrencyPairFromString("LTCUSD")
wsTest.Websocket.Orderbook.LoadSnapshot(snapShot2, "ExchangeTest", false)
var snapShot3 orderbook.Base
asks = []orderbook.Item{
{Price: 51, Amount: 1, ID: 1},
{Price: 52, Amount: 0.5, ID: 2},
{Price: 53, Amount: 2, ID: 3},
{Price: 54, Amount: 3, ID: 4},
{Price: 55, Amount: 5, ID: 5},
{Price: 56, Amount: 2, ID: 6},
{Price: 57, Amount: 1.5, ID: 7},
{Price: 58, Amount: 0.5, ID: 8},
{Price: 59, Amount: 23, ID: 9},
{Price: 50, Amount: 9, ID: 10},
{Price: 60, Amount: 7, ID: 11},
}
bids = []orderbook.Item{
{Price: 49, Amount: 1, ID: 12},
{Price: 48, Amount: 0.5, ID: 13},
{Price: 47, Amount: 2, ID: 14},
{Price: 46, Amount: 3, ID: 15},
{Price: 45, Amount: 5, ID: 16},
{Price: 44, Amount: 2, ID: 17},
{Price: 43, Amount: 1.5, ID: 18},
{Price: 42, Amount: 0.5, ID: 19},
{Price: 41, Amount: 23, ID: 20},
{Price: 40, Amount: 9, ID: 21},
{Price: 39, Amount: 7, ID: 22},
}
snapShot3.Asks = asks
snapShot3.Bids = bids
snapShot3.AssetType = "FUTURES"
snapShot3.CurrencyPair = "LTCUSD"
snapShot3.LastUpdated = time.Now()
snapShot3.Pair = pair.NewCurrencyPairFromString("LTCUSD")
wsTest.Websocket.Orderbook.LoadSnapshot(snapShot3, "ExchangeTest", false)
if len(wsTest.Websocket.Orderbook.ob) != 3 {
t.Error("test failed - inserting orderbook data")
}
}
func TestUpdate(t *testing.T) {
LTCUSDPAIR := pair.NewCurrencyPairFromString("LTCUSD")
BTCUSDPAIR := pair.NewCurrencyPairFromString("BTCUSD")
bidTargets := []orderbook.Item{
{Price: 49, Amount: 24}, // Amend
{Price: 48, Amount: 0}, // Delete
{Price: 1337, Amount: 100}, // Append
{Price: 1336, Amount: 0}, // Ghost delete
}
askTargets := []orderbook.Item{
{Price: 51, Amount: 24}, // Amend
{Price: 52, Amount: 0}, // Delete
{Price: 1337, Amount: 100}, // Append
{Price: 1336, Amount: 0}, // Ghost delete
}
err := wsTest.Websocket.Orderbook.Update(bidTargets,
askTargets,
LTCUSDPAIR,
time.Now(),
"ExchangeTest",
"SPOT")
if err != nil {
t.Error("test failed - OrderbookUpdate error", err)
}
err = wsTest.Websocket.Orderbook.Update(bidTargets,
askTargets,
LTCUSDPAIR,
time.Now(),
"ExchangeTest",
"FUTURES")
if err != nil {
t.Error("test failed - OrderbookUpdate error", err)
}
bidTargets = []orderbook.Item{
{Price: 5999, Amount: 24}, // Amend
{Price: 5998, Amount: 0}, // Delete
{Price: 1337, Amount: 100}, // Append
{Price: 1336, Amount: 0}, // Ghost delete
}
askTargets = []orderbook.Item{
{Price: 6000, Amount: 24}, // Amend
{Price: 6001, Amount: 0}, // Delete
{Price: 1337, Amount: 100}, // Append
{Price: 1336, Amount: 0}, // Ghost delete
}
err = wsTest.Websocket.Orderbook.Update(bidTargets,
askTargets,
BTCUSDPAIR,
time.Now(),
"ExchangeTest",
"SPOT")
if err != nil {
t.Error("test failed - OrderbookUpdate error", err)
}
}
func TestFunctionality(t *testing.T) {
var w Websocket
if w.FormatFunctionality() != NoWebsocketSupportText {
t.Fatalf("Test Failed - FormatFunctionality error expected %s but received %s",
NoWebsocketSupportText, w.FormatFunctionality())
}
w.Functionality = 1 << 31
if w.FormatFunctionality() != UnknownWebsocketFunctionality+"[1<<31]" {
t.Fatal("Test Failed - GetFunctionality error incorrect error returned")
}
w.Functionality = WebsocketOrderbookSupported
if w.GetFunctionality() != WebsocketOrderbookSupported {
t.Fatal("Test Failed - GetFunctionality error incorrect bitmask returned")
}
if !w.SupportsFunctionality(WebsocketOrderbookSupported) {
t.Fatal("Test Failed - SupportsFunctionality error should be true")
}
}