Skip to content

Commit

Permalink
Merge pull request #5985 from multiversx/more_chainSimulator_tests_p2
Browse files Browse the repository at this point in the history
More chain simulator tests part 2
  • Loading branch information
sstanculeanu authored Feb 26, 2024
2 parents d3489af + 4493a69 commit c113d89
Show file tree
Hide file tree
Showing 15 changed files with 866 additions and 43 deletions.
2 changes: 1 addition & 1 deletion factory/api/apiResolverFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func createMockArgs(t *testing.T) *api.ApiResolverArgs {
cryptoComponents := componentsMock.GetCryptoComponents(coreComponents)
networkComponents := componentsMock.GetNetworkComponents(cryptoComponents)
dataComponents := componentsMock.GetDataComponents(coreComponents, shardCoordinator)
stateComponents := componentsMock.GetStateComponents(coreComponents)
stateComponents := componentsMock.GetStateComponents(coreComponents, componentsMock.GetStatusCoreComponents())
processComponents := componentsMock.GetProcessComponents(shardCoordinator, coreComponents, networkComponents, dataComponents, cryptoComponents, stateComponents)
argsB := componentsMock.GetBootStrapFactoryArgs()

Expand Down
2 changes: 1 addition & 1 deletion factory/processing/processComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func createMockProcessComponentsFactoryArgs() processComp.ProcessComponentsFacto
TxExecutionOrderHandler: &txExecOrderStub.TxExecutionOrderHandlerStub{},
}

args.State = components.GetStateComponents(args.CoreData)
args.State = components.GetStateComponents(args.CoreData, args.StatusCoreComponents)

