mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Exchanges: Initial implementation after rebase of depth (WIP) * orderbook/buffer: convert and couple orderbook interaction functionality from buffer to orderbook linked list - Use single point reference for orderbook depth * buffer/orderbook: conversion continued (WIP) * exchange: buffer/linkedlist handover (WIP) * Added some tests for yesterday * linkedList: added more testing and trying to figure out broken things * Started tying everything in * continuous integration and testing * orderbook: expanded tests * go mod tidy * Add in different synchornisation levels for protocols Add in timer for the streaming system to reduce updates to datahandler Add in more test code as I integrate more exchanges * Depth: Add tests, add length check to call linked list updating, add in constructor. Linked List: Improve tests, add in checks for zero liquidity on books. Node: Added in cleaner POC, add in contructor. Buffer: Fixed tests, checked benchmarks. * orderbook: reinstate dispatch calls * Addr glorious & madcozbad nits * fix functionality and add tests * Address linterinos * remove label * expanded comment * fix races and and bitmex test * reinstate go routine for alerting changes * rm line :D * fix more tests * Addr glorious nits * rm glorious field * depth: defer unlock to stop deadlock * orderbook: remove unused vars * buffer: fix test to what it should be * nits: madcosbad addr * nits: glorious nits * linkedlist: remove unused params * orderbook: shift time call to outside of push to inline, add in case for update inster price for zero liquidity, nits * orderbook: nits addressed * engine: change stream -> websocket convention and remove unused function * nits: glorious nits * Websocket Buffer: Add verbosity switch * linked list: Add comment * linked list: fix spelling * nits: glorious nits * orderbook: Adds in test and explicit time type with constructor, fix nits * linter * spelling: removed the dere fence * depth: Update alerting mechanism to a more battle tested state * depth: spelling * nits: glorious nits * linked list: match cases * buffer: fix linter issue * golangci: increase timeout by 30 seconds * nodes: update atomic checks * spelling: fix * node: add in commentary * exchanges/syncer: add function to switch over to REST when websocket functionality is not available for a specific asset type * linter: exchange linter issues * syncer: Add in warning * nits: glorious nits * AssetWebsocketSupport: unexport map * Nits: Adrr * rm letter * exchanges: Orderbook verification change for naming, deprecate checksum bypass as it has the potential to obfuscate errors that are at the tail end of the book, add in verification for websocket stream updates * general: fix spelling remove breakpoint * nits: fix more glorious nits until more are found * orderbook: fix tests * orderbook: fix wait tests and add in more checks * nits: addr * orderbook: remove dispatch reference * linkedlist: consolidate bid/ask functions * linked lisdt: remove words * fix spelling
337 lines
7.8 KiB
Go
337 lines
7.8 KiB
Go
package engine
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/config"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
|
)
|
|
|
|
const (
|
|
testExchange = "Bitstamp"
|
|
)
|
|
|
|
func addValidEvent() (int64, error) {
|
|
return Add(testExchange,
|
|
ItemPrice,
|
|
EventConditionParams{Condition: ConditionGreaterThan, Price: 1},
|
|
currency.NewPair(currency.BTC, currency.USD),
|
|
asset.Spot,
|
|
"SMS,test")
|
|
}
|
|
|
|
func TestAdd(t *testing.T) {
|
|
bot := CreateTestBot(t)
|
|
if config.Cfg.Name == "" && bot != nil {
|
|
config.Cfg = *bot.Config
|
|
}
|
|
_, err := Add("", "", EventConditionParams{}, currency.Pair{}, "", "")
|
|
if err == nil {
|
|
t.Error("should err on invalid params")
|
|
}
|
|
|
|
_, err = addValidEvent()
|
|
if err != nil {
|
|
t.Error("unexpected result", err)
|
|
}
|
|
|
|
_, err = addValidEvent()
|
|
if err != nil {
|
|
t.Error("unexpected result", err)
|
|
}
|
|
|
|
if len(Events) != 2 {
|
|
t.Error("2 events should be stored")
|
|
}
|
|
}
|
|
|
|
func TestRemove(t *testing.T) {
|
|
bot := CreateTestBot(t)
|
|
if config.Cfg.Name == "" && bot != nil {
|
|
config.Cfg = *bot.Config
|
|
}
|
|
id, err := addValidEvent()
|
|
if err != nil {
|
|
t.Error("unexpected result", err)
|
|
}
|
|
|
|
if s := Remove(id); !s {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
if s := Remove(id); s {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestGetEventCounter(t *testing.T) {
|
|
bot := CreateTestBot(t)
|
|
if config.Cfg.Name == "" && bot != nil {
|
|
config.Cfg = *bot.Config
|
|
}
|
|
_, err := addValidEvent()
|
|
if err != nil {
|
|
t.Error("unexpected result", err)
|
|
}
|
|
|
|
n, e := GetEventCounter()
|
|
if n == 0 || e > 0 {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
Events[0].Executed = true
|
|
n, e = GetEventCounter()
|
|
if n == 0 || e == 0 {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestExecuteAction(t *testing.T) {
|
|
t.Parallel()
|
|
bot := CreateTestBot(t)
|
|
if Bot == nil {
|
|
Bot = bot
|
|
}
|
|
if config.Cfg.Name == "" && bot != nil {
|
|
config.Cfg = *bot.Config
|
|
}
|
|
|
|
var e Event
|
|
if r := e.ExecuteAction(); !r {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
e.Action = "SMS,test"
|
|
if r := e.ExecuteAction(); !r {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
e.Action = "SMS,ALL"
|
|
if r := e.ExecuteAction(); !r {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestString(t *testing.T) {
|
|
t.Parallel()
|
|
e := Event{
|
|
Exchange: testExchange,
|
|
Item: ItemPrice,
|
|
Condition: EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
Price: 1,
|
|
},
|
|
Pair: currency.NewPair(currency.BTC, currency.USD),
|
|
Asset: asset.Spot,
|
|
Action: "SMS,ALL",
|
|
}
|
|
|
|
if r := e.String(); r != "If the BTCUSD [SPOT] PRICE on Bitstamp meets the following {> 1 false false 0} then SMS,ALL." {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestProcessTicker(t *testing.T) {
|
|
e := Event{
|
|
Exchange: testExchange,
|
|
Pair: currency.NewPair(currency.BTC, currency.USD),
|
|
Asset: asset.Spot,
|
|
Condition: EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
Price: 1,
|
|
},
|
|
}
|
|
|
|
// now populate it with a 0 entry
|
|
tick := ticker.Price{
|
|
Pair: currency.NewPair(currency.BTC, currency.USD),
|
|
ExchangeName: e.Exchange,
|
|
AssetType: e.Asset,
|
|
}
|
|
if err := ticker.ProcessTicker(&tick); err != nil {
|
|
t.Fatal("unexpected result:", err)
|
|
}
|
|
if r := e.processTicker(false); r {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
// now populate it with a number > 0
|
|
tick.Last = 1337
|
|
if err := ticker.ProcessTicker(&tick); err != nil {
|
|
t.Fatal("unexpected result:", err)
|
|
}
|
|
if r := e.processTicker(false); !r {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestProcessCondition(t *testing.T) {
|
|
t.Parallel()
|
|
var e Event
|
|
tester := []struct {
|
|
Condition string
|
|
Actual float64
|
|
Threshold float64
|
|
ExpectedResult bool
|
|
}{
|
|
{ConditionGreaterThan, 1, 2, false},
|
|
{ConditionGreaterThan, 2, 1, true},
|
|
{ConditionGreaterThanOrEqual, 1, 2, false},
|
|
{ConditionGreaterThanOrEqual, 2, 1, true},
|
|
{ConditionIsEqual, 1, 1, true},
|
|
{ConditionIsEqual, 1, 2, false},
|
|
{ConditionLessThan, 1, 2, true},
|
|
{ConditionLessThan, 2, 1, false},
|
|
{ConditionLessThanOrEqual, 1, 2, true},
|
|
{ConditionLessThanOrEqual, 2, 1, false},
|
|
}
|
|
for x := range tester {
|
|
e.Condition.Condition = tester[x].Condition
|
|
if r := e.processCondition(tester[x].Actual, tester[x].Threshold); r != tester[x].ExpectedResult {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessOrderbook(t *testing.T) {
|
|
e := Event{
|
|
Exchange: testExchange,
|
|
Pair: currency.NewPair(currency.BTC, currency.USD),
|
|
Asset: asset.Spot,
|
|
Condition: EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
CheckBidsAndAsks: true,
|
|
OrderbookAmount: 100,
|
|
},
|
|
}
|
|
|
|
// now populate it with a 0 entry
|
|
o := orderbook.Base{
|
|
Pair: currency.NewPair(currency.BTC, currency.USD),
|
|
Bids: []orderbook.Item{{Amount: 24, Price: 23}},
|
|
Asks: []orderbook.Item{{Amount: 24, Price: 23}},
|
|
Exchange: e.Exchange,
|
|
Asset: e.Asset,
|
|
}
|
|
if err := o.Process(); err != nil {
|
|
t.Fatal("unexpected result:", err)
|
|
}
|
|
|
|
if r := e.processOrderbook(false); !r {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestCheckEventCondition(t *testing.T) {
|
|
t.Parallel()
|
|
if Bot == nil {
|
|
Bot = new(Engine)
|
|
}
|
|
|
|
e := Event{
|
|
Item: ItemPrice,
|
|
}
|
|
if r := e.CheckEventCondition(false); r {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
e.Item = ItemOrderbook
|
|
if r := e.CheckEventCondition(false); r {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestIsValidEvent(t *testing.T) {
|
|
bot := CreateTestBot(t)
|
|
if config.Cfg.Name == "" && bot != nil {
|
|
config.Cfg = *bot.Config
|
|
}
|
|
// invalid exchange name
|
|
if err := IsValidEvent("meow", "", EventConditionParams{}, ""); err != errExchangeDisabled {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
|
|
// invalid item
|
|
if err := IsValidEvent(testExchange, "", EventConditionParams{}, ""); err != errInvalidItem {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
|
|
// invalid condition
|
|
if err := IsValidEvent(testExchange, ItemPrice, EventConditionParams{}, ""); err != errInvalidCondition {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
|
|
// valid condition but empty price which will still throw an errInvalidCondition
|
|
c := EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
}
|
|
if err := IsValidEvent(testExchange, ItemPrice, c, ""); err != errInvalidCondition {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
|
|
// valid condition but empty orderbook amount will still still throw an errInvalidCondition
|
|
if err := IsValidEvent(testExchange, ItemOrderbook, c, ""); err != errInvalidCondition {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
|
|
// test action splitting, but invalid
|
|
c.OrderbookAmount = 1337
|
|
if err := IsValidEvent(testExchange, ItemOrderbook, c, "a,meow"); err != errInvalidAction {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
|
|
// check for invalid action without splitting
|
|
if err := IsValidEvent(testExchange, ItemOrderbook, c, "hi"); err != errInvalidAction {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
|
|
// valid event
|
|
if err := IsValidEvent(testExchange, ItemOrderbook, c, "SMS,test"); err != nil {
|
|
t.Error("unexpected result:", err)
|
|
}
|
|
}
|
|
|
|
func TestIsValidExchange(t *testing.T) {
|
|
t.Parallel()
|
|
if s := IsValidExchange("invalidexchangerino"); s {
|
|
t.Error("unexpected result")
|
|
}
|
|
CreateTestBot(t)
|
|
if s := IsValidExchange(testExchange); !s {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestIsValidCondition(t *testing.T) {
|
|
t.Parallel()
|
|
if s := IsValidCondition("invalidconditionerino"); s {
|
|
t.Error("unexpected result")
|
|
}
|
|
if s := IsValidCondition(ConditionGreaterThan); !s {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestIsValidAction(t *testing.T) {
|
|
t.Parallel()
|
|
if s := IsValidAction("invalidactionerino"); s {
|
|
t.Error("unexpected result")
|
|
}
|
|
if s := IsValidAction(ActionSMSNotify); !s {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestIsValidItem(t *testing.T) {
|
|
t.Parallel()
|
|
if s := IsValidItem("invaliditemerino"); s {
|
|
t.Error("unexpected result")
|
|
}
|
|
if s := IsValidItem(ItemPrice); !s {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|