From 641a7d1215158c6e46e110ae315fb9a963e74108 Mon Sep 17 00:00:00 2001 From: axenteoctavian Date: Tue, 3 Dec 2024 16:52:24 +0200 Subject: [PATCH 1/2] run type components implementation --- factory/interface.go | 24 ++++++ factory/runType/errors.go | 7 ++ factory/runType/interface.go | 7 ++ factory/runType/runTypeComponents.go | 13 +++ factory/runType/runTypeComponentsFactory.go | 18 ++++ factory/runType/runTypeComponentsHandler.go | 83 +++++++++++++++++++ .../runType/runTypeComponentsHandler_test.go | 76 +++++++++++++++++ factory/runType/runTypeComponents_test.go | 38 +++++++++ .../sovereignRunTypeComponentsFactory.go | 18 ++++ .../sovereignRunTypeComponentsFactory_test.go | 19 +++++ 10 files changed, 303 insertions(+) create mode 100644 factory/interface.go create mode 100644 factory/runType/errors.go create mode 100644 factory/runType/interface.go create mode 100644 factory/runType/runTypeComponents.go create mode 100644 factory/runType/runTypeComponentsFactory.go create mode 100644 factory/runType/runTypeComponentsHandler.go create mode 100644 factory/runType/runTypeComponentsHandler_test.go create mode 100644 factory/runType/runTypeComponents_test.go create mode 100644 factory/runType/sovereignRunTypeComponentsFactory.go create mode 100644 factory/runType/sovereignRunTypeComponentsFactory_test.go diff --git a/factory/interface.go b/factory/interface.go new file mode 100644 index 00000000..ae3435e6 --- /dev/null +++ b/factory/interface.go @@ -0,0 +1,24 @@ +package factory + +// ComponentHandler defines the actions common to all component handlers +type ComponentHandler interface { + Create() error + Close() error + CheckSubcomponents() error + String() string +} + +// RunTypeComponentsHandler defines the run type components handler actions +type RunTypeComponentsHandler interface { + ComponentHandler + RunTypeComponentsHolder +} + +// RunTypeComponentsHolder holds the run type components +type RunTypeComponentsHolder interface { + Create() error + Close() error + CheckSubcomponents() error + String() string + IsInterfaceNil() bool +} diff --git a/factory/runType/errors.go b/factory/runType/errors.go new file mode 100644 index 00000000..657663a3 --- /dev/null +++ b/factory/runType/errors.go @@ -0,0 +1,7 @@ +package runType + +import ( + "errors" +) + +var errNilRunTypeComponents = errors.New("nil run type components") diff --git a/factory/runType/interface.go b/factory/runType/interface.go new file mode 100644 index 00000000..37448d92 --- /dev/null +++ b/factory/runType/interface.go @@ -0,0 +1,7 @@ +package runType + +// RunTypeComponentsCreator is the interface for creating run type components +type RunTypeComponentsCreator interface { + Create() *runTypeComponents + IsInterfaceNil() bool +} diff --git a/factory/runType/runTypeComponents.go b/factory/runType/runTypeComponents.go new file mode 100644 index 00000000..c960293b --- /dev/null +++ b/factory/runType/runTypeComponents.go @@ -0,0 +1,13 @@ +package runType + +type runTypeComponents struct{} + +// Close does nothing +func (rtc *runTypeComponents) Close() error { + return nil +} + +// IsInterfaceNil returns true if there is no value under the interface +func (rtc *runTypeComponents) IsInterfaceNil() bool { + return rtc == nil +} diff --git a/factory/runType/runTypeComponentsFactory.go b/factory/runType/runTypeComponentsFactory.go new file mode 100644 index 00000000..24ad9f56 --- /dev/null +++ b/factory/runType/runTypeComponentsFactory.go @@ -0,0 +1,18 @@ +package runType + +type runTypeComponentsFactory struct{} + +// NewRunTypeComponentsFactory will return a new instance of run type components factory +func NewRunTypeComponentsFactory() *runTypeComponentsFactory { + return &runTypeComponentsFactory{} +} + +// Create will create the run type components +func (rtcf *runTypeComponentsFactory) Create() *runTypeComponents { + return &runTypeComponents{} +} + +// IsInterfaceNil returns true if there is no value under the interface +func (rtcf *runTypeComponentsFactory) IsInterfaceNil() bool { + return rtcf == nil +} diff --git a/factory/runType/runTypeComponentsHandler.go b/factory/runType/runTypeComponentsHandler.go new file mode 100644 index 00000000..9b25b92b --- /dev/null +++ b/factory/runType/runTypeComponentsHandler.go @@ -0,0 +1,83 @@ +package runType + +import ( + "sync" + + "github.com/multiversx/mx-chain-core-go/core/check" + + "github.com/multiversx/mx-chain-es-indexer-go/factory" +) + +const runTypeComponentsName = "managedRunTypeComponents" + +var _ factory.ComponentHandler = (*managedRunTypeComponents)(nil) +var _ factory.RunTypeComponentsHandler = (*managedRunTypeComponents)(nil) +var _ factory.RunTypeComponentsHolder = (*managedRunTypeComponents)(nil) + +type managedRunTypeComponents struct { + *runTypeComponents + factory RunTypeComponentsCreator + mutRunTypeCoreComponents sync.RWMutex +} + +// NewManagedRunTypeComponents returns a news instance of managed runType core components +func NewManagedRunTypeComponents(rtc RunTypeComponentsCreator) (*managedRunTypeComponents, error) { + if rtc == nil { + return nil, errNilRunTypeComponents + } + + return &managedRunTypeComponents{ + runTypeComponents: nil, + factory: rtc, + }, nil +} + +// Create will create the managed components +func (mrtc *managedRunTypeComponents) Create() error { + rtc := mrtc.factory.Create() + + mrtc.mutRunTypeCoreComponents.Lock() + mrtc.runTypeComponents = rtc + mrtc.mutRunTypeCoreComponents.Unlock() + + return nil +} + +// Close will close all underlying subcomponents +func (mrtc *managedRunTypeComponents) Close() error { + mrtc.mutRunTypeCoreComponents.Lock() + defer mrtc.mutRunTypeCoreComponents.Unlock() + + if check.IfNil(mrtc.runTypeComponents) { + return nil + } + + err := mrtc.runTypeComponents.Close() + if err != nil { + return err + } + mrtc.runTypeComponents = nil + + return nil +} + +// CheckSubcomponents verifies all subcomponents +func (mrtc *managedRunTypeComponents) CheckSubcomponents() error { + mrtc.mutRunTypeCoreComponents.RLock() + defer mrtc.mutRunTypeCoreComponents.RUnlock() + + if check.IfNil(mrtc.runTypeComponents) { + return errNilRunTypeComponents + } + return nil +} + +// IsInterfaceNil returns true if the interface is nil +func (mrtc *managedRunTypeComponents) IsInterfaceNil() bool { + return mrtc == nil +} + +// String returns the name of the component +func (mrtc *managedRunTypeComponents) String() string { + return runTypeComponentsName +} diff --git a/factory/runType/runTypeComponentsHandler_test.go b/factory/runType/runTypeComponentsHandler_test.go new file mode 100644 index 00000000..573a73ff --- /dev/null +++ b/factory/runType/runTypeComponentsHandler_test.go @@ -0,0 +1,76 @@ +package runType + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/multiversx/mx-chain-es-indexer-go/factory" +) + +func createComponents() (factory.RunTypeComponentsHandler, error) { + rtcf := NewRunTypeComponentsFactory() + return NewManagedRunTypeComponents(rtcf) +} + +func TestNewManagedRunTypeComponents(t *testing.T) { + t.Parallel() + + t.Run("should error", func(t *testing.T) { + managedRunTypeComponents, err := NewManagedRunTypeComponents(nil) + require.ErrorIs(t, err, errNilRunTypeComponents) + require.Nil(t, managedRunTypeComponents) + }) + t.Run("should work", func(t *testing.T) { + rtcf := NewRunTypeComponentsFactory() + managedRunTypeComponents, err := NewManagedRunTypeComponents(rtcf) + require.NoError(t, err) + require.False(t, managedRunTypeComponents.IsInterfaceNil()) + }) +} + +func TestManagedRunTypeComponents_Create(t *testing.T) { + t.Parallel() + + t.Run("should work with getters", func(t *testing.T) { + t.Parallel() + + managedRunTypeComponents, err := createComponents() + require.NoError(t, err) + + err = managedRunTypeComponents.Create() + require.NoError(t, err) + + require.Equal(t, runTypeComponentsName, managedRunTypeComponents.String()) + require.NoError(t, managedRunTypeComponents.Close()) + }) +} + +func TestManagedRunTypeComponents_Close(t *testing.T) { + t.Parallel() + + managedRunTypeComponents, _ := createComponents() + require.NoError(t, managedRunTypeComponents.Close()) + + err := managedRunTypeComponents.Create() + require.NoError(t, err) + + require.NoError(t, managedRunTypeComponents.Close()) +} + +func TestManagedRunTypeComponents_CheckSubcomponents(t *testing.T) { + t.Parallel() + + managedRunTypeComponents, _ := createComponents() + err := managedRunTypeComponents.CheckSubcomponents() + require.Equal(t, errNilRunTypeComponents, err) + + err = managedRunTypeComponents.Create() + require.NoError(t, err) + + //TODO check for nil each subcomponent - MX-15371 + err = managedRunTypeComponents.CheckSubcomponents() + require.NoError(t, err) + + require.NoError(t, managedRunTypeComponents.Close()) +} diff --git a/factory/runType/runTypeComponents_test.go b/factory/runType/runTypeComponents_test.go new file mode 100644 index 00000000..07580802 --- /dev/null +++ b/factory/runType/runTypeComponents_test.go @@ -0,0 +1,38 @@ +package runType + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewRunTypeComponentsFactory(t *testing.T) { + t.Parallel() + + t.Run("should work", func(t *testing.T) { + rtc := NewRunTypeComponentsFactory() + require.NotNil(t, rtc) + }) +} + +func TestRunTypeComponentsFactory_Create(t *testing.T) { + t.Parallel() + + rtcf := NewRunTypeComponentsFactory() + require.NotNil(t, rtcf) + + rtc := rtcf.Create() + require.NotNil(t, rtc) +} + +func TestRunTypeComponentsFactory_Close(t *testing.T) { + t.Parallel() + + rtcf := NewRunTypeComponentsFactory() + require.NotNil(t, rtcf) + + rtc := rtcf.Create() + require.NotNil(t, rtc) + + require.NoError(t, rtc.Close()) +} diff --git a/factory/runType/sovereignRunTypeComponentsFactory.go b/factory/runType/sovereignRunTypeComponentsFactory.go new file mode 100644 index 00000000..fd5226a5 --- /dev/null +++ b/factory/runType/sovereignRunTypeComponentsFactory.go @@ -0,0 +1,18 @@ +package runType + +type sovereignRunTypeComponentsFactory struct{} + +// NewSovereignRunTypeComponentsFactory will return a new instance of sovereign run type components factory +func NewSovereignRunTypeComponentsFactory() *sovereignRunTypeComponentsFactory { + return &sovereignRunTypeComponentsFactory{} +} + +// Create will create the run type components +func (srtcf *sovereignRunTypeComponentsFactory) Create() *runTypeComponents { + return &runTypeComponents{} +} + +// IsInterfaceNil returns true if there is no value under the interface +func (srtcf *sovereignRunTypeComponentsFactory) IsInterfaceNil() bool { + return srtcf == nil +} diff --git a/factory/runType/sovereignRunTypeComponentsFactory_test.go b/factory/runType/sovereignRunTypeComponentsFactory_test.go new file mode 100644 index 00000000..a3e959b4 --- /dev/null +++ b/factory/runType/sovereignRunTypeComponentsFactory_test.go @@ -0,0 +1,19 @@ +package runType + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSovereignRunTypeComponentsFactory_CreateAndClose(t *testing.T) { + t.Parallel() + + srtcf := NewSovereignRunTypeComponentsFactory() + require.NotNil(t, srtcf) + + srtc := srtcf.Create() + require.NotNil(t, srtc) + + require.NoError(t, srtc.Close()) +} From 7ee36158cc402a3f42a0061e632a707af0d8e25d Mon Sep 17 00:00:00 2001 From: axenteoctavian Date: Fri, 6 Dec 2024 12:17:28 +0200 Subject: [PATCH 2/2] fix after review --- factory/runType/sovereignRunTypeComponentsFactory_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/factory/runType/sovereignRunTypeComponentsFactory_test.go b/factory/runType/sovereignRunTypeComponentsFactory_test.go index a3e959b4..71d909ba 100644 --- a/factory/runType/sovereignRunTypeComponentsFactory_test.go +++ b/factory/runType/sovereignRunTypeComponentsFactory_test.go @@ -10,7 +10,7 @@ func TestSovereignRunTypeComponentsFactory_CreateAndClose(t *testing.T) { t.Parallel() srtcf := NewSovereignRunTypeComponentsFactory() - require.NotNil(t, srtcf) + require.False(t, srtcf.IsInterfaceNil()) srtc := srtcf.Create() require.NotNil(t, srtc)