Skip to content

Commit

Permalink
support/log: decouple default logger and horizon unit tests (#4142)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland authored Dec 17, 2021
1 parent 3795a6a commit a04fe9e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 45 deletions.
50 changes: 12 additions & 38 deletions services/horizon/internal/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ type StaticMockServer struct {

// T provides a common set of functionality for each test in horizon
type T struct {
T *testing.T
Assert *assert.Assertions
Require *require.Assertions
Ctx context.Context
HorizonDB *sqlx.DB
CoreDB *sqlx.DB
T *testing.T
Assert *assert.Assertions
Require *require.Assertions
Ctx context.Context
HorizonDB *sqlx.DB
CoreDB *sqlx.DB
EndLogTest func() []logrus.Entry
}

// Context provides a context suitable for testing in tests that do not create
Expand All @@ -55,44 +56,19 @@ func DatabaseURL() string {
return tdb.HorizonURL()
}

// OverrideLogger calls StartTest on default logger. This is used
// by the testing system so that we can collect output from logs during test
// runs. Panics if the logger is already overridden.
func OverrideLogger() {
if endLogTest != nil {
panic("logger already overridden")
}

endLogTest = log.StartTest(log.DebugLevel)
}

// RestoreLogger restores the default horizon logger after it is overridden
// using a call to `OverrideLogger`. Panics if the default logger is not
// presently overridden.
func RestoreLogger() []logrus.Entry {
if endLogTest == nil {
panic("logger not overridden, cannot restore")
}

entries := endLogTest()
endLogTest = nil
return entries
}

// Start initializes a new test helper object and conceptually "starts" a new
// test
// Start initializes a new test helper object, a new instance of log,
// and conceptually "starts" a new test
func Start(t *testing.T) *T {
result := &T{}

result.T = t
logger := log.New()

OverrideLogger()

result.Ctx = log.Set(context.Background(), log.DefaultLogger)
result.Ctx = log.Set(context.Background(), logger)
result.HorizonDB = Database(t)
result.CoreDB = StellarCoreDatabase(t)
result.Assert = assert.New(t)
result.Require = require.New(t)
result.EndLogTest = logger.StartTest(log.DebugLevel)

return result
}
Expand All @@ -111,5 +87,3 @@ func StellarCoreDatabase(t *testing.T) *sqlx.DB {
func StellarCoreDatabaseURL() string {
return tdb.StellarCoreURL()
}

var endLogTest func() []logrus.Entry = nil
12 changes: 11 additions & 1 deletion services/horizon/internal/test/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"

"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
"github.com/stellar/go/services/horizon/internal/db2/schema"
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/operationfeestats"
Expand All @@ -26,7 +27,7 @@ func (t *T) CoreSession() *db.Session {
// Finish finishes the test, logging any accumulated horizon logs to the logs
// output
func (t *T) Finish() {
logEntries := RestoreLogger()
logEntries := t.testLogs()
operationfeestats.ResetState()

for _, entry := range logEntries {
Expand Down Expand Up @@ -164,3 +165,12 @@ func (t *T) LoadLedgerStatus() ledger.Status {

return next
}

// retrieves entries from test logger instance
func (t *T) testLogs() []logrus.Entry {
if t.EndLogTest == nil {
return []logrus.Entry{}
}

return t.EndLogTest()
}
6 changes: 0 additions & 6 deletions support/log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,6 @@ func Panic(args ...interface{}) {
DefaultLogger.Panic(args...)
}

// StartTest shifts the default logger into "test" mode. See Entry's
// documentation for the StartTest() method for more info.
func StartTest(level logrus.Level) func() []logrus.Entry {
return DefaultLogger.StartTest(level)
}

type contextKey string

var loggerContextKey = contextKey("logger")
Expand Down

0 comments on commit a04fe9e

Please sign in to comment.