Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for node package + small refactoring & fixes #5132

Merged
merged 15 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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