mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-22 23:16:48 +00:00
* Added dispatch service * Added orderbook streaming capabilities * Assigned correct orderbook.base exchange name * Fixed Requested niterinos Add in cli orderbook QA tool to gctcli Add exchange orderbook streaming * Add ticker streaming support through dispatch package * Added in some more info on error returns for orderbook.go * fix linter issues * Fix some issues * Update * Fix requested * move dispatch out of exchanges folder to its own independant folder * Fix requested * change orderbook string to tickers * Limit orderbooks to 50 and made dispatch system more stateless in operation * lower cases for update/retrieve/sub exchange name * Adds in asset validation and lower case conversion on cli * Remove comment * Moved timer to a higher scope so its not constantly initialised just reset per instance and removed returning unused channel on error * Rm unused release function in dispatch.go Reset timer and bleed buffered timer chan if needed in dispatch.go Added in ticker.Stop() and timer.Stop() functions for worker routine return in dispatch.go Index aggregated bid and ask functions for orderbook.go Added in dummy slice for wsorderbook_test.go * Moved drain to above Reset so potential race would not occur in dispatch.go Fix various linter issues dispatch.go * Fix some issues * change to start/stop service, added in service state change via cli, updated logger * fix requested * Add worker amount init spawning * fix linter issues * Fix more linter issues * More fixes * Fix race issue on releasing pipe channel on a close after shutting down dispatcher system * Moved all types to dispatch_types.go && remove panic * Moved types into serperate file && improve test coverage * RM unnecessary select case for draining channel && fixed error string * Added orderbook_types file and improved code coverage * gofmt file * reinstated select cases on drain because I am silly * Remove error for drop worker * Added more test cases * not checking error issue fix * remove func causing race in test, this has required protection via an exported function * set Gemini websocket orderbook exchange name
506 lines
11 KiB
Go
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)
|
|
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("test failed - 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("test failed - 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("test failed:", 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()
|
|
currency := currency.NewPairFromStrings("BTC", "USD")
|
|
base := Base{
|
|
Pair: currency,
|
|
Bids: []Item{{Price: 100, Amount: 10}},
|
|
LastUpdated: time.Now(),
|
|
}
|
|
|
|
a, b := base.TotalBidsAmount()
|
|
if a != 10 && b != 1000 {
|
|
t.Fatal("Test failed. TestCalculateTotalBids expected a = 10 and b = 1000")
|
|
}
|
|
}
|
|
|
|
func TestCalculateTotaAsks(t *testing.T) {
|
|
t.Parallel()
|
|
currency := currency.NewPairFromStrings("BTC", "USD")
|
|
base := Base{
|
|
Pair: currency,
|
|
Asks: []Item{{Price: 100, Amount: 10}},
|
|
}
|
|
|
|
a, b := base.TotalAsksAmount()
|
|
if a != 10 && b != 1000 {
|
|
t.Fatal("Test failed. TestCalculateTotalAsks expected a = 10 and b = 1000")
|
|
}
|
|
}
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
currency := currency.NewPairFromStrings("BTC", "USD")
|
|
timeNow := time.Now()
|
|
base := Base{
|
|
Pair: currency,
|
|
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("test failed. TestUpdate expected LastUpdated to be greater then original time")
|
|
}
|
|
|
|
a, b := base.TotalAsksAmount()
|
|
if a != 100 && b != 20200 {
|
|
t.Fatal("Test failed. TestUpdate expected a = 100 and b = 20100")
|
|
}
|
|
|
|
a, b = base.TotalBidsAmount()
|
|
if a != 100 && b != 20100 {
|
|
t.Fatal("Test failed. 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("Test failed. TestGetOrderbook failed to get orderbook. Error %s",
|
|
err)
|
|
}
|
|
if !result.Pair.Equal(c) {
|
|
t.Fatal("Test failed. TestGetOrderbook failed. Mismatched pairs")
|
|
}
|
|
|
|
_, err = Get("nonexistent", c, asset.Spot)
|
|
if err == nil {
|
|
t.Fatal("Test failed. TestGetOrderbook retrieved non-existent orderbook")
|
|
}
|
|
|
|
c.Base = currency.NewCode("blah")
|
|
_, err = Get("Exchange", c, asset.Spot)
|
|
if err == nil {
|
|
t.Fatal("Test failed. 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("Test failed. 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("Test failed. TestCreateNewOrderbook failed to create new orderbook", err)
|
|
}
|
|
|
|
if !result.Pair.Equal(c) {
|
|
t.Fatal("Test failed. TestCreateNewOrderbook result pair is incorrect")
|
|
}
|
|
|
|
a, b := result.TotalAsksAmount()
|
|
if a != 10 && b != 1000 {
|
|
t.Fatal("Test failed. TestCreateNewOrderbook CalculateTotalAsks value is incorrect")
|
|
}
|
|
|
|
a, b = result.TotalBidsAmount()
|
|
if a != 10 && b != 2000 {
|
|
t.Fatal("Test failed. 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("Test failed. TestProcessOrderbook failed to create new orderbook")
|
|
}
|
|
if !result.Pair.Equal(c) {
|
|
t.Fatal("Test failed. 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("Test Failed - Process() error", err)
|
|
}
|
|
result, err = Get("ProcessOrderbook", c, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
|
}
|
|
if !result.Pair.Equal(c) {
|
|
t.Fatal("Test failed. 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("Test Failed - Process() error", err)
|
|
}
|
|
result, err = Get("ProcessOrderbook", c, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
|
}
|
|
if !result.Pair.Equal(c) {
|
|
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
|
|
}
|
|
|
|
base.Asks = []Item{{Price: 200, Amount: 200}}
|
|
base.AssetType = "monthly"
|
|
err = base.Process()
|
|
if err != nil {
|
|
t.Error("Test Failed - Process() error", err)
|
|
}
|
|
|
|
result, err = Get("ProcessOrderbook", c, "monthly")
|
|
if err != nil {
|
|
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
|
}
|
|
|
|
a, b := result.TotalAsksAmount()
|
|
if a != 200 && b != 40000 {
|
|
t.Fatal("Test failed. 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("Test Failed - Process() error", err)
|
|
}
|
|
|
|
result, err = Get("Blah", c, "quarterly")
|
|
if err != nil {
|
|
t.Fatal("Test failed. TestProcessOrderbook failed to create new orderbook")
|
|
}
|
|
|
|
if a != 200 && b != 84000 {
|
|
t.Fatal("Test failed. 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("Test Failed - 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("Test failed. TestProcessOrderbook failed bad values")
|
|
}
|
|
|
|
if result.Bids[0] != test.Bids[0] {
|
|
t.Error("Test failed. TestProcessOrderbook failed bad values")
|
|
}
|
|
|
|
wg.Done()
|
|
}(test)
|
|
|
|
if fatalErr {
|
|
t.Fatal("Test failed. 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 ")
|
|
}
|
|
}
|