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

Small restructure of plugin #2221

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"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"
Expand Down Expand Up @@ -159,15 +159,13 @@
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)

Check warning on line 163 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L163

Added line #L163 was not covered by tests
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))

Check warning on line 168 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L167-L168

Added lines #L167 - L168 were not covered by tests
}

var p2pService *p2p.Service
Expand Down
36 changes: 3 additions & 33 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -54,7 +24,7 @@
}

func Load(pluginPath string) (JunoPlugin, error) {
plug, err := plugin.Open(pluginPath)
plug, err := stdplugin.Open(pluginPath)

Check warning on line 27 in plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

plugin/plugin.go#L27

Added line #L27 was not covered by tests
if err != nil {
return nil, fmt.Errorf("error loading plugin .so file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package junoplugin_test
package plugin_test

import (
"context"
Expand Down
19 changes: 19 additions & 0 deletions plugin/service.go
Original file line number Diff line number Diff line change
@@ -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()
}
45 changes: 45 additions & 0 deletions plugin/service_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}