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

Conversation

iulianpascalau
Copy link
Contributor

@iulianpascalau iulianpascalau commented Mar 28, 2023

Reasoning behind the pull request

  • added unit tests for increasing code coverage

Proposed changes

  • added unit tests for the node runner

Testing procedure

  • standard system test. Stopping the node & shuffling out should work, also, the stats directory should now contain the genesisSmartContracts.json and the gasSchedules directory and all the gas schedules files.

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

@codecov-commenter
Copy link

codecov-commenter commented Mar 28, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +1.13 🎉

Comparison is base (6a1b1c8) 75.74% compared to head (ede0314) 76.87%.

❗ Current head ede0314 differs from pull request most recent head 8a5f2d4. Consider uploading reports for the commit 8a5f2d4 to get more accurate results

Additional details and impacted files
@@              Coverage Diff              @@
##           rc/v1.5.0    #5132      +/-   ##
=============================================
+ Coverage      75.74%   76.87%   +1.13%     
=============================================
  Files            650      650              
  Lines          85464    85470       +6     
=============================================
+ Hits           64735    65708     +973     
+ Misses         15639    14555    -1084     
- Partials        5090     5207     +117     
Impacted Files Coverage Δ
node/metrics/metrics.go 100.00% <100.00%> (+35.44%) ⬆️
node/nodeRunner.go 74.00% <100.00%> (+74.00%) ⬆️

... and 5 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@iulianpascalau iulianpascalau changed the base branch from master to rc/v1.5.0 March 29, 2023 13:00
@iulianpascalau iulianpascalau changed the title [WIP]Unit tests for node package Unit tests for node package + small refactoring Mar 29, 2023
@iulianpascalau iulianpascalau changed the title Unit tests for node package + small refactoring Unit tests for node package + small refactoring & fixes Mar 29, 2023
@iulianpascalau iulianpascalau self-assigned this Mar 29, 2023
@iulianpascalau iulianpascalau marked this pull request as ready for review March 29, 2023 13:01
}
}

func (trigger *applicationRunningTrigger) Write(p []byte) (n int, err error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added, thanks

// src
// +- file2
// +- dir1
// +- file3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@raduchis raduchis self-requested a review March 30, 2023 07:09
raduchis
raduchis previously approved these changes Mar 30, 2023
err := logger.AddLogObserver(trigger, &logger.PlainFormatter{})
require.Nil(t, err)

// start a go routine that will send the SIGINT message after 1 minute
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after 1 second (after the application started)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, indeed, rotten comment.

raduchis
raduchis previously approved these changes Mar 30, 2023
@sstanculeanu sstanculeanu self-requested a review March 30, 2023 10:25
for k, v := range expectedValues {
assert.Equal(t, v, keys[k], fmt.Sprintf("for key %s", k))
}
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addition 3: extra tests here

	t.Run("should work - metachain", func(t *testing.T) {
		t.Parallel()

		keys := make(map[string]interface{})
		localStatusHandler := &statusHandler.AppStatusHandlerStub{
			SetUInt64ValueHandler: func(key string, value uint64) {
				keys[key] = value
			},
			SetStringValueHandler: func(key string, value string) {
				keys[key] = value
			},
		}
		localShardCoordinator := &testscommon.ShardsCoordinatorMock{
			NoShards: 3,
			SelfIDCalled: func() uint32 {
				return common.MetachainShardId
			},
		}

		err := InitMetrics(localStatusHandler, pubkeyString, nodeType, localShardCoordinator, nodesSetup, version, economicsConfigs, roundsPerEpoch, minTransactionVersion)
		assert.Nil(t, err)

		expectedValues := map[string]interface{}{
			common.MetricPublicKeyBlockSign:           pubkeyString,
			common.MetricShardId:                      uint64(localShardCoordinator.SelfId()),
			common.MetricNumShardsWithoutMetachain:    uint64(localShardCoordinator.NoShards),
			common.MetricNodeType:                     string(nodeType),
			common.MetricRoundTime:                    uint64(6),
			common.MetricAppVersion:                   version,
			common.MetricRoundsPerEpoch:               uint64(roundsPerEpoch),
			common.MetricCrossCheckBlockHeight:        "0",
			common.MetricCrossCheckBlockHeight + "_0": uint64(0),
			common.MetricCrossCheckBlockHeight + "_1": uint64(0),
			common.MetricCrossCheckBlockHeight + "_2": uint64(0),
			common.MetricCrossCheckBlockHeightMeta:    uint64(0),
			common.MetricIsSyncing:                    uint64(1),
			common.MetricLeaderPercentage:             fmt.Sprintf("%f", 2.0),
			common.MetricDenomination:                 uint64(4),
			common.MetricShardConsensusGroupSize:      uint64(63),
			common.MetricMetaConsensusGroupSize:       uint64(400),
			common.MetricNumNodesPerShard:             uint64(402),
			common.MetricNumMetachainNodes:            uint64(401),
			common.MetricStartTime:                    uint64(111111),
			common.MetricRoundDuration:                uint64(6000),
			common.MetricMinTransactionVersion:        uint64(1),
			common.MetricNumValidators:                uint64(0),
			common.MetricConsensusGroupSize:           uint64(400),
		}

		assert.Equal(t, len(expectedValues), len(keys))
		for k, v := range expectedValues {
			assert.Equal(t, v, keys[k], fmt.Sprintf("for key %s", k))
		}
	})
	t.Run("should work - invalid shard id", func(t *testing.T) {
		t.Parallel()

		keys := make(map[string]interface{})
		localStatusHandler := &statusHandler.AppStatusHandlerStub{
			SetUInt64ValueHandler: func(key string, value uint64) {
				keys[key] = value
			},
			SetStringValueHandler: func(key string, value string) {
				keys[key] = value
			},
		}
		localShardCoordinator := &testscommon.ShardsCoordinatorMock{
			NoShards: 3,
			SelfIDCalled: func() uint32 {
				return 10
			},
		}

		err := InitMetrics(localStatusHandler, pubkeyString, nodeType, localShardCoordinator, nodesSetup, version, economicsConfigs, roundsPerEpoch, minTransactionVersion)
		assert.Nil(t, err)

		assert.Equal(t, uint64(0), keys[common.MetricConsensusGroupSize])
	})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added, thanks

Comment on lines +385 to +387
{
LeaderPercentage: 2,
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addition 2: extra entry here:

Suggested change
{
LeaderPercentage: 2,
},
{
LeaderPercentage: 2,
},
{
LeaderPercentage: 2,
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@@ -329,3 +332,185 @@ func TestInitRatingsMetrics(t *testing.T) {
assert.Equal(t, v, keys[k])
}
}

func TestInitMetrics(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% coverage can be reached on this component using the next 3 additions:
addition 1:

  • on L140, as part of TestInitConfigMetrics, add MaxNodesChangeEnableEpoch
		MaxNodesChangeEnableEpoch: []config.MaxNodesChangeConfig{
				{
					EpochEnable:            0,
					MaxNumNodes:            1,
					NodesToShufflePerShard: 2,
				},
			},

then on L190 we'll need new expected metrics:

		"erd_max_nodes_change_enable_epoch0_epoch_enable":               uint32(0),
		"erd_max_nodes_change_enable_epoch0_max_num_nodes":              uint32(1),
		"erd_max_nodes_change_enable_epoch0_nodes_to_shuffle_per_shard": uint32(2),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added, thanks.

)

// these exceptions appear because the delayedComponent prevented the call of the first 2 components
// as the closable components are called in revered order
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// as the closable components are called in revered order
// as the closable components are called in reversed order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

)

// these exceptions appear because the delayedComponent prevented the call of the first 2 components
// as the closable components are called in revered order
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// as the closable components are called in revered order
// as the closable components are called in reversed order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

assert.Equal(tb, numKeys, len(closedCalled))
}

