Skip to content

Commit

Permalink
Merge pull request #5132 from multiversx/add-unit-tests-for-node-package
Browse files Browse the repository at this point in the history
Unit tests for `node` package + small refactoring & fixes
  • Loading branch information
iulianpascalau authored Apr 3, 2023
2 parents f29f696 + 8a5f2d4 commit 31f14ca
Show file tree
Hide file tree
Showing 12 changed files with 1,166 additions and 102 deletions.
22 changes: 18 additions & 4 deletions node/external/logs/logsFacade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/testscommon"
Expand All @@ -23,7 +22,7 @@ func TestNewLogsFacade(t *testing.T) {
facade, err := NewLogsFacade(arguments)
require.ErrorIs(t, err, errCannotCreateLogsFacade)
require.ErrorContains(t, err, core.ErrNilStore.Error())
require.True(t, check.IfNil(facade))
require.Nil(t, facade)
})

t.Run("NilMarshaller", func(t *testing.T) {
Expand All @@ -36,7 +35,7 @@ func TestNewLogsFacade(t *testing.T) {
facade, err := NewLogsFacade(arguments)
require.ErrorIs(t, err, errCannotCreateLogsFacade)
require.ErrorContains(t, err, core.ErrNilMarshalizer.Error())
require.True(t, check.IfNil(facade))
require.Nil(t, facade)
})

t.Run("NilPubKeyConverter", func(t *testing.T) {
Expand All @@ -49,7 +48,7 @@ func TestNewLogsFacade(t *testing.T) {
facade, err := NewLogsFacade(arguments)
require.ErrorIs(t, err, errCannotCreateLogsFacade)
require.ErrorContains(t, err, core.ErrNilPubkeyConverter.Error())
require.True(t, check.IfNil(facade))
require.Nil(t, facade)
})
}

Expand Down Expand Up @@ -144,3 +143,18 @@ func TestLogsFacade_IncludeLogsInTransactionsShouldWork(t *testing.T) {
require.Nil(t, transactions[2].Logs)
require.Equal(t, "fourth", transactions[3].Logs.Events[0].Identifier)
}

func TestLogsFacade_IsInterfaceNil(t *testing.T) {
t.Parallel()

var lf *logsFacade
require.True(t, lf.IsInterfaceNil())

arguments := ArgsNewLogsFacade{
StorageService: genericMocks.NewChainStorerMock(7),
Marshaller: &marshal.GogoProtoMarshalizer{},
PubKeyConverter: testscommon.NewPubkeyConverterMock(32),
}
lf, _ = NewLogsFacade(arguments)
require.False(t, lf.IsInterfaceNil())
}
14 changes: 12 additions & 2 deletions node/external/nodeApiResolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/api"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/common"
Expand Down Expand Up @@ -127,7 +126,7 @@ func TestNewNodeApiResolver_ShouldWork(t *testing.T) {
nar, err := external.NewNodeApiResolver(arg)

assert.Nil(t, err)
assert.False(t, check.IfNil(nar))
assert.NotNil(t, nar)
}

func TestNodeApiResolver_CloseShouldReturnNil(t *testing.T) {
Expand Down Expand Up @@ -676,3 +675,14 @@ func TestNodeApiResolver_GetGasConfigs(t *testing.T) {
_ = nar.GetGasConfigs()
require.True(t, wasCalled)
}

func TestNodeApiResolver_IsInterfaceNil(t *testing.T) {
t.Parallel()

nar, _ := external.NewNodeApiResolver(external.ArgNodeApiResolver{})
require.True(t, nar.IsInterfaceNil())

arg := createMockArgs()
nar, _ = external.NewNodeApiResolver(arg)
require.False(t, nar.IsInterfaceNil())
}
18 changes: 16 additions & 2 deletions node/external/timemachine/fee/feeComputer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"
"testing"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/process"
Expand All @@ -24,7 +23,7 @@ func TestNewFeeComputer(t *testing.T) {

computer, err := NewFeeComputer(arguments)
require.Equal(t, process.ErrNilBuiltInFunctionsCostHandler, err)
require.True(t, check.IfNil(computer))
require.Nil(t, computer)
})

t.Run("AllArgumentsProvided", func(t *testing.T) {
Expand Down Expand Up @@ -216,3 +215,18 @@ func TestFeeComputer_InHighConcurrency(t *testing.T) {

wg.Wait()
}

func TestFeeComputer_IsInterfaceNil(t *testing.T) {
t.Parallel()

var fc *feeComputer
require.True(t, fc.IsInterfaceNil())

arguments := ArgsNewFeeComputer{
BuiltInFunctionsCostHandler: &testscommon.BuiltInCostHandlerStub{},
EconomicsConfig: testscommon.GetEconomicsConfig(),
}

fc, _ = NewFeeComputer(arguments)
require.False(t, fc.IsInterfaceNil())
}
14 changes: 14 additions & 0 deletions node/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"fmt"
"runtime/debug"
"sort"
"strconv"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/sharding"
logger "github.com/multiversx/mx-chain-logger-go"
)

const millisecondsInSecond = 1000
Expand All @@ -18,6 +20,8 @@ const initInt = int64(0)
const initString = ""
const initZeroString = "0"

var log = logger.GetOrCreate("node/metrics")

// InitBaseMetrics will initialize base, default metrics to 0 values
func InitBaseMetrics(appStatusHandler core.AppStatusHandler) error {
if check.IfNil(appStatusHandler) {
Expand Down Expand Up @@ -271,10 +275,20 @@ func InitMetrics(

// SaveUint64Metric will save an uint64 metric in status handler
func SaveUint64Metric(ash core.AppStatusHandler, key string, value uint64) {
if check.IfNil(ash) {
log.Error("programming error: nil AppStatusHandler in SaveUint64Metric", "stack", string(debug.Stack()))
return
}

ash.SetUInt64Value(key, value)
}

// SaveStringMetric will save a string metric in status handler
func SaveStringMetric(ash core.AppStatusHandler, key, value string) {
if check.IfNil(ash) {
log.Error("programming error: nil AppStatusHandler in SaveStringMetric", "stack", string(debug.Stack()))
return
}

ash.SetStringValue(key, value)
}
Loading

0 comments on commit 31f14ca

Please sign in to comment.