-
Notifications
You must be signed in to change notification settings - Fork 11
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
run type components implementation #312
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package runType | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var errNilRunTypeComponents = errors.New("nil run type components") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package runType | ||
|
||
// RunTypeComponentsCreator is the interface for creating run type components | ||
type RunTypeComponentsCreator interface { | ||
Create() *runTypeComponents | ||
IsInterfaceNil() bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For code coverage, also add check for
IsInterfaceNil