diff options
Diffstat (limited to 'tests/plugins/service/service_plugin_test.go')
-rw-r--r-- | tests/plugins/service/service_plugin_test.go | 180 |
1 files changed, 177 insertions, 3 deletions
diff --git a/tests/plugins/service/service_plugin_test.go b/tests/plugins/service/service_plugin_test.go index add6d374..2c4e53fe 100644 --- a/tests/plugins/service/service_plugin_test.go +++ b/tests/plugins/service/service_plugin_test.go @@ -8,15 +8,16 @@ import ( "testing" "time" + "github.com/golang/mock/gomock" endure "github.com/spiral/endure/pkg/container" "github.com/spiral/roadrunner/v2/plugins/config" - "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/service" + "github.com/spiral/roadrunner/v2/tests/mocks" "github.com/stretchr/testify/assert" ) func TestServiceInit(t *testing.T) { - cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel)) + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) assert.NoError(t, err) cfg := &config.Viper{ @@ -24,9 +25,182 @@ func TestServiceInit(t *testing.T) { Prefix: "rr", } + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("worker destructed", "pid", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Info("The number is: 0\n").MinTimes(1) + mockLogger.EXPECT().Info("The number is: 1\n").MinTimes(1) + mockLogger.EXPECT().Info("The number is: 2\n").MinTimes(1) + mockLogger.EXPECT().Info("The number is: 3\n").MinTimes(1) + mockLogger.EXPECT().Info("The number is: 4\n").AnyTimes() + + // process interrupt error + mockLogger.EXPECT().Error("process wait error", gomock.Any()).MinTimes(2) + + mockLogger.EXPECT().Info("Hello 0\n The number is: 0\n").MinTimes(1) + mockLogger.EXPECT().Info("Hello 1\n The number is: 1\n").MinTimes(1) + mockLogger.EXPECT().Info("Hello 2\n The number is: 2\n").MinTimes(1) + mockLogger.EXPECT().Info("Hello 3\n The number is: 3\n").MinTimes(1) + + mockLogger.EXPECT().Info("Hello 0").MinTimes(1) + mockLogger.EXPECT().Info("Hello 1").MinTimes(1) + mockLogger.EXPECT().Info("Hello 2").MinTimes(1) + mockLogger.EXPECT().Info("Hello 3").MinTimes(1) + mockLogger.EXPECT().Info("Hello 4").AnyTimes() + + err = cont.RegisterAll( + cfg, + mockLogger, + &service.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + stopCh := make(chan struct{}, 1) + + go func() { + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-stopCh: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + time.Sleep(time.Second * 10) + stopCh <- struct{}{} + wg.Wait() +} + +func TestServiceError(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-service-error.yaml", + Prefix: "rr", + } + + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("worker destructed", "pid", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes() + + // process interrupt error + mockLogger.EXPECT().Error("process wait error", gomock.Any()).MinTimes(2) + + err = cont.RegisterAll( + cfg, + mockLogger, + &service.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + assert.NoError(t, err) + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + stopCh := make(chan struct{}, 1) + + go func() { + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-stopCh: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + time.Sleep(time.Second * 10) + stopCh <- struct{}{} + wg.Wait() +} + +func TestServiceRestarts(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-service-restarts.yaml", + Prefix: "rr", + } + + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("worker destructed", "pid", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes() + + // process interrupt error + mockLogger.EXPECT().Error("process wait error", gomock.Any()).MinTimes(2) + + // should not be more than Hello 0, because of restarts + mockLogger.EXPECT().Info("Hello 0").MinTimes(1) + err = cont.RegisterAll( cfg, - &logger.ZapLogger{}, + mockLogger, &service.Plugin{}, ) assert.NoError(t, err) |