return args
}
Expand Down
14 changes: 7 additions & 7 deletions factory/state/stateComponentsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNewManagedStateComponents(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
stateComponentsFactory, _ := stateComp.NewStateComponentsFactory(args)
managedStateComponents, err := stateComp.NewManagedStateComponents(stateComponentsFactory)
require.NoError(t, err)
Expand All @@ -42,7 +42,7 @@ func TestManagedStateComponents_Create(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
stateComponentsFactory, _ := stateComp.NewStateComponentsFactory(args)
managedStateComponents, err := stateComp.NewManagedStateComponents(stateComponentsFactory)
require.NoError(t, err)
Expand All @@ -56,7 +56,7 @@ func TestManagedStateComponents_Create(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
stateComponentsFactory, _ := stateComp.NewStateComponentsFactory(args)
managedStateComponents, err := stateComp.NewManagedStateComponents(stateComponentsFactory)
require.NoError(t, err)
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestManagedStateComponents_Close(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
stateComponentsFactory, _ := stateComp.NewStateComponentsFactory(args)
managedStateComponents, _ := stateComp.NewManagedStateComponents(stateComponentsFactory)
require.NoError(t, managedStateComponents.Close())
Expand All @@ -102,7 +102,7 @@ func TestManagedStateComponents_CheckSubcomponents(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
stateComponentsFactory, _ := stateComp.NewStateComponentsFactory(args)
managedStateComponents, _ := stateComp.NewManagedStateComponents(stateComponentsFactory)
err := managedStateComponents.CheckSubcomponents()
Expand All @@ -121,7 +121,7 @@ func TestManagedStateComponents_Setters(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
stateComponentsFactory, _ := stateComp.NewStateComponentsFactory(args)
managedStateComponents, _ := stateComp.NewManagedStateComponents(stateComponentsFactory)
err := managedStateComponents.Create()
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestManagedStateComponents_IsInterfaceNil(t *testing.T) {
require.True(t, managedStateComponents.IsInterfaceNil())

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
stateComponentsFactory, _ := stateComp.NewStateComponentsFactory(args)
managedStateComponents, _ = stateComp.NewManagedStateComponents(stateComponentsFactory)
require.False(t, managedStateComponents.IsInterfaceNil())
Expand Down
18 changes: 9 additions & 9 deletions factory/state/stateComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNewStateComponentsFactory(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
args.Core = nil

scf, err := stateComp.NewStateComponentsFactory(args)
Expand All @@ -31,7 +31,7 @@ func TestNewStateComponentsFactory(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
args.StatusCore = nil

scf, err := stateComp.NewStateComponentsFactory(args)
Expand All @@ -42,7 +42,7 @@ func TestNewStateComponentsFactory(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())

scf, err := stateComp.NewStateComponentsFactory(args)
require.NoError(t, err)
Expand All @@ -57,7 +57,7 @@ func TestStateComponentsFactory_Create(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
coreCompStub := factory.NewCoreComponentsHolderStubFromRealComponent(args.Core)
coreCompStub.InternalMarshalizerCalled = func() marshal.Marshalizer {
return nil
Expand All @@ -73,7 +73,7 @@ func TestStateComponentsFactory_Create(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
args.Config.EvictionWaitingList.RootHashesSize = 0
scf, _ := stateComp.NewStateComponentsFactory(args)

Expand All @@ -85,7 +85,7 @@ func TestStateComponentsFactory_Create(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())

coreCompStub := factory.NewCoreComponentsHolderStubFromRealComponent(args.Core)
cnt := 0
Expand All @@ -107,7 +107,7 @@ func TestStateComponentsFactory_Create(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())

coreCompStub := factory.NewCoreComponentsHolderStubFromRealComponent(args.Core)
cnt := 0
Expand All @@ -129,7 +129,7 @@ func TestStateComponentsFactory_Create(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
scf, _ := stateComp.NewStateComponentsFactory(args)

sc, err := scf.Create()
Expand All @@ -143,7 +143,7 @@ func TestStateComponents_Close(t *testing.T) {
t.Parallel()

coreComponents := componentsMock.GetCoreComponents()
args := componentsMock.GetStateFactoryArgs(coreComponents)
args := componentsMock.GetStateFactoryArgs(coreComponents, componentsMock.GetStatusCoreComponents())
scf, _ := stateComp.NewStateComponentsFactory(args)

sc, err := scf.Create()
Expand Down
3 changes: 2 additions & 1 deletion node/chainSimulator/components/dataComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type dataComponentsHolder struct {
}

// CreateDataComponents will create the data components holder
func CreateDataComponents(args ArgsDataComponentsHolder) (factory.DataComponentsHandler, error) {
func CreateDataComponents(args ArgsDataComponentsHolder) (*dataComponentsHolder, error) {
miniBlockStorer, err := args.StorageService.GetStorer(dataRetriever.MiniBlockUnit)
if err != nil {
return nil, err
Expand Down Expand Up @@ -89,6 +89,7 @@ func (d *dataComponentsHolder) Clone() interface{} {
storageService: d.storageService,
dataPool: d.dataPool,
miniBlockProvider: d.miniBlockProvider,
closeHandler: d.closeHandler,
}
}

Expand Down
110 changes: 110 additions & 0 deletions node/chainSimulator/components/dataComponents_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package components

import (
"testing"

retriever "github.com/multiversx/mx-chain-go/dataRetriever"
chainStorage "github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/dataRetriever"
"github.com/multiversx/mx-chain-go/testscommon/storage"
"github.com/stretchr/testify/require"
)

func createArgsDataComponentsHolder() ArgsDataComponentsHolder {
return ArgsDataComponentsHolder{
Chain: &testscommon.ChainHandlerStub{},
StorageService: &storage.ChainStorerStub{
GetStorerCalled: func(unitType retriever.UnitType) (chainStorage.Storer, error) {
return &storage.StorerStub{}, nil
},
},
DataPool: &dataRetriever.PoolsHolderStub{
MiniBlocksCalled: func() chainStorage.Cacher {
return &testscommon.CacherStub{}
},
},
InternalMarshaller: &testscommon.MarshallerStub{},
}
}

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

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

comp, err := CreateDataComponents(createArgsDataComponentsHolder())
require.NoError(t, err)
require.NotNil(t, comp)

require.Nil(t, comp.Create())
require.Nil(t, comp.Close())
})
t.Run("NewMiniBlockProvider failure should error", func(t *testing.T) {
t.Parallel()

args := createArgsDataComponentsHolder()
args.DataPool = &dataRetriever.PoolsHolderStub{
MiniBlocksCalled: func() chainStorage.Cacher {
return nil
},
}
comp, err := CreateDataComponents(args)
require.Error(t, err)
require.Nil(t, comp)
})
t.Run("GetStorer failure should error", func(t *testing.T) {
t.Parallel()

args := createArgsDataComponentsHolder()
args.StorageService = &storage.ChainStorerStub{
GetStorerCalled: func(unitType retriever.UnitType) (chainStorage.Storer, error) {
return nil, expectedErr
},
}
comp, err := CreateDataComponents(args)
require.Equal(t, expectedErr, err)
require.Nil(t, comp)
})
}

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

var comp *dataComponentsHolder
require.True(t, comp.IsInterfaceNil())

comp, _ = CreateDataComponents(createArgsDataComponentsHolder())
require.False(t, comp.IsInterfaceNil())
require.Nil(t, comp.Close())
}

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

comp, err := CreateDataComponents(createArgsDataComponentsHolder())
require.NoError(t, err)

require.NotNil(t, comp.Blockchain())
require.Nil(t, comp.SetBlockchain(nil))
require.Nil(t, comp.Blockchain())
require.NotNil(t, comp.StorageService())
require.NotNil(t, comp.Datapool())
require.NotNil(t, comp.MiniBlocksProvider())
require.Nil(t, comp.CheckSubcomponents())
require.Empty(t, comp.String())
require.Nil(t, comp.Close())
}

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

comp, err := CreateDataComponents(createArgsDataComponentsHolder())
require.NoError(t, err)

compClone := comp.Clone()
require.Equal(t, comp, compClone)
require.False(t, comp == compClone) // pointer testing
require.Nil(t, comp.Close())
}
Loading

0 comments on commit c113d89

Please sign in to comment.