-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small restructure of plugin logic (#2221)
- Loading branch information
Showing
5 changed files
with
72 additions
and
40 deletions.
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package junoplugin_test | ||
package plugin_test | ||
|
||
import ( | ||
"context" | ||
|
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 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() | ||
} |
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,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) | ||
}) | ||
} |