mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-18 07:26:50 +00:00
* implements futures functions and GRPC functions on new branch * lint and test fixes * Fix uneven split pnl. Adds collateral weight test. docs. New clear func * Test protection if someone has zero collateral * Uses string instead of double for accuracy * Fixes old code panic * context, match, docs * Addresses Shazniterinos, var names, expanded tests * Returns subaccount name, provides USD values when offlinecalc * Fixes oopsie * Fixes cool bug which allowed made up subaccount results * Subaccount override on FTX, subaccount results for collateral * Strenghten collateral account info checks. Improve FTX test * English is my first language * Fixes oopsies * Adds some conceptual futures order details to track PNL * Initial design of future order processing in the backtester * Introduces futures concept for collateral and spot/futures config diffs * Fixes most tests * Simple designs for collateral funding pair concept * Expands interface use so much it hurts * Implements more collateral interfaces * Adds liquidation, adds strategy, struggles with Binance * Attempts at getting FTX to work * Adds calculatePNL as a wrapper function and adds an `IsFutures` asset check * Successfully loads backtester with collateral currency * Fails to really get much going for supporting futures * Merges master changes * Fleshes out how FTX processes collateral * Further FTX collateral workings * hooks up more ftx collateral and pnl calculations * more funcs to flesh out handling * Adds more links, just can't fit the pieces together :( * Greatly expands futures order processing * Fleshes out position tracker to also handle asset and exchange +testing * RM linkedOrderID. rn positioncontroller, unexport * Successfully tracks futures order positions * Fails to calculate PNL * Calculates pnl from orders accurately with exception to flipping orders * Calculates PNL from orders * Adds another controller layer to make it ez from orderstore * Backtester now compiles. Adds test coverage * labels things add scaling collateral test * Calculates pnl in line with fees * Mostly accurate PNL, with exception to appending with diff prices * Adds locks, adds rpc function * grpc implementations * Gracefully handles rpc function * beautiful tests! * rejiggles tests to polish * Finishes FTX testing, adds comments * Exposes collateral calculations to rpc * Adds commands and testing for rpcserver.go functions * Increase testing and fix up backtester code * Returns cool changes to original branch * end of day fixes * Fixing some tests * Fixing tests 🎉 * Fixes all the tests * Splits the backtester setup and running into different files * Merge, minor fixes * Messing with some strategy updates * Failed understanding at collateral usage * Begins the creation of cash and carry strategy * Adds underlying pair, adds filldependentevent for futures * Completes fill prerequsite event implementation. Can't short though * Some bug fixes * investigating funds * CAN NOW CREATE A SHORT ORDER * Minor change in short size * Fixes for unrealised PNL & collateral rendering * Fixes lint and tests * Adds some verbosity * Updates to pnl calc * Tracks pnl for short orders, minor update to strategy * Close and open event based on conditions * Adds pnl data for currency statistics * Working through PNL calculation automatically. Now panics * Adds tracking, is blocked from design * Work to flesh out closing a position * vain attempts at tracking zeroing out bugs * woww, super fun new subloggers 🎉 * Begins attempt at automatically handling contracts and collateral based on direction * Merge master + fixes * Investigating issues with pnl and holdings * Minor pnl fixes * Fixes future position sizing, needs contract sizing * Can render pnl results, focussing on funding statistics * tracking candles for futures, but why not btc * Improves funding statistics * Colours and stats * Fixes collateral and snapshot bugs * Completes test * Fixes totals bug * Fix double buy, expand stats, fixes usd totals, introduce interface * Begins report formatting and calculations * Appends pnl to receiving curr. Fixes map[time]. accurate USD * Improves report output rendering * PNL stats in report. New tests for futures * Fixes existing tests before adding new coverage * Test coverage * Completes portfolio coverage * Increase coverage exchange, portfolio. fix size bug. NEW CHART * WHAT IS GOING ON WITH PNL * Fixes PNL calculation. Adds ability to skip om futures tracking * minor commit before merge * Adds basic liquidation to backtester * Changes liquidation to order based * Liquidationnnnnn * Further fleshes out liquidations * Completes liquidations in a honorable manner. Adds AppendReasonf * Beginnings of spot futures gap chart. Needs to link currencies to render difference * Removes fake liquidation. Adds cool new chart * Fixes somet tests,allows for zero fee value v nil distinction,New tests * Some annoying test fixes that took too long * portfolio coverage * holding coverage, privatisation funding * Testwork * boring tests * engine coverage * More backtesting coverage * Funding, strategy, report test coverage * Completes coverage of report package * Documentation, fixes some assumptions on asset errors * Changes before master merge * Lint and Tests * defaults to non-coloured rendering * Chart rendering * Fixes surprise non-local-lints * Niterinos to the extremeos * Fixes merge problems * The linter splintered across the glinting plinths * Many nits addressed. Now sells spot position on final candle * Adds forgotten coverage * Adds ability to size futures contracts to match spot positions. * fixes order sell sizing * Adds tests to sizing. Fixes charting issue * clint splintered the linters with flint * Improves stats, stat rendering * minifix * Fixes tests and fee bug * Merge fixeroos * Microfixes * Updates orderPNL on first Correctly utilises fees. Adds committed funds * New base funcs. New order summary * Fun test updates * Fix logo colouring * Fixes niteroonies * Fix report * BAD COMMIT * Fixes funding issues.Updates default fee rates.Combines cashcarry case * doc regen * Now returns err * Fixes sizing bug issue introduced in PR * Fixes fun fee/total US value bug * Fix chart bug. Show log charts with disclaimer * sellside fee * fixes fee and slippage view * Fixed slippage price issue * Fixes calculation and removes rendering * Fixes stats and some rendering * Merge fix * Fixes merge issues * go mod tidy, lint updates * New linter attempt * Version bump in appveyor and makefile * Regex filename, config fixes, template h2 fixes * Removes bad stats. * neatens config builder. Moves filename generator * Fixes issue where linter wants to fix my spelling * Fixes pointers and starts
642 lines
13 KiB
Go
642 lines
13 KiB
Go
package log
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common/convert"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
err := setupTestLoggers()
|
|
if err != nil {
|
|
log.Fatal("cannot set up test loggers", err)
|
|
}
|
|
tempDir, err := ioutil.TempDir(os.TempDir(), "")
|
|
if err != nil {
|
|
log.Fatal("Cannot create temporary file", err)
|
|
}
|
|
log.Println("temp dir created at:", tempDir)
|
|
LogPath = tempDir
|
|
r := m.Run()
|
|
err = os.Remove(tempDir)
|
|
if err != nil {
|
|
log.Println("failed to remove temp file:", tempDir)
|
|
}
|
|
os.Exit(r)
|
|
}
|
|
|
|
func setupTestLoggers() error {
|
|
logTest := Config{
|
|
Enabled: convert.BoolPtr(true),
|
|
SubLoggerConfig: SubLoggerConfig{
|
|
Output: "console",
|
|
Level: "INFO|WARN|DEBUG|ERROR",
|
|
},
|
|
AdvancedSettings: advancedSettings{
|
|
ShowLogSystemName: convert.BoolPtr(true),
|
|
Spacer: " | ",
|
|
TimeStampFormat: timestampFormat,
|
|
Headers: headers{
|
|
Info: "[INFO]",
|
|
Warn: "[WARN]",
|
|
Debug: "[DEBUG]",
|
|
Error: "[ERROR]",
|
|
},
|
|
},
|
|
SubLoggers: []SubLoggerConfig{
|
|
{
|
|
Name: "lOg",
|
|
Level: "INFO|DEBUG|WARN|ERROR",
|
|
Output: "stdout",
|
|
}},
|
|
}
|
|
RWM.Lock()
|
|
GlobalLogConfig = &logTest
|
|
RWM.Unlock()
|
|
if err := SetupGlobalLogger(); err != nil {
|
|
return err
|
|
}
|
|
return SetupSubLoggers(logTest.SubLoggers)
|
|
}
|
|
|
|
func SetupDisabled() error {
|
|
logTest := Config{
|
|
Enabled: convert.BoolPtr(false),
|
|
}
|
|
RWM.Lock()
|
|
GlobalLogConfig = &logTest
|
|
RWM.Unlock()
|
|
|
|
if err := SetupGlobalLogger(); err != nil {
|
|
return err
|
|
}
|
|
return SetupSubLoggers(logTest.SubLoggers)
|
|
}
|
|
|
|
func BenchmarkInfo(b *testing.B) {
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
Info(Global, "Hello this is an info benchmark")
|
|
}
|
|
}
|
|
|
|
func TestAddWriter(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := multiWriter(io.Discard, io.Discard)
|
|
if !errors.Is(err, errWriterAlreadyLoaded) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, errWriterAlreadyLoaded)
|
|
}
|
|
|
|
mw, err := multiWriter()
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
|
}
|
|
err = mw.Add(io.Discard)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = mw.Add(os.Stdin)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = mw.Add(os.Stdout)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if total := len(mw.writers); total != 3 {
|
|
t.Errorf("expected m.Writers to be 3 %v", total)
|
|
}
|
|
}
|
|
|
|
func TestRemoveWriter(t *testing.T) {
|
|
t.Parallel()
|
|
mw, err := multiWriter()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = mw.Add(io.Discard)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = mw.Add(os.Stdin)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = mw.Add(os.Stdout)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
total := len(mw.writers)
|
|
err = mw.Remove(os.Stdin)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = mw.Remove(os.Stdout)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = mw.Remove(&bytes.Buffer{})
|
|
if !errors.Is(err, errWriterNotFound) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, errWriterNotFound)
|
|
}
|
|
|
|
if len(mw.writers) != total-2 {
|
|
t.Errorf("expected m.Writers to be %v got %v", total-2, len(mw.writers))
|
|
}
|
|
}
|
|
|
|
type WriteShorter struct{}
|
|
|
|
func (w *WriteShorter) Write(p []byte) (int, error) {
|
|
return 1, nil
|
|
}
|
|
|
|
type WriteError struct{}
|
|
|
|
func (w *WriteError) Write(p []byte) (int, error) {
|
|
return 0, errWriteError
|
|
}
|
|
|
|
var errWriteError = errors.New("write error")
|
|
|
|
func TestMultiWriterWrite(t *testing.T) {
|
|
t.Parallel()
|
|
mw, err := multiWriter(io.Discard, &bytes.Buffer{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
payload := "woooooooooooooooooooooooooooooooooooow"
|
|
l, err := mw.Write([]byte(payload))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if l != len(payload) {
|
|
t.Fatal("unexpected return")
|
|
}
|
|
|
|
mw, err = multiWriter(&WriteShorter{}, io.Discard)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = mw.Write([]byte(payload))
|
|
if !errors.Is(err, io.ErrShortWrite) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, io.ErrShortWrite)
|
|
}
|
|
|
|
mw, err = multiWriter(&WriteError{}, io.Discard)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = mw.Write([]byte(payload))
|
|
if !errors.Is(err, errWriteError) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, errWriteError)
|
|
}
|
|
}
|
|
|
|
func TestGetWriters(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := getWriters(nil)
|
|
if !errors.Is(err, errSubloggerConfigIsNil) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, errSubloggerConfigIsNil)
|
|
}
|
|
|
|
outputWriters := "stDout|stderr|filE"
|
|
|
|
_, err = getWriters(&SubLoggerConfig{Output: outputWriters})
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
|
|
}
|
|
|
|
outputWriters = "stdout|stderr|file|noobs"
|
|
_, err = getWriters(&SubLoggerConfig{Output: outputWriters})
|
|
if !errors.Is(err, errUnhandledOutputWriter) {
|
|
t.Fatalf("received: '%v' but expected: '%v'", err, errUnhandledOutputWriter)
|
|
}
|
|
}
|
|
|
|
func TestGenDefaultSettings(t *testing.T) {
|
|
t.Parallel()
|
|
if cfg := GenDefaultSettings(); cfg.Enabled == nil {
|
|
t.Fatal("unexpected items in struct")
|
|
}
|
|
}
|
|
|
|
func TestLevel(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := Level("LOG")
|
|
if err != nil {
|
|
t.Errorf("Failed to get log %s levels skipping", err)
|
|
}
|
|
|
|
_, err = Level("totallyinvalidlogger")
|
|
if err == nil {
|
|
t.Error("Expected error on invalid logger")
|
|
}
|
|
}
|
|
|
|
func TestSetLevel(t *testing.T) {
|
|
t.Parallel()
|
|
newLevel, err := SetLevel("LOG", "ERROR")
|
|
if err != nil {
|
|
t.Skipf("Failed to get log %s levels skipping", err)
|
|
}
|
|
|
|
if newLevel.Info || newLevel.Debug || newLevel.Warn {
|
|
t.Error("failed to set level correctly")
|
|
}
|
|
|
|
if !newLevel.Error {
|
|
t.Error("failed to set level correctly")
|
|
}
|
|
|
|
_, err = SetLevel("abc12345556665", "ERROR")
|
|
if err == nil {
|
|
t.Error("SetLevel() Should return error on invalid logger")
|
|
}
|
|
}
|
|
|
|
func TestCloseLogger(t *testing.T) {
|
|
t.Parallel()
|
|
if err := CloseLogger(); err != nil {
|
|
t.Errorf("CloseLogger() failed %v", err)
|
|
}
|
|
}
|
|
|
|
func TestConfigureSubLogger(t *testing.T) {
|
|
t.Parallel()
|
|
err := configureSubLogger("LOG", "INFO", os.Stdin)
|
|
if err != nil {
|
|
t.Skipf("configureSubLogger() returned unexpected error %v", err)
|
|
}
|
|
levels := Global.GetLevels()
|
|
if (levels != Levels{Info: true, Debug: false}) {
|
|
t.Error("configureSubLogger() incorrectly configure subLogger")
|
|
}
|
|
if Global.name != "LOG" {
|
|
t.Error("configureSubLogger() Failed to uppercase name")
|
|
}
|
|
}
|
|
|
|
func TestSplitLevel(t *testing.T) {
|
|
t.Parallel()
|
|
levelsInfoDebug := splitLevel("INFO|DEBUG")
|
|
|
|
expected := Levels{
|
|
Info: true,
|
|
Debug: true,
|
|
Warn: false,
|
|
Error: false,
|
|
}
|
|
|
|
if levelsInfoDebug != expected {
|
|
t.Errorf("splitLevel() returned invalid data expected: %+v got: %+v", expected, levelsInfoDebug)
|
|
}
|
|
}
|
|
|
|
func BenchmarkInfoDisabled(b *testing.B) {
|
|
if err := SetupDisabled(); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
Info(Global, "Hello this is an info benchmark")
|
|
}
|
|
}
|
|
|
|
func BenchmarkInfof(b *testing.B) {
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
Infof(Global, "Hello this is an infof benchmark %v %v %v\n", n, 1, 2)
|
|
}
|
|
}
|
|
|
|
func BenchmarkInfoln(b *testing.B) {
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
Infoln(Global, "Hello this is an infoln benchmark")
|
|
}
|
|
}
|
|
|
|
func TestNewLogEvent(t *testing.T) {
|
|
t.Parallel()
|
|
w := &bytes.Buffer{}
|
|
RWM.Lock()
|
|
err := logger.newLogEvent("out", "header", "SUBLOGGER", w)
|
|
RWM.Unlock()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if w.String() == "" {
|
|
t.Error("newLogEvent() failed expected output got empty string")
|
|
}
|
|
|
|
RWM.Lock()
|
|
err = logger.newLogEvent("out", "header", "SUBLOGGER", nil)
|
|
RWM.Unlock()
|
|
if err == nil {
|
|
t.Error("Error expected with output is set to nil")
|
|
}
|
|
}
|
|
|
|
func TestInfo(t *testing.T) {
|
|
t.Parallel()
|
|
w := &bytes.Buffer{}
|
|
|
|
sl := registerNewSubLogger("TESTYMCTESTALOTINFO")
|
|
sl.SetLevels(splitLevel("INFO|WARN|DEBUG|ERROR"))
|
|
sl.SetOutput(w)
|
|
|
|
Info(sl, "Hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Infof(sl, "%s", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Infoln(sl, "hello", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
_, err := SetLevel("TESTYMCTESTALOTINFO", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
Info(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
|
|
Infoln(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
}
|
|
|
|
func TestDebug(t *testing.T) {
|
|
t.Parallel()
|
|
w := &bytes.Buffer{}
|
|
|
|
sl := registerNewSubLogger("TESTYMCTESTALOTDEBUG")
|
|
sl.SetLevels(splitLevel("INFO|WARN|DEBUG|ERROR"))
|
|
sl.SetOutput(w)
|
|
|
|
Debug(sl, "Hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Debugf(sl, "%s", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Debugln(sl, "hello", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
_, err := SetLevel("TESTYMCTESTALOTDEBUG", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
Debug(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
|
|
Debugln(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
}
|
|
|
|
func TestWarn(t *testing.T) {
|
|
t.Parallel()
|
|
w := &bytes.Buffer{}
|
|
|
|
sl := registerNewSubLogger("TESTYMCTESTALOTWARN")
|
|
sl.SetLevels(splitLevel("INFO|WARN|DEBUG|ERROR"))
|
|
sl.SetOutput(w)
|
|
|
|
Warn(sl, "Hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Warnf(sl, "%s", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Warnln(sl, "hello", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
_, err := SetLevel("TESTYMCTESTALOTWARN", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
Warn(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
|
|
Warnln(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
}
|
|
|
|
func TestError(t *testing.T) {
|
|
t.Parallel()
|
|
w := &bytes.Buffer{}
|
|
|
|
sl := registerNewSubLogger("TESTYMCTESTALOTERROR")
|
|
sl.SetLevels(splitLevel("INFO|WARN|DEBUG|ERROR"))
|
|
sl.SetOutput(w)
|
|
|
|
Error(sl, "Hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Errorf(sl, "%s", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
Errorln(sl, "hello", "hello")
|
|
|
|
if w.String() == "" {
|
|
t.Error("expected Info() to write output to buffer")
|
|
}
|
|
w.Reset()
|
|
|
|
_, err := SetLevel("TESTYMCTESTALOTERROR", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
Error(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
|
|
Errorln(sl, "HelloHello")
|
|
|
|
if w.String() != "" {
|
|
t.Error("Expected output buffer to be empty but wrote to output", w.String())
|
|
}
|
|
}
|
|
|
|
func TestSubLoggerName(t *testing.T) {
|
|
t.Parallel()
|
|
w := &bytes.Buffer{}
|
|
registerNewSubLogger("sublogger")
|
|
RWM.Lock()
|
|
err := logger.newLogEvent("out", "header", "SUBLOGGER", w)
|
|
RWM.Unlock()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(w.String(), "SUBLOGGER") {
|
|
t.Error("Expected SUBLOGGER in output")
|
|
}
|
|
|
|
RWM.Lock()
|
|
logger.ShowLogSystemName = false
|
|
RWM.Unlock()
|
|
w.Reset()
|
|
RWM.Lock()
|
|
err = logger.newLogEvent("out", "header", "SUBLOGGER", w)
|
|
RWM.Unlock()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(w.String(), "SUBLOGGER") {
|
|
t.Error("Unexpected SUBLOGGER in output")
|
|
}
|
|
}
|
|
|
|
func TestNewSubLogger(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := NewSubLogger("")
|
|
if !errors.Is(err, errEmptyLoggerName) {
|
|
t.Fatalf("received: %v but expected: %v", err, errEmptyLoggerName)
|
|
}
|
|
|
|
sl, err := NewSubLogger("TESTERINOS")
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: %v but expected: %v", err, nil)
|
|
}
|
|
|
|
Debug(sl, "testerinos")
|
|
|
|
_, err = NewSubLogger("TESTERINOS")
|
|
if !errors.Is(err, ErrSubLoggerAlreadyRegistered) {
|
|
t.Fatalf("received: %v but expected: %v", err, ErrSubLoggerAlreadyRegistered)
|
|
}
|
|
}
|
|
|
|
func BenchmarkNewLogEvent(b *testing.B) {
|
|
var bro bytes.Buffer
|
|
l := Logger{Spacer: " "}
|
|
for i := 0; i < b.N; i++ {
|
|
_ = l.newLogEvent("somedata", "header", "sublog", &bro)
|
|
}
|
|
}
|
|
|
|
func TestRotateWrite(t *testing.T) {
|
|
t.Parallel()
|
|
empty := Rotate{Rotate: convert.BoolPtr(true), FileName: "test.txt"}
|
|
payload := make([]byte, defaultMaxSize*megabyte+1)
|
|
_, err := empty.Write(payload)
|
|
if !errors.Is(err, errExceedsMaxFileSize) {
|
|
t.Fatalf("received: %v but expected: %v", err, errExceedsMaxFileSize)
|
|
}
|
|
|
|
empty.MaxSize = 1
|
|
payload = make([]byte, 1*megabyte+1)
|
|
_, err = empty.Write(payload)
|
|
if !errors.Is(err, errExceedsMaxFileSize) {
|
|
t.Fatalf("received: %v but expected: %v", err, errExceedsMaxFileSize)
|
|
}
|
|
|
|
// test write
|
|
payload = make([]byte, 1*megabyte-1)
|
|
_, err = empty.Write(payload)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: %v but expected: %v", err, nil)
|
|
}
|
|
|
|
// test rotate
|
|
payload = make([]byte, 1*megabyte)
|
|
_, err = empty.Write(payload)
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: %v but expected: %v", err, nil)
|
|
}
|
|
}
|
|
|
|
func TestOpenNew(t *testing.T) {
|
|
t.Parallel()
|
|
empty := Rotate{}
|
|
err := empty.openNew()
|
|
if !errors.Is(err, errFileNameIsEmpty) {
|
|
t.Fatalf("received: %v but expected: %v", err, errFileNameIsEmpty)
|
|
}
|
|
|
|
empty.FileName = "wow.txt"
|
|
err = empty.openNew()
|
|
if !errors.Is(err, nil) {
|
|
t.Fatalf("received: %v but expected: %v", err, nil)
|
|
}
|
|
}
|