mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* Initial codes for a trade tracker * Moving everything in a broken fashion * Removes tradetracker. Removes some errors for subsystems * Cleans up some subsystems, renames stuttering types. Removes some global Bot usage * More basic subsystem renaming and file moving * Removes engine dependency from events,ntpserver,ordermanager,comms manager * Exports eventManager, fixes rpcserver. puts rpcserver back for now * Removes redundant error message, further removes engine dependencies * experimental end of day interface usage * adds ability to build the application * Withdraw and event manager handling * cleans up apiserver and communications manager * Cleans up some start/setup processes. Though should separate * More consistency with Setup Start Stop IsRunning funcs * Final consistency pass before testing phase * Fixes engine tests. Fixes stop nil issue * api server tests * Communications manager testing * Connection manager tests and nilsubsystem error * End of day currencypairsyncer tests * Adds databaseconnection/databaseconnection_test.go * Adds withdrawal manager tests * Deposit address testing. Moved orderbook sync first as its more important * Adds test for event manager * More full eventmanager testing * Adds testfile. Enables skipped test. * ntp manager tests * Adds ordermanager tests, Extracts a whole new subsystem from engine and fanangles import cycles * Adds websocket routine manager tests * Basic portfolio manager testing * Fixes issue with currency pair sync startup * Fixes issue with event manager startup * Starts the order manager before backtester starts * Fixes fee tests. Expands testing. Doesnt fix races * Fixes most test races * Resolves data races * Fixes subsystem test issues * currency pair syncer coverage tests * Refactors portfolio. Fixes tests. Withdraw validation Portfolio didn't need to exist with a portfolio manager. Now the porfolio manager is in charge how the portfolio is handled and all portfolio functions are attached to the base instead of just exported at the package level Withdrawal validation occurred at the exchange level when it can just be run at the withdrawal manager level. All withdrawal requests go through that endpoint * lint -fix * golang lint fixes * lints and comments everything * Updates GCT logo, adds documentation for some subsystems * More documentation and more logo updates * Fixes backtesting and apiserver errors encountered * Fixes errors and typos from reviewing * More minor fixes * Changes %h verb to %w * reverbs to %s * Humbly begins reverting to more flat engine package The main reasoning for this is that the subsystem split doesn't make sense in a golang environment. The subsystems are only meant to be used with engine and so by placing them in a non-engine area, it does not work and is inconsistent with the rest of the application's package layout. This will begin salvaging the changes made by reverting to a flat engine package, but maintaining the consistent designs introduced. Further, I will look to remove any TestMains and decrease the scope of testing to be more local and decrease the issues that have been caused from our style of testing. * Manages to re-flatten things. Everything is within its own file * mini fixes * Fixes tests and data races and lints * Updates docs tool for engine to create filename readmes * os -> ioutil * remove err * Appveyor version increase test * Removes tCleanup as its unsupported on appveyor * Adds stuff that I thought was in previous merge master commit * Removes cancel from test * Fixes really fun test-exclusive data race * minor nit fixes * niterinos * docs gen * rm;rf test * Remove typoline. expands startstop helper. Splits apiserver * Removes accidental folder * Uses update instead of replace for order upsert * addresses nits. Renames files. Regenerates documentation. * lint and removal of comments * Add new test for default scenario * Fixes typo * regen docs
562 lines
13 KiB
Go
562 lines
13 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
)
|
|
|
|
// omfExchange aka ordermanager fake exchange overrides exchange functions
|
|
// we're not testing an actual exchange's implemented functions
|
|
type omfExchange struct {
|
|
exchange.IBotExchange
|
|
}
|
|
|
|
// CancelOrder overrides testExchange's cancel order function
|
|
// to do the bare minimum required with no API calls or credentials required
|
|
func (f omfExchange) CancelOrder(o *order.Cancel) error {
|
|
o.Status = order.Cancelled
|
|
return nil
|
|
}
|
|
|
|
// GetOrderInfo overrides testExchange's get order function
|
|
// to do the bare minimum required with no API calls or credentials required
|
|
func (f omfExchange) GetOrderInfo(orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
|
|
if orderID == "" {
|
|
return order.Detail{}, errors.New("")
|
|
}
|
|
|
|
return order.Detail{
|
|
Exchange: testExchange,
|
|
ID: orderID,
|
|
Pair: pair,
|
|
AssetType: assetType,
|
|
}, nil
|
|
}
|
|
|
|
func TestSetupOrderManager(t *testing.T) {
|
|
_, err := SetupOrderManager(nil, nil, nil, false)
|
|
if !errors.Is(err, errNilExchangeManager) {
|
|
t.Errorf("error '%v', expected '%v'", err, errNilExchangeManager)
|
|
}
|
|
|
|
_, err = SetupOrderManager(SetupExchangeManager(), nil, nil, false)
|
|
if !errors.Is(err, errNilCommunicationsManager) {
|
|
t.Errorf("error '%v', expected '%v'", err, errNilCommunicationsManager)
|
|
}
|
|
_, err = SetupOrderManager(SetupExchangeManager(), &CommunicationManager{}, nil, false)
|
|
if !errors.Is(err, errNilWaitGroup) {
|
|
t.Errorf("error '%v', expected '%v'", err, errNilWaitGroup)
|
|
}
|
|
var wg sync.WaitGroup
|
|
_, err = SetupOrderManager(SetupExchangeManager(), &CommunicationManager{}, &wg, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
}
|
|
|
|
func TestOrderManagerStart(t *testing.T) {
|
|
var m *OrderManager
|
|
err := m.Start()
|
|
if !errors.Is(err, ErrNilSubsystem) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrNilSubsystem)
|
|
}
|
|
var wg sync.WaitGroup
|
|
m, err = SetupOrderManager(SetupExchangeManager(), &CommunicationManager{}, &wg, 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)
|
|
}
|
|
}
|
|
|
|
func TestOrderManagerIsRunning(t *testing.T) {
|
|
var m *OrderManager
|
|
if m.IsRunning() {
|
|
t.Error("expected false")
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
m, err := SetupOrderManager(SetupExchangeManager(), &CommunicationManager{}, &wg, false)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
if m.IsRunning() {
|
|
t.Error("expected false")
|
|
}
|
|
|
|
err = m.Start()
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
if !m.IsRunning() {
|
|
t.Error("expected true")
|
|
}
|
|
}
|
|
|
|
func TestOrderManagerStop(t *testing.T) {
|
|
var m *OrderManager
|
|
err := m.Stop()
|
|
if !errors.Is(err, ErrNilSubsystem) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrNilSubsystem)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
m, err = SetupOrderManager(SetupExchangeManager(), &CommunicationManager{}, &wg, false)
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func OrdersSetup(t *testing.T) *OrderManager {
|
|
var wg sync.WaitGroup
|
|
em := SetupExchangeManager()
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
exch.SetDefaults()
|
|
|
|
fakeExchange := omfExchange{
|
|
IBotExchange: exch,
|
|
}
|
|
em.Add(fakeExchange)
|
|
m, err := SetupOrderManager(em, &CommunicationManager{}, &wg, 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)
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func TestOrdersGet(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
if m.orderStore.get() == nil {
|
|
t.Error("orderStore not established")
|
|
}
|
|
}
|
|
|
|
func TestOrdersAdd(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
err := m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestOrdersAdd",
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = m.orderStore.add(&order.Detail{
|
|
Exchange: "testTest",
|
|
ID: "TestOrdersAdd",
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error from non existent exchange")
|
|
}
|
|
|
|
err = m.orderStore.add(nil)
|
|
if err == nil {
|
|
t.Error("Expected error from nil order")
|
|
}
|
|
|
|
err = m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestOrdersAdd",
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error re-adding order")
|
|
}
|
|
}
|
|
|
|
func TestGetByInternalOrderID(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
err := m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestGetByInternalOrderID",
|
|
InternalOrderID: "internalTest",
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
o, err := m.orderStore.getByInternalOrderID("internalTest")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if o == nil {
|
|
t.Fatal("Expected a matching order")
|
|
}
|
|
if o.ID != "TestGetByInternalOrderID" {
|
|
t.Error("Expected to retrieve order")
|
|
}
|
|
|
|
_, err = m.orderStore.getByInternalOrderID("NoOrder")
|
|
if err != ErrOrderNotFound {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestGetByExchange(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
err := m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestGetByExchange",
|
|
InternalOrderID: "internalTestGetByExchange",
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
err = m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestGetByExchange2",
|
|
InternalOrderID: "internalTestGetByExchange2",
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
err = m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestGetByExchange3",
|
|
InternalOrderID: "internalTest3",
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
var o []*order.Detail
|
|
o, err = m.orderStore.getByExchange(testExchange)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if o == nil {
|
|
t.Error("Expected non nil response")
|
|
}
|
|
var o1Found, o2Found bool
|
|
for i := range o {
|
|
if o[i].ID == "TestGetByExchange" && o[i].Exchange == testExchange {
|
|
o1Found = true
|
|
}
|
|
if o[i].ID == "TestGetByExchange2" && o[i].Exchange == testExchange {
|
|
o2Found = true
|
|
}
|
|
}
|
|
if !o1Found || !o2Found {
|
|
t.Error("Expected orders 'TestGetByExchange' and 'TestGetByExchange2' to be returned")
|
|
}
|
|
|
|
_, err = m.orderStore.getByInternalOrderID("NoOrder")
|
|
if err != ErrOrderNotFound {
|
|
t.Error(err)
|
|
}
|
|
err = m.orderStore.add(&order.Detail{
|
|
Exchange: "thisWillFail",
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected exchange not found error")
|
|
}
|
|
}
|
|
|
|
func TestGetByExchangeAndID(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
err := m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestGetByExchangeAndID",
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
o, err := m.orderStore.getByExchangeAndID(testExchange, "TestGetByExchangeAndID")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if o.ID != "TestGetByExchangeAndID" {
|
|
t.Error("Expected to retrieve order")
|
|
}
|
|
|
|
_, err = m.orderStore.getByExchangeAndID("", "TestGetByExchangeAndID")
|
|
if err != ErrExchangeNotFound {
|
|
t.Error(err)
|
|
}
|
|
|
|
_, err = m.orderStore.getByExchangeAndID(testExchange, "")
|
|
if err != ErrOrderNotFound {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestExists(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
if m.orderStore.exists(nil) {
|
|
t.Error("Expected false")
|
|
}
|
|
o := &order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestExists",
|
|
}
|
|
err := m.orderStore.add(o)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
b := m.orderStore.exists(o)
|
|
if !b {
|
|
t.Error("Expected true")
|
|
}
|
|
}
|
|
|
|
func TestCancelOrder(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
|
|
err := m.Cancel(nil)
|
|
if err == nil {
|
|
t.Error("Expected error due to empty order")
|
|
}
|
|
|
|
err = m.Cancel(&order.Cancel{})
|
|
if err == nil {
|
|
t.Error("Expected error due to empty order")
|
|
}
|
|
|
|
err = m.Cancel(&order.Cancel{
|
|
Exchange: testExchange,
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error due to no order ID")
|
|
}
|
|
|
|
err = m.Cancel(&order.Cancel{
|
|
ID: "ID",
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error due to no Exchange")
|
|
}
|
|
|
|
err = m.Cancel(&order.Cancel{
|
|
ID: "ID",
|
|
Exchange: testExchange,
|
|
AssetType: asset.Binary,
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error due to bad asset type")
|
|
}
|
|
|
|
o := &order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "1337",
|
|
Status: order.New,
|
|
}
|
|
err = m.orderStore.add(o)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
err = m.Cancel(&order.Cancel{
|
|
ID: "Unknown",
|
|
Exchange: testExchange,
|
|
AssetType: asset.Spot,
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error due to no order found")
|
|
}
|
|
|
|
pair, err := currency.NewPairFromString("BTCUSD")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cancel := &order.Cancel{
|
|
Exchange: testExchange,
|
|
ID: "1337",
|
|
Side: order.Sell,
|
|
Status: order.New,
|
|
AssetType: asset.Spot,
|
|
Date: time.Now(),
|
|
Pair: pair,
|
|
}
|
|
err = m.Cancel(cancel)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
|
|
if o.Status != order.Cancelled {
|
|
t.Error("Failed to cancel")
|
|
}
|
|
}
|
|
|
|
func TestGetOrderInfo(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
_, err := m.GetOrderInfo("", "", currency.Pair{}, "")
|
|
if err == nil {
|
|
t.Error("Expected error due to empty order")
|
|
}
|
|
|
|
var result order.Detail
|
|
result, err = m.GetOrderInfo(testExchange, "1337", currency.Pair{}, "")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if result.ID != "1337" {
|
|
t.Error("unexpected order returned")
|
|
}
|
|
|
|
result, err = m.GetOrderInfo(testExchange, "1337", currency.Pair{}, "")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if result.ID != "1337" {
|
|
t.Error("unexpected order returned")
|
|
}
|
|
}
|
|
|
|
func TestCancelAllOrders(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
o := &order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "TestCancelAllOrders",
|
|
Status: order.New,
|
|
}
|
|
err := m.orderStore.add(o)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
exch := m.orderStore.exchangeManager.GetExchangeByName(testExchange)
|
|
m.CancelAllOrders([]exchange.IBotExchange{})
|
|
if o.Status == order.Cancelled {
|
|
t.Error("Order should not be cancelled")
|
|
}
|
|
|
|
m.CancelAllOrders([]exchange.IBotExchange{exch})
|
|
if o.Status != order.Cancelled {
|
|
t.Error("Order should be cancelled")
|
|
}
|
|
|
|
o.Status = order.New
|
|
m.CancelAllOrders(nil)
|
|
if o.Status != order.New {
|
|
t.Error("Order should not be cancelled")
|
|
}
|
|
}
|
|
|
|
func TestSubmit(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
_, err := m.Submit(nil)
|
|
if err == nil {
|
|
t.Error("Expected error from nil order")
|
|
}
|
|
|
|
o := &order.Submit{
|
|
Exchange: "",
|
|
ID: "FakePassingExchangeOrder",
|
|
Status: order.New,
|
|
Type: order.Market,
|
|
}
|
|
_, err = m.Submit(o)
|
|
if err == nil {
|
|
t.Error("Expected error from empty exchange")
|
|
}
|
|
|
|
o.Exchange = testExchange
|
|
_, err = m.Submit(o)
|
|
if err == nil {
|
|
t.Error("Expected error from validation")
|
|
}
|
|
|
|
pair, err := currency.NewPairFromString("BTCUSD")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
m.cfg.EnforceLimitConfig = true
|
|
m.cfg.AllowMarketOrders = false
|
|
o.Pair = pair
|
|
o.AssetType = asset.Spot
|
|
o.Side = order.Buy
|
|
o.Amount = 1
|
|
o.Price = 1
|
|
_, err = m.Submit(o)
|
|
if err == nil {
|
|
t.Error("Expected fail due to order market type is not allowed")
|
|
}
|
|
m.cfg.AllowMarketOrders = true
|
|
m.cfg.LimitAmount = 1
|
|
o.Amount = 2
|
|
_, err = m.Submit(o)
|
|
if err == nil {
|
|
t.Error("Expected fail due to order limit exceeds allowed limit")
|
|
}
|
|
m.cfg.LimitAmount = 0
|
|
m.cfg.AllowedExchanges = []string{"fake"}
|
|
_, err = m.Submit(o)
|
|
if err == nil {
|
|
t.Error("Expected fail due to order exchange not found in allowed list")
|
|
}
|
|
|
|
failPair, err := currency.NewPairFromString("BTCAUD")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
m.cfg.AllowedExchanges = nil
|
|
m.cfg.AllowedPairs = currency.Pairs{failPair}
|
|
_, err = m.Submit(o)
|
|
if err == nil {
|
|
t.Error("Expected fail due to order pair not found in allowed list")
|
|
}
|
|
|
|
m.cfg.AllowedPairs = nil
|
|
_, err = m.Submit(o)
|
|
if !errors.Is(err, exchange.ErrAuthenticatedRequestWithoutCredentialsSet) {
|
|
t.Errorf("error '%v', expected '%v'", err, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
|
|
}
|
|
|
|
err = m.orderStore.add(&order.Detail{
|
|
Exchange: testExchange,
|
|
ID: "FakePassingExchangeOrder",
|
|
})
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("error '%v', expected '%v'", err, nil)
|
|
}
|
|
|
|
o2, err := m.orderStore.getByExchangeAndID(testExchange, "FakePassingExchangeOrder")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if o2.InternalOrderID == "" {
|
|
t.Error("Failed to assign internal order id")
|
|
}
|
|
}
|
|
|
|
func TestProcessOrders(t *testing.T) {
|
|
m := OrdersSetup(t)
|
|
m.processOrders()
|
|
}
|