func contains(needle string, haystack []string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

sstanculeanu
sstanculeanu previously approved these changes Mar 30, 2023
raduchis
raduchis previously approved these changes Mar 30, 2023
gabi-vuls
gabi-vuls previously approved these changes Mar 31, 2023
Copy link
Contributor

@gabi-vuls gabi-vuls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System test passed.
@@ Log scanner @@

add-unit-tests-for-node-package

================================================================================

  • Known Warnings 14
  • New Warnings 2
  • Known Errors 0
  • New Errors 1
  • Panics 0
    ================================================================================
  • block hash does not match 10938
  • wrong nonce in block 3558
  • miniblocks does not match 0
  • num miniblocks does not match 0
  • miniblock hash does not match 0
  • block bodies does not match 0
  • receipts hash missmatch 0
    ================================================================================
  • No jailed nodes on the thestnet
    ================================================================================

Copy link
Contributor Author

@iulianpascalau iulianpascalau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored some tests due to this comment: #5141 (comment)

@@ -216,3 +216,18 @@ func TestFeeComputer_InHighConcurrency(t *testing.T) {

wg.Wait()
}

func TestFullHistoryPruningStorer_IsInterfaceNil(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func TestFullHistoryPruningStorer_IsInterfaceNil(t *testing.T) {
func TestFeeComputer_IsInterfaceNil(t *testing.T) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too much copy-paste :D
Thanks.

sstanculeanu
sstanculeanu previously approved these changes Mar 31, 2023
t.Parallel()

var lf *logsFacade
require.True(t, check.IfNil(lf))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.True(t, check.IfNil(lf))
require.True(t, lf.IsInterfaceNil())

Copy link
Contributor Author

@iulianpascalau iulianpascalau Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done on the whole package
Kept check.IfNil call whenever the function under test returned an interface (compatible with NilInterfaceChecker).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!

PubKeyConverter: testscommon.NewPubkeyConverterMock(32),
}
lf, _ = NewLogsFacade(arguments)
require.False(t, check.IfNil(lf))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.False(t, check.IfNil(lf))
require.False(t, lf.IsInterfaceNil())

@iulianpascalau iulianpascalau changed the title Unit tests for node package + small refactoring & fixes Unit tests for node package + small refactoring & fixes Mar 31, 2023
@iulianpascalau iulianpascalau merged commit 31f14ca into rc/v1.5.0 Apr 3, 2023
@iulianpascalau iulianpascalau deleted the add-unit-tests-for-node-package branch April 3, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants