mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* Added dispatch service * Added orderbook streaming capabilities * Assigned correct orderbook.base exchange name * Fixed Requested niterinos Add in cli orderbook QA tool to gctcli Add exchange orderbook streaming * Add ticker streaming support through dispatch package * Added in some more info on error returns for orderbook.go * fix linter issues * Fix some issues * Update * Fix requested * move dispatch out of exchanges folder to its own independant folder * Fix requested * change orderbook string to tickers * Limit orderbooks to 50 and made dispatch system more stateless in operation * lower cases for update/retrieve/sub exchange name * Adds in asset validation and lower case conversion on cli * Remove comment * Moved timer to a higher scope so its not constantly initialised just reset per instance and removed returning unused channel on error * Rm unused release function in dispatch.go Reset timer and bleed buffered timer chan if needed in dispatch.go Added in ticker.Stop() and timer.Stop() functions for worker routine return in dispatch.go Index aggregated bid and ask functions for orderbook.go Added in dummy slice for wsorderbook_test.go * Moved drain to above Reset so potential race would not occur in dispatch.go Fix various linter issues dispatch.go * Fix some issues * change to start/stop service, added in service state change via cli, updated logger * fix requested * Add worker amount init spawning * fix linter issues * Fix more linter issues * More fixes * Fix race issue on releasing pipe channel on a close after shutting down dispatcher system * Moved all types to dispatch_types.go && remove panic * Moved types into serperate file && improve test coverage * RM unnecessary select case for draining channel && fixed error string * Added orderbook_types file and improved code coverage * gofmt file * reinstated select cases on drain because I am silly * Remove error for drop worker * Added more test cases * not checking error issue fix * remove func causing race in test, this has required protection via an exported function * set Gemini websocket orderbook exchange name
347 lines
7.6 KiB
Go
347 lines
7.6 KiB
Go
package engine
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"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"
|
|
)
|
|
|
|
var (
|
|
configLoaded = false
|
|
)
|
|
|
|
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) {
|
|
if !configLoaded {
|
|
loadConfig(t)
|
|
}
|
|
|
|
_, 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) {
|
|
if !configLoaded {
|
|
loadConfig(t)
|
|
}
|
|
|
|
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) {
|
|
if !configLoaded {
|
|
loadConfig(t)
|
|
}
|
|
|
|
_, 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()
|
|
if Bot == nil {
|
|
Bot = new(Engine)
|
|
}
|
|
|
|
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) {
|
|
if Bot == nil {
|
|
Bot = new(Engine)
|
|
}
|
|
Bot.Settings.Verbose = true
|
|
|
|
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),
|
|
Last: 0,
|
|
}
|
|
if err := ticker.ProcessTicker(e.Exchange, &tick, e.Asset); err != nil {
|
|
t.Fatal("unexpected result:", err)
|
|
}
|
|
if r := e.processTicker(); r {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
// now populate it with a number > 0
|
|
tick.Last = 1337
|
|
if err := ticker.ProcessTicker(e.Exchange, &tick, e.Asset); err != nil {
|
|
t.Fatal("unexpected result:", err)
|
|
}
|
|
if r := e.processTicker(); !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) {
|
|
if Bot == nil {
|
|
Bot = new(Engine)
|
|
}
|
|
Bot.Settings.Verbose = true
|
|
|
|
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}},
|
|
ExchangeName: e.Exchange,
|
|
AssetType: e.Asset,
|
|
}
|
|
if err := o.Process(); err != nil {
|
|
t.Fatal("unexpected result:", err)
|
|
}
|
|
|
|
if r := e.processOrderbook(); !r {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestCheckEventCondition(t *testing.T) {
|
|
t.Parallel()
|
|
if Bot == nil {
|
|
Bot = new(Engine)
|
|
}
|
|
Bot.Settings.Verbose = true
|
|
|
|
e := Event{
|
|
Item: ItemPrice,
|
|
}
|
|
if r := e.CheckEventCondition(); r {
|
|
t.Error("unexpected result")
|
|
}
|
|
|
|
e.Item = ItemOrderbook
|
|
if r := e.CheckEventCondition(); r {
|
|
t.Error("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestIsValidEvent(t *testing.T) {
|
|
if !configLoaded {
|
|
loadConfig(t)
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
loadConfig(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")
|
|
}
|
|
}
|