Files
gocryptotrader/gctscript/wrappers/gct/exchange/exchange_test.go
Ryan O'Hara-Reid 4e135c9590 logger: reduce go routine generation (#992)
* logger: reduce go routine generation

* logger: shift most of processing and prep work to the worker pool, add pool for fields because each log we are pushing the struct to the heap, has better segregation now and includes a buffer in scope instead of relying on a pool

* logger: shift fmt package calls to worker pool

* logger: conform tests to new design

* linter: fix issues

* Update log/logger_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/logger_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* UN-GLORIOUS: nits

* logger: Handle config variable

* logger: NITERINOS BY GLORIOUS CODE

* logger: revert

* glorious: nits

* Panic at the disco: fix

* Panic at the disco: fix

* logger: make sure logger closed and job channel emptied on start up error

* fix tests

* logger: reduce globals

* logger: finished reduces globals, reduce workers to one too keep everything in line.

* logger: remove comments

* logger/exhchange: linter issues

* db/test: fix linter

* logger: add tests shift wait before unlock

* logger: consolidate worker code; fix linter issue and make sure we can sustain writing for external testing.

* logger: fix race and warn for conflict in config

* logger: fix name and add to tests

* logger: remove zero value field

* glorious: panic fix and removal of code

* logger: reinstate channels in close

* logger: shift reinstate processing to SetupGlobalLogger

* logger: segregate config.json from internal log.Config

* logger: fix silly mistake that is silly

* engine: Add protection for nil issues and implement new constructor in tests

* logger: Force singular mutex usage throughout package, throw away funcs that are not used outside of this package, unexport a bunch. Fix tests.

* logger: actually set advanced settings

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/logger_multiwriter.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* logger: test issue when not purging temp file and contents

* loggertest: add more protections for the panics

* linter: fix

* glorious: nits

* cleanup

* logger: linter fix

* linter: fix(?) :/

* linter: revert change

* linter: fix

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
2022-10-24 11:46:18 +11:00

227 lines
5.2 KiB
Go

package exchange
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/engine"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
// change these if you wish to test another exchange and/or currency pair
const (
exchName = "BTC Markets" // change to test on another exchange
exchAPIKEY = ""
exchAPISECRET = ""
exchClientID = ""
pairs = "BTC-AUD" // change to test another currency pair
delimiter = "-"
assetType = asset.Spot
orderID = "1234"
orderType = order.Limit
orderSide = order.Buy
orderClientID = ""
orderPrice = 1
orderAmount = 1
)
var (
settings = engine.Settings{
ConfigFile: filepath.Join("..", "..", "..", "..", "testdata", "configtest.json"),
EnableDryRun: true,
DataDir: filepath.Join("..", "..", "..", "..", "testdata", "gocryptotrader"),
}
exchangeTest = Exchange{}
)
func TestMain(m *testing.M) {
var t int
if err := setupEngine(); err != nil {
fmt.Printf("Failed to configure exchange test cannot continue: %v", err)
os.Exit(1)
}
t = m.Run()
cleanup()
os.Exit(t)
}
func TestExchange_Exchanges(t *testing.T) {
t.Parallel()
if x := exchangeTest.Exchanges(false); len(x) != 1 {
t.Fatalf("expected 1 received %v", x)
}
}
func TestExchange_GetExchange(t *testing.T) {
t.Parallel()
_, err := exchangeTest.GetExchange(exchName)
if err != nil {
t.Fatal(err)
}
_, err = exchangeTest.GetExchange("hello world")
if err == nil {
t.Fatal("unexpected error message received nil")
}
}
func TestExchange_IsEnabled(t *testing.T) {
t.Parallel()
x := exchangeTest.IsEnabled(exchName)
if !x {
t.Fatal("expected return to be true")
}
x = exchangeTest.IsEnabled("fake_exchange")
if x {
t.Fatal("expected return to be false")
}
}
func TestExchange_Ticker(t *testing.T) {
t.Parallel()
c, err := currency.NewPairDelimiter(pairs, delimiter)
if err != nil {
t.Fatal(err)
}
_, err = exchangeTest.Ticker(context.Background(), exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
}
func TestExchange_Orderbook(t *testing.T) {
t.Parallel()
c, err := currency.NewPairDelimiter(pairs, delimiter)
if err != nil {
t.Fatal(err)
}
_, err = exchangeTest.Orderbook(context.Background(), exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
}
func TestExchange_Pairs(t *testing.T) {
t.Parallel()
_, err := exchangeTest.Pairs(exchName, false, assetType)
if err != nil {
t.Fatal(err)
}
_, err = exchangeTest.Pairs(exchName, true, assetType)
if err != nil {
t.Fatal(err)
}
}
func TestExchange_AccountInformation(t *testing.T) {
if !configureExchangeKeys() {
t.Skip("no exchange configured test skipped")
}
_, err := exchangeTest.AccountInformation(context.Background(),
exchName, asset.Spot)
if err != nil {
t.Fatal(err)
}
}
func TestExchange_QueryOrder(t *testing.T) {
if !configureExchangeKeys() {
t.Skip("no exchange configured test skipped")
}
t.Parallel()
_, err := exchangeTest.QueryOrder(context.Background(),
exchName, orderID, currency.EMPTYPAIR, assetType)
if err != nil {
t.Fatal(err)
}
}
func TestExchange_SubmitOrder(t *testing.T) {
if !configureExchangeKeys() {
t.Skip("no exchange configured test skipped")
}
t.Parallel()
c, err := currency.NewPairDelimiter(pairs, delimiter)
if err != nil {
t.Fatal(err)
}
tempOrder := &order.Submit{
Pair: c,
Type: orderType,
Side: orderSide,
Price: orderPrice,
Amount: orderAmount,
ClientID: orderClientID,
Exchange: exchName,
AssetType: asset.Spot,
}
_, err = exchangeTest.SubmitOrder(context.Background(), tempOrder)
if err != nil {
t.Fatal(err)
}
}
func TestExchange_CancelOrder(t *testing.T) {
if !configureExchangeKeys() {
t.Skip("no exchange configured test skipped")
}
t.Parallel()
cp := currency.NewPair(currency.BTC, currency.USD)
a := asset.Spot
_, err := exchangeTest.CancelOrder(context.Background(),
exchName, orderID, cp, a)
if err != nil {
t.Fatal(err)
}
}
func TestOHLCV(t *testing.T) {
t.Parallel()
cp := currency.NewPair(currency.BTC, currency.AUD)
cp.Delimiter = currency.DashDelimiter
calvinKline, err := exchangeTest.OHLCV(context.Background(), exchName, cp, assetType, time.Now().Add(-time.Hour*24).UTC(), time.Now().UTC(), kline.OneHour)
if err != nil {
t.Error(err)
}
if calvinKline.Exchange != exchName {
t.Error("unexpected response")
}
}
func setupEngine() (err error) {
engine.Bot, err = engine.NewFromSettings(&settings, nil)
if err != nil {
return err
}
em := engine.SetupExchangeManager()
engine.Bot.ExchangeManager = em
return engine.Bot.LoadExchange(exchName, nil)
}
func cleanup() {
err := os.RemoveAll(settings.DataDir)
if err != nil {
fmt.Printf("Clean up failed to remove file: %v manual removal may be required", err)
}
}
func configureExchangeKeys() bool {
ex, err := engine.Bot.GetExchangeByName(exchName)
if err != nil {
return false
}
b := ex.GetBase()
b.SetCredentials(exchAPIKEY, exchAPISECRET, exchClientID, "", "", "")
b.SkipAuthCheck = true
return b.AreCredentialsValid(context.Background())
}