Files
gocryptotrader/exchanges/orderbook/orderbook_test.go
Scott 11a68a9bb7 Utilising authenticated websocket functions in exchange wrappers (#384)
* Basic concept commit

* Initial changes to support bitfinex v2. Reverts linter changes as they suck. Exports bitfinex ws types

* Adds ticker, trade and orderbook support

* Candles sub that returns no data COMPLETE

* Adds authenticated ws support

* Adds the barebones endpoints to support

* Adds more endpoints

* Even more endpoints

* minicommit to switch and test

* All the interactive types

* Adds support for simultaneous connections. Updates tests. Nothing is working

* Successfully adds place order. Moves all authenticated endpoints to new switch case

* Cancel order and modify order

* Cancel all orders, cancel multi orders

* Finalises implementation. Uses testMain

* Adds WS wrapper support for some funcs

* Fixing rebasing issues

* Replaces use of currency as a variable. Updates a lot of coinut websocket auth endpoint stuff

* Fixes some fun for loops with GetEnabledPairs

* Fixes tests impacted by currency var change

* Adds coinut support for WS functions. Replaces `order` vars with `ord`. Fixes some for loops too. Removes verbose from bitfinex

* So many panics

* I'm fixing a hole, where the panics get in, and stops my mind from wandering, where it will go

* Moves func `CanUseAuthenticatedWebsocketEndpoint` to Websocket package as it fits better. Adds test coverage of `CanUseAuthenticatedWebsocketEndpoint`

* Finishes up all of coinuts ws implementations.

* GateIO implementation

* Adds some helper funcs for types, sides and status. Adds support for huobi. Removes unnecessary type

* Adds forgotten huobi endpoint

* Fixes cancel order endpoint

* go hates my formatting and so do I

* The process to get authenticated kraken websocket to work. Uses testmain. Adds new auth channel, auth subscriptions, auth data handling. Not working yet

* Finishes open orders handling

* Mini update for status only updates

* Fixes some kraken points

* Finishes WS kraken since it doesn't work

* Unfinished commit, cleaning up types

* Finishes the const replacing

* Fixes extra GetNAmes after rebase

* An end to the cleanup. testmain for gateio

* Adds ZB support

* Bitfinex cleanup. Renamed func

* Testmain-47s for everyone!!! yayaaaaaaa

* Adds kraken websocket wrapper support

* Fixes rebase issues

* Fixes tests from rebase

* Adds test for conversion. Fixes for loop. Updates test order pricing. Fixes some poor made tests. Adds proper error handling for ws responses instead of logging them. Fixed issue where commented code ruined kraken ws.

* Fixes secret linting issues. Prioritises bitfinex channelID responses over authorised

* Fixes sloppy error/var declarations

* Fixes crazy bad logic where submit order errors weren't really considered. Parralols alphapoint/alphapoint_test.go. Removes buffer for multi-websocket comms channel.

* Removal of inline string and removal of redundant nil checkerinos

* Fixes err checks. Checks whether float has decimal. Fixes append. Drops omitempties. Parallel to some tests. Moves var declarations

* Replaces my lazy sprintfs with strconv.FormatInt(time.Now().Unix(), 10)

* Adds shiny new FullyMatched bool. Fixes coinbene buy sell consts

* Fixes oopsie with coinbene const replacement

* Fixes currency issue

* Cleans up new places that use JSONDecode

* Fixes huge panic bug from string int conversion. Adds large testtable for strings to order types

* Fixes some more strconversion issues. Fixes table test var usage. Changes mapperino name

* Added some new scenarios for number splitting

* Fixes lint issues

* negative num fix

* Typo fix

* Accuracy warning comment
2019-12-04 14:16:23 +11:00

506 lines
11 KiB
Go

package orderbook
import (
"log"
"math/rand"
"os"
"strconv"
"sync"
"testing"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/dispatch"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
func TestMain(m *testing.M) {
err := dispatch.Start(1, dispatch.DefaultJobsLimit)
if err != nil {
log.Fatal(err)
}
cpyMux = service.mux
os.Exit(m.Run())
}
var cpyMux *dispatch.Mux
func TestSubscribeOrderbook(t *testing.T) {
_, err := SubscribeOrderbook("", currency.Pair{}, asset.Item(""))
if err == nil {
t.Error("error cannot be nil")
}
p := currency.NewPair(currency.BTC, currency.USD)
b := Base{
Pair: p,
AssetType: asset.Spot,
}
err = b.Process()
if err == nil {
t.Error("error cannot be nil")
}
b.ExchangeName = "SubscribeOBTest"
err = b.Process()
if err == nil {
t.Error("error cannot be nil")
}
b.Bids = []Item{{}}
err = b.Process()
if err != nil {
t.Error("process error", err)
}
_, err = SubscribeOrderbook("SubscribeOBTest", p, asset.Spot)
if err != nil {
t.Error("error cannot be nil")
}
// process redundant update
err = b.Process()
if err != nil {
t.Error("process error", err)
}
}
func TestUpdateBooks(t *testing.T) {
p := currency.NewPair(currency.BTC, currency.USD)
b := Base{
Pair: p,
AssetType: asset.Spot,
ExchangeName: "UpdateTest",
}
service.mux = nil
err := service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
b.Pair.Base = currency.CYC
err = service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
b.Pair.Quote = currency.ENAU
err = service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
b.AssetType = "unicorns"
err = service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
service.mux = cpyMux
}
func TestSubscribeToExchangeOrderbooks(t *testing.T) {
_, err := SubscribeToExchangeOrderbooks("")
if err == nil {
t.Error("error cannot be nil")
}
p := currency.NewPair(currency.BTC, currency.USD)
b := Base{
Pair: p,
AssetType: asset.Spot,
ExchangeName: "SubscribeToExchangeOrderbooks",
Bids: []Item{{}},
}
err = b.Process()
if err != nil {
t.Error("", err)
}
_, err = SubscribeToExchangeOrderbooks("SubscribeToExchangeOrderbooks")
if err != nil {
t.Error(err)
}
}
func TestVerify(t *testing.T) {
t.Parallel()
b := Base{
ExchangeName: "TestExchange",
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: []Item{
{Price: 100}, {Price: 101}, {Price: 99},
},
Asks: []Item{
{Price: 100}, {Price: 99}, {Price: 101},
},
}
b.Verify()
if r := b.Bids[1].Price; r != 100 {
t.Error("unexpected result")
}
if r := b.Asks[1].Price; r != 100 {
t.Error("unexpected result")
}
}
func TestCalculateTotalBids(t *testing.T) {
t.Parallel()
curr := currency.NewPairFromStrings("BTC", "USD")
base := Base{
Pair: curr,
Bids: []Item{{Price: 100, Amount: 10}},
LastUpdated: time.Now(),
}
a, b := base.TotalBidsAmount()
if a != 10 && b != 1000 {
t.Fatal("TestCalculateTotalBids expected a = 10 and b = 1000")
}
}
func TestCalculateTotaAsks(t *testing.T) {
t.Parallel()
curr := currency.NewPairFromStrings("BTC", "USD")
base := Base{
Pair: curr,
Asks: []Item{{Price: 100, Amount: 10}},
}
a, b := base.TotalAsksAmount()
if a != 10 && b != 1000 {
t.Fatal("TestCalculateTotalAsks expected a = 10 and b = 1000")
}
}
func TestUpdate(t *testing.T) {
t.Parallel()
curr := currency.NewPairFromStrings("BTC", "USD")
timeNow := time.Now()
base := Base{
Pair: curr,
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
LastUpdated: timeNow,
}
asks := []Item{{Price: 200, Amount: 101}}
bids := []Item{{Price: 201, Amount: 100}}
time.Sleep(time.Millisecond * 50)
base.Update(bids, asks)
if !base.LastUpdated.After(timeNow) {
t.Fatal("TestUpdate expected LastUpdated to be greater then original time")
}
a, b := base.TotalAsksAmount()
if a != 100 && b != 20200 {
t.Fatal("TestUpdate expected a = 100 and b = 20100")
}
a, b = base.TotalBidsAmount()
if a != 100 && b != 20100 {
t.Fatal("TestUpdate expected a = 100 and b = 20100")
}
}
func TestGetOrderbook(t *testing.T) {
c := currency.NewPairFromStrings("BTC", "USD")
base := &Base{
Pair: c,
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
ExchangeName: "Exchange",
AssetType: asset.Spot,
}
err := base.Process()
if err != nil {
t.Fatal(err)
}
result, err := Get("Exchange", c, asset.Spot)
if err != nil {
t.Fatalf("TestGetOrderbook failed to get orderbook. Error %s",
err)
}
if !result.Pair.Equal(c) {
t.Fatal("TestGetOrderbook failed. Mismatched pairs")
}
_, err = Get("nonexistent", c, asset.Spot)
if err == nil {
t.Fatal("TestGetOrderbook retrieved non-existent orderbook")
}
c.Base = currency.NewCode("blah")
_, err = Get("Exchange", c, asset.Spot)
if err == nil {
t.Fatal("TestGetOrderbook retrieved non-existent orderbook using invalid first currency")
}
newCurrency := currency.NewPairFromStrings("BTC", "AUD")
_, err = Get("Exchange", newCurrency, asset.Spot)
if err == nil {
t.Fatal("TestGetOrderbook retrieved non-existent orderbook using invalid second currency")
}
base.Pair = newCurrency
err = base.Process()
if err != nil {
t.Error(err)
}
_, err = Get("Exchange", newCurrency, "meowCats")
if err == nil {
t.Error("error cannot be nil")
}
}
func TestCreateNewOrderbook(t *testing.T) {
c := currency.NewPairFromStrings("BTC", "USD")
base := &Base{
Pair: c,
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
ExchangeName: "testCreateNewOrderbook",
AssetType: asset.Spot,
}
err := base.Process()
if err != nil {
t.Fatal(err)
}
result, err := Get("testCreateNewOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestCreateNewOrderbook failed to create new orderbook", err)
}
if !result.Pair.Equal(c) {
t.Fatal("TestCreateNewOrderbook result pair is incorrect")
}
a, b := result.TotalAsksAmount()
if a != 10 && b != 1000 {
t.Fatal("TestCreateNewOrderbook CalculateTotalAsks value is incorrect")
}
a, b = result.TotalBidsAmount()
if a != 10 && b != 2000 {
t.Fatal("TestCreateNewOrderbook CalculateTotalBids value is incorrect")
}
}
func TestProcessOrderbook(t *testing.T) {
c := currency.NewPairFromStrings("BTC", "USD")
base := Base{
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
ExchangeName: "ProcessOrderbook",
}
// test for empty pair
base.Pair = currency.Pair{}
err := base.Process()
if err == nil {
t.Error("empty pair should throw an err")
}
// test for empty asset type
base.Pair = c
err = base.Process()
if err == nil {
t.Error("empty asset type should throw an err")
}
// now process a valid orderbook
base.AssetType = asset.Spot
err = base.Process()
if err != nil {
t.Error("unexpcted result: ", err)
}
result, err := Get("ProcessOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestProcessOrderbook failed to create new orderbook")
}
if !result.Pair.Equal(c) {
t.Fatal("TestProcessOrderbook result pair is incorrect")
}
// now test for processing a pair with a different quote currency
c = currency.NewPairFromStrings("BTC", "GBP")
base.Pair = c
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
result, err = Get("ProcessOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
if !result.Pair.Equal(c) {
t.Fatal("TestProcessOrderbook result pair is incorrect")
}
// now test for processing a pair which has a different base currency
c = currency.NewPairFromStrings("LTC", "GBP")
base.Pair = c
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
result, err = Get("ProcessOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
if !result.Pair.Equal(c) {
t.Fatal("TestProcessOrderbook result pair is incorrect")
}
base.Asks = []Item{{Price: 200, Amount: 200}}
base.AssetType = "monthly"
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
result, err = Get("ProcessOrderbook", c, "monthly")
if err != nil {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
a, b := result.TotalAsksAmount()
if a != 200 && b != 40000 {
t.Fatal("TestProcessOrderbook CalculateTotalsAsks incorrect values")
}
base.Bids = []Item{{Price: 420, Amount: 200}}
base.ExchangeName = "Blah"
base.AssetType = "quarterly"
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
result, err = Get("Blah", c, "quarterly")
if err != nil {
t.Fatal("TestProcessOrderbook failed to create new orderbook")
}
if a != 200 && b != 84000 {
t.Fatal("TestProcessOrderbook CalculateTotalsBids incorrect values")
}
type quick struct {
Name string
P currency.Pair
Bids []Item
Asks []Item
}
var testArray []quick
_ = rand.NewSource(time.Now().Unix())
var wg sync.WaitGroup
var m sync.Mutex
var catastrophicFailure bool
for i := 0; i < 500; i++ {
if catastrophicFailure {
break
}
wg.Add(1)
go func() {
newName := "Exchange" + strconv.FormatInt(rand.Int63(), 10)
newPairs := currency.NewPair(currency.NewCode("BTC"+strconv.FormatInt(rand.Int63(), 10)),
currency.NewCode("USD"+strconv.FormatInt(rand.Int63(), 10)))
asks := []Item{{Price: rand.Float64(), Amount: rand.Float64()}}
bids := []Item{{Price: rand.Float64(), Amount: rand.Float64()}}
base := &Base{
Pair: newPairs,
Asks: asks,
Bids: bids,
ExchangeName: newName,
AssetType: asset.Spot,
}
m.Lock()
err = base.Process()
if err != nil {
t.Error(err)
catastrophicFailure = true
return
}
testArray = append(testArray, quick{Name: newName, P: newPairs, Bids: bids, Asks: asks})
m.Unlock()
wg.Done()
}()
}
if catastrophicFailure {
t.Fatal("Process() error", err)
}
wg.Wait()
for _, test := range testArray {
wg.Add(1)
fatalErr := false
go func(test quick) {
result, err := Get(test.Name, test.P, asset.Spot)
if err != nil {
fatalErr = true
return
}
if result.Asks[0] != test.Asks[0] {
t.Error("TestProcessOrderbook failed bad values")
}
if result.Bids[0] != test.Bids[0] {
t.Error("TestProcessOrderbook failed bad values")
}
wg.Done()
}(test)
if fatalErr {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
}
wg.Wait()
}
func TestSetNewData(t *testing.T) {
err := service.SetNewData(nil)
if err == nil {
t.Error("error cannot be nil")
}
}
func TestGetAssociations(t *testing.T) {
_, err := service.GetAssociations(nil)
if err == nil {
t.Error("error cannot be nil")
}
}