From 1174e03e6638614940bc940d58bff77a6393ca2b Mon Sep 17 00:00:00 2001 From: Kirill Date: Wed, 16 Oct 2024 17:46:45 +0400 Subject: [PATCH 1/2] Change logic in plugin.Service, rename junoplugin package to plugin --- node/node.go | 10 ++++------ plugin/plugin.go | 36 +++--------------------------------- plugin/plugin_test.go | 2 +- plugin/service.go | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 40 deletions(-) create mode 100644 plugin/service.go diff --git a/node/node.go b/node/node.go index 96d94a182d..ccb316c84d 100644 --- a/node/node.go +++ b/node/node.go @@ -22,7 +22,7 @@ import ( "github.com/NethermindEth/juno/l1" "github.com/NethermindEth/juno/migration" "github.com/NethermindEth/juno/p2p" - junoplugin "github.com/NethermindEth/juno/plugin" + "github.com/NethermindEth/juno/plugin" "github.com/NethermindEth/juno/rpc" "github.com/NethermindEth/juno/service" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" @@ -159,15 +159,13 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen synchronizer := sync.New(chain, adaptfeeder.New(client), log, cfg.PendingPollInterval, dbIsRemote) gatewayClient := gateway.NewClient(cfg.Network.GatewayURL, log).WithUserAgent(ua).WithAPIKey(cfg.GatewayAPIKey) - pluginService := junoplugin.New(log) if cfg.PluginPath != "" { - plugin, err := junoplugin.Load(cfg.PluginPath) + p, err := plugin.Load(cfg.PluginPath) if err != nil { return nil, err } - synchronizer.WithPlugin(plugin) - pluginService.WithPlugin(plugin) - services = append(services, pluginService) + synchronizer.WithPlugin(p) + services = append(services, plugin.NewService(p)) } var p2pService *p2p.Service diff --git a/plugin/plugin.go b/plugin/plugin.go index cb18f70343..8dd9ee13d2 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -1,43 +1,13 @@ -package junoplugin +package plugin import ( - "context" "fmt" - "plugin" - "sync" + stdplugin "plugin" "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/utils" ) -type PluginService struct { - jPlugin JunoPlugin - wg sync.WaitGroup - log utils.SimpleLogger -} - -func New(log utils.SimpleLogger) *PluginService { - return &PluginService{wg: sync.WaitGroup{}, log: log} -} - -func (p *PluginService) WithPlugin(jPlugin JunoPlugin) { - p.jPlugin = jPlugin -} - -func (p *PluginService) Run(ctx context.Context) error { - p.wg.Add(1) - go func() { - defer p.wg.Done() - <-ctx.Done() - if err := p.jPlugin.Shutdown(); err != nil { - p.log.Errorw("Error while calling plugin Shutdown() function", "err", err) - } - }() - p.wg.Wait() - return nil -} - //go:generate mockgen -destination=../mocks/mock_plugin.go -package=mocks github.com/NethermindEth/juno/plugin JunoPlugin type JunoPlugin interface { Init() error @@ -54,7 +24,7 @@ type BlockAndStateUpdate struct { } func Load(pluginPath string) (JunoPlugin, error) { - plug, err := plugin.Open(pluginPath) + plug, err := stdplugin.Open(pluginPath) if err != nil { return nil, fmt.Errorf("error loading plugin .so file: %w", err) } diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 54f9cffe17..3933835ae7 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -1,4 +1,4 @@ -package junoplugin_test +package plugin_test import ( "context" diff --git a/plugin/service.go b/plugin/service.go new file mode 100644 index 0000000000..907feefa9e --- /dev/null +++ b/plugin/service.go @@ -0,0 +1,19 @@ +package plugin + +import "context" + +// Service provides an abstraction for signalling the plugin to shut down. +type Service struct { + plugin JunoPlugin +} + +func NewService(plugin JunoPlugin) *Service { + return &Service{ + plugin: plugin, + } +} + +func (p *Service) Run(ctx context.Context) error { + <-ctx.Done() + return p.plugin.Shutdown() +} From da27a9764d24b9f71455cd8b0115dd7ab86b03ed Mon Sep 17 00:00:00 2001 From: Kirill Date: Wed, 16 Oct 2024 19:02:13 +0400 Subject: [PATCH 2/2] Add test for plugin service --- plugin/service_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 plugin/service_test.go diff --git a/plugin/service_test.go b/plugin/service_test.go new file mode 100644 index 0000000000..786cfc6a61 --- /dev/null +++ b/plugin/service_test.go @@ -0,0 +1,45 @@ +package plugin_test + +import ( + "context" + "errors" + "testing" + + "github.com/NethermindEth/juno/mocks" + "github.com/NethermindEth/juno/plugin" + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" +) + +func TestService(t *testing.T) { + t.Run("shutdown ok", func(t *testing.T) { + ctrl := gomock.NewController(t) + + p := mocks.NewMockJunoPlugin(ctrl) + p.EXPECT().Shutdown().Return(nil) + service := plugin.NewService(p) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + // after ^ this ctx already cancelled + + err := service.Run(ctx) + require.NoError(t, err) + }) + t.Run("shutdown with error", func(t *testing.T) { + ctrl := gomock.NewController(t) + + shutdownErr := errors.New("error during shutdown") + + p := mocks.NewMockJunoPlugin(ctrl) + p.EXPECT().Shutdown().Return(shutdownErr) + service := plugin.NewService(p) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + // after ^ this ctx already cancelled + + err := service.Run(ctx) + require.Equal(t, shutdownErr, err) + }) +}