From da27a9764d24b9f71455cd8b0115dd7ab86b03ed Mon Sep 17 00:00:00 2001 From: Kirill Date: Wed, 16 Oct 2024 19:02:13 +0400 Subject: [PATCH] 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) + }) +}