From cfaaacabaff24a6ddde2285e63d0f9e1ef278cfd Mon Sep 17 00:00:00 2001 From: jules01 Date: Fri, 24 Mar 2023 17:44:39 +0200 Subject: [PATCH] - refactored print function --- trie/node.go | 10 +++------ trie/node_test.go | 53 +++++++++++++++++------------------------------ 2 files changed, 22 insertions(+), 41 deletions(-) diff --git a/trie/node.go b/trie/node.go index e75c29d2ff3..c69d0a9d484 100644 --- a/trie/node.go +++ b/trie/node.go @@ -130,15 +130,11 @@ func getNodeFromDBAndDecode(n []byte, db common.DBWriteCacher, marshalizer marsh } func treatLogError(logInstance logger.Logger, err error, key []byte) { - logLevel := logger.LogTrace - extraInfo := make([]interface{}, 0, 6) - extraInfo = append(extraInfo, "error", err, "key", key) - if !errors.IsClosingError(err) { - logLevel = logger.LogWarning - extraInfo = append(extraInfo, "stack trace", string(debug.Stack())) + if logInstance.GetLevel() != logger.LogTrace { + return } - logInstance.Log(logLevel, common.GetNodeFromDBErrorString, extraInfo...) + logInstance.Trace(common.GetNodeFromDBErrorString, "error", err, "key", key, "stack trace", string(debug.Stack())) } func resolveIfCollapsed(n node, pos byte, db common.DBWriteCacher) error { diff --git a/trie/node_test.go b/trie/node_test.go index 3eb3e9ffb51..35b5de45d35 100644 --- a/trie/node_test.go +++ b/trie/node_test.go @@ -10,8 +10,6 @@ import ( "github.com/multiversx/mx-chain-core-go/core/atomic" "github.com/multiversx/mx-chain-go/common" dataMock "github.com/multiversx/mx-chain-go/dataRetriever/mock" - mxErrors "github.com/multiversx/mx-chain-go/errors" - "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/testscommon" "github.com/multiversx/mx-chain-go/trie/keyBuilder" logger "github.com/multiversx/mx-chain-logger-go" @@ -631,57 +629,44 @@ func TestShouldStopIfContextDoneBlockingIfBusy(t *testing.T) { func TestTreatLogError(t *testing.T) { t.Parallel() - t.Run("error is not of type of closing error", func(t *testing.T) { + t.Run("logger instance is not in Trace mode, should not call", func(t *testing.T) { t.Parallel() key := []byte("key") err := errors.New("trie was not found") - wasCalled := false logInstance := &testscommon.LoggerStub{ - LogCalled: func(logLevel logger.LogLevel, message string, args ...interface{}) { - wasCalled = true - require.Equal(t, logger.LogWarning, logLevel) - require.Equal(t, common.GetNodeFromDBErrorString, message) - require.Equal(t, 6, len(args)) - expectedFirst5Args := []interface{}{"error", err, "key", key, "stack trace"} - require.Equal(t, expectedFirst5Args, args[:5]) + GetLevelCalled: func() logger.LogLevel { + return logger.LogDebug + }, + TraceCalled: func(message string, args ...interface{}) { + assert.Fail(t, "should have not called Log") }, } treatLogError(logInstance, err, key) - assert.True(t, wasCalled) treatLogError(log, err, key) //display only }) - t.Run("error is of type of closing error", func(t *testing.T) { + t.Run("logger instance is in Trace mode, should call", func(t *testing.T) { t.Parallel() key := []byte("key") - numCalled := 0 - var err error - + wasCalled := false + err := errors.New("error") logInstance := &testscommon.LoggerStub{ - LogCalled: func(logLevel logger.LogLevel, message string, args ...interface{}) { - numCalled++ - require.Equal(t, logger.LogTrace, logLevel) + GetLevelCalled: func() logger.LogLevel { + return logger.LogTrace + }, + TraceCalled: func(message string, args ...interface{}) { + wasCalled = true require.Equal(t, common.GetNodeFromDBErrorString, message) - require.Equal(t, 4, len(args)) - expectedFirst5Args := []interface{}{"error", err, "key", key} - require.Equal(t, expectedFirst5Args, args) + require.Equal(t, 6, len(args)) + expectedFirst5Args := []interface{}{"error", err, "key", key, "stack trace"} + require.Equal(t, expectedFirst5Args, args[:5]) }, } - t.Run("db is closed", func(t *testing.T) { - crtCounter := numCalled - err = storage.ErrDBIsClosed - treatLogError(logInstance, err, key) - assert.Equal(t, crtCounter+1, numCalled) - }) - t.Run("context closing", func(t *testing.T) { - crtCounter := numCalled - err = mxErrors.ErrContextClosing - treatLogError(logInstance, err, key) - assert.Equal(t, crtCounter+1, numCalled) - }) + treatLogError(logInstance, err, key) + assert.True(t, wasCalled) }) }