mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* initial concept of a nice validation tester for exchanges * adds some datahandler design * expand testing * more tests and fixes * minor end of day fix for bithumb * fixes implementation issues * more test coverage and improvements, but not sure if i should continue * fix more wrapper implementations * adds error type, more fixes * changes signature, fixes implementations * fixes more wrapper implementations * one more bit * more cleanup * WOW things work? * lintle 1/1337 * mini bump * fixes all linting * neaten * GetOrderInfo+ asset pair fixes+improvements * adds new websocket test * expand ws testing * fix bug, expand tests, improve implementation * code coverage of a lot of new codes * fixes everything * reverts accidental changes * minor fixes from reviewing code * removes Bitfinex cancelBatchOrder implementation * fixes dumb baby typo for babies * mini nit fixes * so many nits to address * addresses all the nits * Titlecase * switcheroo * removes websocket testing for now * fix appveyor, minor test fix * fixes typo, re-kindles killed kode * skip binance wrapper tests when running CI * expired context, huobi okx fixes * kodespull * fix ordering * time fix because why not * fix exmo, others * hopefully this fixes all of my life's problems * last thing today * huobi, more like hypotrophy * golangci-lint, more like mypooroldknee-splint * fix huobi times by removing them * should fix okx currency issues * blocks the application * adds last little contingency for pairs * addresses most nits and new problems * lovely fixed before seeing why okx sucks * fixes issues with okx websocket * the classic receieieivaier * lintle * adds test and fixes existing tests * expands error handling messages during setup * fixes dumb okx bugs introduced * quick fix for lint and exmo * fixes nixes * fix exmo deposit issue * lint * fixes issue with extra asset runs missing * fix surprise race * all the lint and merge fixes * fixes surprise bugs in OKx * fixes issues with times and chains * fixing all the merge stuff * merge fix * rm logs and a panic potential * lovely lint lament * an easy demonstration of scenario, but not of initial purpose * put it in the bin * Revert "put it in the bin" This reverts commit 15c6490f713233d43f10957367fcbf18e3818bdd. * re-add after immediate error popup * fix mini poor test design * okx okay * merge fixes * fixes issues discovered in lovely test * I FORGOT TO COMMIT THIS * nit fixaroonaboo * forgoetten test fix * revert old okx asset intrument work * fixes * revert problems I didnt understand. update bybit * fix merge bugs * test cleanup * further improvements * reshuffle and lint * rm redundant CI_TEST by rm the CI_TEST field that is redundant * path fix * move to its own section, dont run on 32 bit + appveyor * lint * fix lbank * address nits * let it rip * fix failing test time range * niteroo boogaloo * mod tidy, use common.SimpleTimeFormat
345 lines
9.4 KiB
Go
345 lines
9.4 KiB
Go
package engine
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
)
|
|
|
|
func TestSetupEventManager(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := setupEventManager(nil, nil, 0, false)
|
|
if !errors.Is(err, errNilComManager) {
|
|
t.Errorf("error '%v', expected '%v'", err, errNilComManager)
|
|
}
|
|
|
|
_, err = setupEventManager(&CommunicationManager{}, nil, 0, false)
|
|
if !errors.Is(err, errNilExchangeManager) {
|
|
t.Errorf("error '%v', expected '%v'", err, errNilExchangeManager)
|
|
}
|
|
|
|
m, err := setupEventManager(&CommunicationManager{}, &ExchangeManager{}, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
if m == nil { //nolint:staticcheck,nolintlint // SA5011 Ignore the nil warnings
|
|
t.Fatal("expected manager")
|
|
}
|
|
if m.sleepDelay == 0 { //nolint:staticcheck,nolintlint // SA5011 Ignore the nil warnings
|
|
t.Error("expected default set")
|
|
}
|
|
}
|
|
|
|
func TestEventManagerStart(t *testing.T) {
|
|
m, err := setupEventManager(&CommunicationManager{}, &ExchangeManager{}, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
|
|
err = m.Start()
|
|
if !errors.Is(err, ErrSubSystemAlreadyStarted) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrSubSystemAlreadyStarted)
|
|
}
|
|
|
|
m = nil
|
|
err = m.Start()
|
|
if !errors.Is(err, ErrNilSubsystem) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrNilSubsystem)
|
|
}
|
|
}
|
|
|
|
func TestEventManagerIsRunning(t *testing.T) {
|
|
t.Parallel()
|
|
m, err := setupEventManager(&CommunicationManager{}, &ExchangeManager{}, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
if !m.IsRunning() {
|
|
t.Error("expected true")
|
|
}
|
|
atomic.StoreInt32(&m.started, 0)
|
|
if m.IsRunning() {
|
|
t.Error("expected false")
|
|
}
|
|
m = nil
|
|
if m.IsRunning() {
|
|
t.Error("expected false")
|
|
}
|
|
}
|
|
|
|
func TestEventManagerStop(t *testing.T) {
|
|
t.Parallel()
|
|
m, err := setupEventManager(&CommunicationManager{}, &ExchangeManager{}, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
err = m.Stop()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
err = m.Stop()
|
|
if !errors.Is(err, ErrSubSystemNotStarted) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrSubSystemNotStarted)
|
|
}
|
|
m = nil
|
|
err = m.Stop()
|
|
if !errors.Is(err, ErrNilSubsystem) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrNilSubsystem)
|
|
}
|
|
}
|
|
|
|
func TestEventManagerAdd(t *testing.T) {
|
|
t.Parallel()
|
|
em := NewExchangeManager()
|
|
m, err := setupEventManager(&CommunicationManager{}, em, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
_, err = m.Add("", "", EventConditionParams{}, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, "")
|
|
if !errors.Is(err, ErrSubSystemNotStarted) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrSubSystemNotStarted)
|
|
}
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
_, err = m.Add("", "", EventConditionParams{}, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, "")
|
|
if !errors.Is(err, errExchangeDisabled) {
|
|
t.Errorf("error '%v', expected '%v'", err, errExchangeDisabled)
|
|
}
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
exch.SetDefaults()
|
|
err = em.Add(exch)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
|
}
|
|
_, err = m.Add(testExchange, "", EventConditionParams{}, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, "")
|
|
if !errors.Is(err, errInvalidItem) {
|
|
t.Errorf("error '%v', expected '%v'", err, errInvalidItem)
|
|
}
|
|
|
|
cond := EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
Price: 1337,
|
|
OrderbookAmount: 1337,
|
|
}
|
|
_, err = m.Add(testExchange, ItemPrice, cond, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, "")
|
|
if !errors.Is(err, errInvalidAction) {
|
|
t.Errorf("error '%v', expected '%v'", err, errInvalidAction)
|
|
}
|
|
|
|
_, err = m.Add(testExchange, ItemPrice, cond, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, ActionTest)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
|
|
action := ActionSMSNotify + "," + ActionTest
|
|
_, err = m.Add(testExchange, ItemPrice, cond, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, action)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
}
|
|
|
|
func TestEventManagerRemove(t *testing.T) {
|
|
t.Parallel()
|
|
em := NewExchangeManager()
|
|
m, err := setupEventManager(&CommunicationManager{}, em, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
if m.Remove(0) {
|
|
t.Error("expected false")
|
|
}
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
if m.Remove(0) {
|
|
t.Error("expected false")
|
|
}
|
|
action := ActionSMSNotify + "," + ActionTest
|
|
cond := EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
Price: 1337,
|
|
OrderbookAmount: 1337,
|
|
}
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
exch.SetDefaults()
|
|
err = em.Add(exch)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
|
}
|
|
id, err := m.Add(testExchange, ItemPrice, cond, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, action)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
|
|
if !m.Remove(id) {
|
|
t.Error("expected true")
|
|
}
|
|
}
|
|
|
|
func TestGetEventCounter(t *testing.T) {
|
|
t.Parallel()
|
|
em := NewExchangeManager()
|
|
m, err := setupEventManager(&CommunicationManager{}, em, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
total, executed := m.getEventCounter()
|
|
if total != 0 && executed != 0 {
|
|
t.Error("expected 0")
|
|
}
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
total, executed = m.getEventCounter()
|
|
if total != 0 && executed != 0 {
|
|
t.Error("expected 0")
|
|
}
|
|
action := ActionSMSNotify + "," + ActionTest
|
|
cond := EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
Price: 1337,
|
|
OrderbookAmount: 1337,
|
|
}
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
exch.SetDefaults()
|
|
err = em.Add(exch)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
|
}
|
|
_, err = m.Add(testExchange, ItemPrice, cond, currency.NewPair(currency.BTC, currency.USDC), asset.Spot, action)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
|
|
total, _ = m.getEventCounter()
|
|
if total == 0 {
|
|
t.Error("expected 1")
|
|
}
|
|
}
|
|
|
|
func TestCheckEventCondition(t *testing.T) {
|
|
em := NewExchangeManager()
|
|
m, err := setupEventManager(&CommunicationManager{}, em, 0, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
m.m.Lock()
|
|
err = m.checkEventCondition(nil)
|
|
if !errors.Is(err, ErrSubSystemNotStarted) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrSubSystemNotStarted)
|
|
}
|
|
m.m.Unlock()
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
m.m.Lock()
|
|
err = m.checkEventCondition(nil)
|
|
if !errors.Is(err, errNilEvent) {
|
|
t.Errorf("error '%v', expected '%v'", err, errNilEvent)
|
|
}
|
|
m.m.Unlock()
|
|
|
|
action := ActionSMSNotify + "," + ActionTest
|
|
cond := EventConditionParams{
|
|
Condition: ConditionGreaterThan,
|
|
Price: 1337,
|
|
OrderbookAmount: 1337,
|
|
}
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
exch.SetDefaults()
|
|
conf, err := exch.GetDefaultConfig(context.Background())
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = exch.Setup(conf)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = em.Add(exch)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
|
}
|
|
_, err = m.Add(testExchange, ItemPrice, cond, currency.NewPair(currency.BTC, currency.USD), asset.Spot, action)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
m.m.Lock()
|
|
err = m.checkEventCondition(&m.events[0])
|
|
if err != nil && !strings.Contains(err.Error(), "no tickers for") {
|
|
t.Error(err)
|
|
} else if err == nil {
|
|
t.Error("expected error")
|
|
}
|
|
m.m.Unlock()
|
|
_, err = exch.FetchTicker(context.Background(),
|
|
currency.NewPair(currency.BTC, currency.USD), asset.Spot)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
m.m.Lock()
|
|
err = m.checkEventCondition(&m.events[0])
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
m.m.Unlock()
|
|
|
|
m.events[0].Item = ItemOrderbook
|
|
m.events[0].Executed = false
|
|
m.events[0].Condition.CheckAsks = true
|
|
m.events[0].Condition.CheckBids = true
|
|
m.m.Lock()
|
|
err = m.checkEventCondition(&m.events[0])
|
|
if err != nil && !strings.Contains(err.Error(), "cannot find orderbook") {
|
|
t.Error(err)
|
|
} else if err == nil {
|
|
t.Error("expected error")
|
|
}
|
|
m.m.Unlock()
|
|
|
|
_, err = exch.FetchOrderbook(context.Background(),
|
|
currency.NewPair(currency.BTC, currency.USD), asset.Spot)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
m.m.Lock()
|
|
err = m.checkEventCondition(&m.events[0])
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
m.m.Unlock()
|
|
}
|