mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* orderbook: export orderbook nodes for external strategy inspection * orderbook: Add in methods for locking and unlocking multiple books at the same time e.g. book1.LockWith(book2); defer book1.UnlockWith(book2) * include waiting functionality for depth change alert * backtester: add word. * log: include logger changes to impl with downstream integration * engine: reduce params for loading exchange * assort: rm verbose in tests, change wording in ob, expose sync.waitgroup for ext. sync options * ticker: reduce map look ups and contention when using RW mutex when there are over 80% writes adds find last function to get the latest rate * engine/syncmanager: add in waitgroup for step over for external package calls * cleaup * engine: linter fix * currency/fx: include all references to fiat currencies to default * orderbook: Add in fields to Unsafe type for strategies to detect potential out of sync book operations * syncmanager: changed config variable to display correct time * ordermanager: Add time when none provided * currency/manager: update getasset param to get enabled assets for minor optimizations * ftx: use get all wallet balances for a better accounts breakdown * orderbook: unlock in reverse order * bithumb: fixes bug on market buy and sell orders * bithumb: fix bug for nonce is also time window sensitive * bithumb: get orders add required parameter * bithumb: Add asset type to account struct * currency: improve log output when checking currency and it fails * bithumb: Add error return on incomplete pair * ticker:unexport all service related methods * ticker/currency: fixes * orderbook: fix comment * engine: revert variable name in LoadExchange method * sync_manager: fix panic when enabling disabling manager * engine: fix naming convention of exported function and comments * engine: update comment * orderbook: fix comment for unsafe type
296 lines
6.8 KiB
Go
296 lines
6.8 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/config"
|
|
)
|
|
|
|
func TestLoadConfigWithSettings(t *testing.T) {
|
|
empty := ""
|
|
somePath := "somePath"
|
|
// Clean up after the tests
|
|
defer os.RemoveAll(somePath)
|
|
tests := []struct {
|
|
name string
|
|
flags []string
|
|
settings *Settings
|
|
want *string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "invalid file",
|
|
settings: &Settings{
|
|
ConfigFile: "nonExistent.json",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "test file",
|
|
settings: &Settings{
|
|
ConfigFile: config.TestFile,
|
|
EnableDryRun: true,
|
|
},
|
|
want: &empty,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "data dir in settings overrides config data dir",
|
|
flags: []string{"datadir"},
|
|
settings: &Settings{
|
|
ConfigFile: config.TestFile,
|
|
DataDir: somePath,
|
|
EnableDryRun: true,
|
|
},
|
|
want: &somePath,
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// prepare the 'flags'
|
|
flagSet := make(map[string]bool)
|
|
for _, v := range tt.flags {
|
|
flagSet[v] = true
|
|
}
|
|
// Run the test
|
|
got, err := loadConfigWithSettings(tt.settings, flagSet)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("loadConfigWithSettings() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != nil || tt.want != nil {
|
|
if (got == nil && tt.want != nil) || (got != nil && tt.want == nil) {
|
|
t.Errorf("loadConfigWithSettings() = is nil %v, want nil %v", got == nil, tt.want == nil)
|
|
} else if got.DataDirectory != *tt.want {
|
|
t.Errorf("loadConfigWithSettings() = %v, want %v", got.DataDirectory, *tt.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStartStopDoesNotCausePanic(t *testing.T) {
|
|
t.Parallel()
|
|
tempDir, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
t.Fatalf("Problem creating temp dir at %s: %s\n", tempDir, err)
|
|
}
|
|
defer func() {
|
|
err = os.RemoveAll(tempDir)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
botOne, err := NewFromSettings(&Settings{
|
|
ConfigFile: config.TestFile,
|
|
EnableDryRun: true,
|
|
DataDir: tempDir,
|
|
}, nil)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
botOne.Settings.EnableGRPCProxy = false
|
|
for i := range botOne.Config.Exchanges {
|
|
if botOne.Config.Exchanges[i].Name != testExchange {
|
|
// there is no need to load all exchanges for this test
|
|
botOne.Config.Exchanges[i].Enabled = false
|
|
}
|
|
}
|
|
if err = botOne.Start(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
botOne.Stop()
|
|
}
|
|
|
|
var enableExperimentalTest = false
|
|
|
|
func TestStartStopTwoDoesNotCausePanic(t *testing.T) {
|
|
t.Parallel()
|
|
if !enableExperimentalTest {
|
|
t.Skip("test is functional, however does not need to be included in go test runs")
|
|
}
|
|
tempDir, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
t.Fatalf("Problem creating temp dir at %s: %s\n", tempDir, err)
|
|
}
|
|
tempDir2, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
t.Fatalf("Problem creating temp dir at %s: %s\n", tempDir, err)
|
|
}
|
|
defer func() {
|
|
err = os.RemoveAll(tempDir)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = os.RemoveAll(tempDir2)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
botOne, err := NewFromSettings(&Settings{
|
|
ConfigFile: config.TestFile,
|
|
EnableDryRun: true,
|
|
DataDir: tempDir,
|
|
}, nil)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
botOne.Settings.EnableGRPCProxy = false
|
|
|
|
botTwo, err := NewFromSettings(&Settings{
|
|
ConfigFile: config.TestFile,
|
|
EnableDryRun: true,
|
|
DataDir: tempDir2,
|
|
}, nil)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
botTwo.Settings.EnableGRPCProxy = false
|
|
|
|
if err = botOne.Start(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err = botTwo.Start(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
botOne.Stop()
|
|
botTwo.Stop()
|
|
}
|
|
|
|
func TestCheckExchangeExists(t *testing.T) {
|
|
t.Parallel()
|
|
em := SetupExchangeManager()
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received '%v' expected '%v'", err, nil)
|
|
}
|
|
exch.SetDefaults()
|
|
em.Add(exch)
|
|
e := &Engine{ExchangeManager: em}
|
|
if e.GetExchangeByName(testExchange) == nil {
|
|
t.Errorf("TestGetExchangeExists: Unable to find exchange")
|
|
}
|
|
|
|
if e.GetExchangeByName("Asdsad") != nil {
|
|
t.Errorf("TestGetExchangeExists: Non-existent exchange found")
|
|
}
|
|
}
|
|
|
|
func TestGetExchangeByName(t *testing.T) {
|
|
t.Parallel()
|
|
em := SetupExchangeManager()
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received '%v' expected '%v'", err, nil)
|
|
}
|
|
exch.SetDefaults()
|
|
exch.SetEnabled(true)
|
|
em.Add(exch)
|
|
e := &Engine{ExchangeManager: em}
|
|
|
|
if !exch.IsEnabled() {
|
|
t.Errorf("TestGetExchangeByName: Unexpected result")
|
|
}
|
|
|
|
exch.SetEnabled(false)
|
|
bfx := e.GetExchangeByName(testExchange)
|
|
if bfx.IsEnabled() {
|
|
t.Errorf("TestGetExchangeByName: Unexpected result")
|
|
}
|
|
|
|
if exch.GetName() != testExchange {
|
|
t.Errorf("TestGetExchangeByName: Unexpected result")
|
|
}
|
|
|
|
exch = e.GetExchangeByName("Asdasd")
|
|
if exch != nil {
|
|
t.Errorf("TestGetExchangeByName: Non-existent exchange found")
|
|
}
|
|
}
|
|
|
|
func TestUnloadExchange(t *testing.T) {
|
|
t.Parallel()
|
|
em := SetupExchangeManager()
|
|
exch, err := em.NewExchangeByName(testExchange)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received '%v' expected '%v'", err, nil)
|
|
}
|
|
exch.SetDefaults()
|
|
exch.SetEnabled(true)
|
|
em.Add(exch)
|
|
e := &Engine{ExchangeManager: em,
|
|
Config: &config.Config{Exchanges: []config.ExchangeConfig{{Name: testExchange}}},
|
|
}
|
|
err = e.UnloadExchange("asdf")
|
|
if !errors.Is(err, config.ErrExchangeNotFound) {
|
|
t.Errorf("error '%v', expected '%v'", err, config.ErrExchangeNotFound)
|
|
}
|
|
|
|
err = e.UnloadExchange(testExchange)
|
|
if err != nil {
|
|
t.Errorf("TestUnloadExchange: Failed to get exchange. %s",
|
|
err)
|
|
}
|
|
|
|
err = e.UnloadExchange(testExchange)
|
|
if !errors.Is(err, ErrNoExchangesLoaded) {
|
|
t.Errorf("error '%v', expected '%v'", err, ErrNoExchangesLoaded)
|
|
}
|
|
}
|
|
|
|
func TestDryRunParamInteraction(t *testing.T) {
|
|
t.Parallel()
|
|
bot := &Engine{
|
|
ExchangeManager: SetupExchangeManager(),
|
|
Settings: Settings{},
|
|
Config: &config.Config{
|
|
Exchanges: []config.ExchangeConfig{
|
|
{
|
|
Name: testExchange,
|
|
WebsocketTrafficTimeout: time.Second,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if err := bot.LoadExchange(testExchange, nil); err != nil {
|
|
t.Error(err)
|
|
}
|
|
exchCfg, err := bot.Config.GetExchangeConfig(testExchange)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if exchCfg.Verbose {
|
|
t.Error("verbose should have been disabled")
|
|
}
|
|
if err = bot.UnloadExchange(testExchange); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
// Now set dryrun mode to true,
|
|
// enable exchange verbose mode and verify that verbose mode
|
|
// will be set on Bitfinex
|
|
bot.Settings.EnableDryRun = true
|
|
bot.Settings.CheckParamInteraction = true
|
|
bot.Settings.EnableExchangeVerbose = true
|
|
if err = bot.LoadExchange(testExchange, nil); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
exchCfg, err = bot.Config.GetExchangeConfig(testExchange)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !bot.Settings.EnableDryRun ||
|
|
!exchCfg.Verbose {
|
|
t.Error("dryrun should be true and verbose should be true")
|
|
}
|
|
}
|