Files
gocryptotrader/engine/apiserver_test.go
Gareth Kirwan 37b1121bbd BTSE: Fix duplicate pair errors on Million pairs (M_*) (#1401)
* BTSE: Fix duplicate error on Million pairs (M_*)

BTSE has listed Pitbull token with two symbols:
PIT-USD and M_PIT-USD for millons of PIT / USD.
The native token is not tradable, so we ignore them and
get a base of M_PIT because that's what later APIs will accept

* BTSE: Fix test errors on locked market

* Common: Improve AppendError and ExcludeError

This change switches from a stateful multiError to caring more about the
Unwrap() []error interface, the same as [go standard
lib](https://github.com/golang/go/blob/go1.21.4/src/errors/wrap.go#L54-L68)

Notably, if we implement Unwrap() []error and do NOT implement Is() then
we get free compatibility with the core functions.

The only distateful thing here is needing to deeply unwrap fmt.Errorf
errors, since they don't flatten. I can't see any way around that

* Pairs: Fix exchange config Pairs loading

When a pair string contained two punctuation runes, the first one is used,
and the configFormat is ignored.

This fix checks the list and corrects any with the wrong delimiter, or
errors if the format is inconsistent.

* BTSE: Fix all tickers retrieved by GetTicker

PR #764 introduced GetTickers, but it wasn't rolled out to BTSE.
This fix ensures that when one ticker is a locked market, the rest continue to
function. Particularly important if the locked market wasn't even
enabled anyway.

* Kucoin: Fix test config future pairs

* BTSE: Remove PIT tests; Token removed

BTSE have removed the PIT token pairs

All these changes stand, and this just removes the test

* ITBit: Fix fatal error on second run

This fix removes incorrect config pair delimiter, because it would be
re-inserted into config the first run, and then error the second time.

This delimiter doesn't match the config we have.
There's no implementation of fetching pairs, so what's in config files
now is all that matters

* Engine: Fix TestConfigAllJsonResponse

* Clarity of non-matching json improved
* Handling for fixing pair delimiters
2023-12-19 14:40:13 +11:00

286 lines
7.7 KiB
Go

package engine
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/thrasher-corp/gocryptotrader/config"
)
func TestSetupAPIServerManager(t *testing.T) {
t.Parallel()
_, err := setupAPIServerManager(nil, nil, nil, nil, nil, "")
if !errors.Is(err, errNilRemoteConfig) {
t.Errorf("error '%v', expected '%v'", err, errNilRemoteConfig)
}
_, err = setupAPIServerManager(&config.RemoteControlConfig{}, nil, nil, nil, nil, "")
if !errors.Is(err, errNilPProfConfig) {
t.Errorf("error '%v', expected '%v'", err, errNilPProfConfig)
}
_, err = setupAPIServerManager(&config.RemoteControlConfig{}, &config.Profiler{}, nil, nil, nil, "")
if !errors.Is(err, errNilExchangeManager) {
t.Errorf("error '%v', expected '%v'", err, errNilExchangeManager)
}
_, err = setupAPIServerManager(&config.RemoteControlConfig{}, &config.Profiler{}, &ExchangeManager{}, nil, nil, "")
if !errors.Is(err, errNilBot) {
t.Errorf("error '%v', expected '%v'", err, errNilBot)
}
_, err = setupAPIServerManager(&config.RemoteControlConfig{}, &config.Profiler{}, &ExchangeManager{}, &fakeBot{}, nil, "")
if !errors.Is(err, errEmptyConfigPath) {
t.Errorf("error '%v', expected '%v'", err, errEmptyConfigPath)
}
wd, _ := os.Getwd()
_, err = setupAPIServerManager(&config.RemoteControlConfig{}, &config.Profiler{}, &ExchangeManager{}, &fakeBot{}, nil, wd)
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
}
func TestStartRESTServer(t *testing.T) {
t.Parallel()
wd, _ := os.Getwd()
m, err := setupAPIServerManager(&config.RemoteControlConfig{}, &config.Profiler{}, &ExchangeManager{}, &fakeBot{}, nil, wd)
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StartRESTServer()
if !errors.Is(err, errServerDisabled) {
t.Errorf("error '%v', expected '%v'", err, errServerDisabled)
}
m.remoteConfig.DeprecatedRPC.Enabled = true
err = m.StartRESTServer()
if err != nil {
t.Fatal(err)
}
}
func TestStartWebsocketServer(t *testing.T) {
t.Parallel()
wd, _ := os.Getwd()
m, err := setupAPIServerManager(&config.RemoteControlConfig{}, &config.Profiler{}, &ExchangeManager{}, &fakeBot{}, nil, wd)
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StartWebsocketServer()
if !errors.Is(err, errServerDisabled) {
t.Errorf("error '%v', expected '%v'", err, errServerDisabled)
}
m.remoteConfig.WebsocketRPC.Enabled = true
err = m.StartWebsocketServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
}
func TestStopRESTServer(t *testing.T) {
t.Parallel()
wd, _ := os.Getwd()
m, err := setupAPIServerManager(&config.RemoteControlConfig{
DeprecatedRPC: config.DepcrecatedRPCConfig{
Enabled: true,
ListenAddress: "localhost:9051",
},
}, &config.Profiler{}, &ExchangeManager{}, &fakeBot{}, nil, wd)
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StopRESTServer()
if !errors.Is(err, ErrSubSystemNotStarted) {
t.Errorf("error '%v', expected '%v'", err, ErrSubSystemNotStarted)
}
err = m.StartRESTServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StopRESTServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
// do it again to ensure things have reset appropriately and no errors occur starting
err = m.StartRESTServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StopRESTServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
}
func TestWebsocketStop(t *testing.T) {
t.Parallel()
wd, _ := os.Getwd()
m, err := setupAPIServerManager(&config.RemoteControlConfig{
WebsocketRPC: config.WebsocketRPCConfig{
Enabled: true,
ListenAddress: "localhost:9052",
},
}, &config.Profiler{}, &ExchangeManager{}, &fakeBot{}, nil, wd)
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StopWebsocketServer()
if !errors.Is(err, ErrSubSystemNotStarted) {
t.Errorf("error '%v', expected '%v'", err, ErrSubSystemNotStarted)
}
err = m.StartWebsocketServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StopWebsocketServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
// do it again to ensure things have reset appropriately and no errors occur starting
err = m.StartWebsocketServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
err = m.StopWebsocketServer()
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
}
func TestIsRESTServerRunning(t *testing.T) {
t.Parallel()
m := &apiServerManager{}
if m.IsRESTServerRunning() {
t.Error("expected false")
}
m.restStarted = 1
if !m.IsRESTServerRunning() {
t.Error("expected true")
}
m = nil
if m.IsRESTServerRunning() {
t.Error("expected false")
}
}
func TestIsWebsocketServerRunning(t *testing.T) {
t.Parallel()
m := &apiServerManager{}
if m.IsWebsocketServerRunning() {
t.Error("expected false")
}
m.websocketStarted = 1
if !m.IsWebsocketServerRunning() {
t.Error("expected true")
}
m = nil
if m.IsWebsocketServerRunning() {
t.Error("expected false")
}
}
func TestGetAllActiveOrderbooks(t *testing.T) {
man := NewExchangeManager()
bs, err := man.NewExchangeByName("Bitstamp")
if err != nil {
t.Fatal(err)
}
bs.SetDefaults()
err = man.Add(bs)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
resp := getAllActiveOrderbooks(man)
if resp == nil {
t.Error("expected not nil")
}
}
func TestGetAllActiveTickers(t *testing.T) {
t.Parallel()
man := NewExchangeManager()
bs, err := man.NewExchangeByName("Bitstamp")
if err != nil {
t.Fatal(err)
}
bs.SetDefaults()
err = man.Add(bs)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
resp := getAllActiveTickers(man)
if resp == nil {
t.Error("expected not nil")
}
}
func TestGetAllActiveAccounts(t *testing.T) {
t.Parallel()
man := NewExchangeManager()
bs, err := man.NewExchangeByName("Bitstamp")
if err != nil {
t.Fatal(err)
}
bs.SetDefaults()
err = man.Add(bs)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
resp := getAllActiveAccounts(man)
if resp == nil {
t.Error("expected not nil")
}
}
func makeHTTPGetRequest(t *testing.T, response interface{}) *http.Response {
t.Helper()
w := httptest.NewRecorder()
err := writeResponse(w, response)
if err != nil {
t.Error("Failed to make response.", err)
}
return w.Result()
}
// TestConfigAllJsonResponse test if config/all restful json response is valid
func TestConfigAllJsonResponse(t *testing.T) {
t.Parallel()
var c config.Config
err := c.LoadConfig(config.TestFile, true)
assert.NoError(t, err, "LoadConfig should not error")
resp := makeHTTPGetRequest(t, c)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "ReadAll should not error")
err = resp.Body.Close()
assert.NoError(t, err, "Close body should not error")
var responseConfig config.Config
err = json.Unmarshal(body, &responseConfig)
assert.NoError(t, err, "Unmarshal should not error")
for _, e := range responseConfig.Exchanges {
err = e.CurrencyPairs.SetDelimitersFromConfig()
assert.NoError(t, err, "SetDelimitersFromConfig should not error")
}
assert.Equal(t, c, responseConfig, "Config should match api response")
}
// fakeBot is a basic implementation of the iBot interface used for testing
type fakeBot struct{}
// SetupExchanges is a basic implementation of the iBot interface used for testing
func (f *fakeBot) SetupExchanges() error {
return nil
}