diff options
Diffstat (limited to 'tests/plugins')
-rw-r--r-- | tests/plugins/http/http_plugin_test.go | 8 | ||||
-rw-r--r-- | tests/plugins/informer/informer_test.go | 4 | ||||
-rw-r--r-- | tests/plugins/informer/test_plugin.go | 16 | ||||
-rw-r--r-- | tests/plugins/service/configs/.rr-service-error.yaml | 16 | ||||
-rw-r--r-- | tests/plugins/service/configs/.rr-service-init.yaml | 22 | ||||
-rw-r--r-- | tests/plugins/service/configs/.rr-service-restarts.yaml | 16 | ||||
-rw-r--r-- | tests/plugins/service/service_plugin_test.go | 255 | ||||
-rw-r--r-- | tests/plugins/service/test_files/loop.php | 6 | ||||
-rwxr-xr-x | tests/plugins/service/test_files/test_binary | bin | 0 -> 1363968 bytes |
9 files changed, 334 insertions, 9 deletions
diff --git a/tests/plugins/http/http_plugin_test.go b/tests/plugins/http/http_plugin_test.go index 51c5fda2..73d6d102 100644 --- a/tests/plugins/http/http_plugin_test.go +++ b/tests/plugins/http/http_plugin_test.go @@ -21,13 +21,13 @@ import ( endure "github.com/spiral/endure/pkg/container" goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2/pkg/events" + "github.com/spiral/roadrunner/v2/pkg/process" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/informer" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/resetter" "github.com/spiral/roadrunner/v2/plugins/server" "github.com/spiral/roadrunner/v2/tests/mocks" - "github.com/spiral/roadrunner/v2/tools" "github.com/yookoala/gofast" httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" @@ -288,7 +288,7 @@ func informerTest(t *testing.T) { // WorkerList contains list of workers. list := struct { // Workers is list of workers. - Workers []tools.ProcessState `json:"workers"` + Workers []process.State `json:"workers"` }{} err = client.Call("informer.Workers", "http", &list) @@ -1336,7 +1336,7 @@ func informerTestBefore(t *testing.T) { // WorkerList contains list of workers. list := struct { // Workers is list of workers. - Workers []tools.ProcessState `json:"workers"` + Workers []process.State `json:"workers"` }{} err = client.Call("informer.Workers", "http", &list) @@ -1353,7 +1353,7 @@ func informerTestAfter(t *testing.T) { // WorkerList contains list of workers. list := struct { // Workers is list of workers. - Workers []tools.ProcessState `json:"workers"` + Workers []process.State `json:"workers"` }{} assert.NotZero(t, workerPid) diff --git a/tests/plugins/informer/informer_test.go b/tests/plugins/informer/informer_test.go index 31e14ff4..b6f50fd5 100644 --- a/tests/plugins/informer/informer_test.go +++ b/tests/plugins/informer/informer_test.go @@ -12,12 +12,12 @@ import ( endure "github.com/spiral/endure/pkg/container" goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" + "github.com/spiral/roadrunner/v2/pkg/process" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/informer" "github.com/spiral/roadrunner/v2/plugins/logger" rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" "github.com/spiral/roadrunner/v2/plugins/server" - "github.com/spiral/roadrunner/v2/tools" "github.com/stretchr/testify/assert" ) @@ -100,7 +100,7 @@ func informerWorkersRPCTest(t *testing.T) { // WorkerList contains list of workers. list := struct { // Workers is list of workers. - Workers []tools.ProcessState `json:"workers"` + Workers []process.State `json:"workers"` }{} err = client.Call("informer.Workers", "informer.plugin1", &list) diff --git a/tests/plugins/informer/test_plugin.go b/tests/plugins/informer/test_plugin.go index 8a1fb933..0c9065a3 100644 --- a/tests/plugins/informer/test_plugin.go +++ b/tests/plugins/informer/test_plugin.go @@ -5,7 +5,7 @@ import ( "time" "github.com/spiral/roadrunner/v2/pkg/pool" - "github.com/spiral/roadrunner/v2/pkg/worker" + "github.com/spiral/roadrunner/v2/pkg/process" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/server" ) @@ -49,11 +49,21 @@ func (p1 *Plugin1) Name() string { return "informer.plugin1" } -func (p1 *Plugin1) Workers() []worker.BaseProcess { +func (p1 *Plugin1) Workers() []process.State { p, err := p1.server.NewWorkerPool(context.Background(), testPoolConfig, nil) if err != nil { panic(err) } - return p.Workers() + ps := make([]process.State, 0, len(p.Workers())) + workers := p.Workers() + for i := 0; i < len(workers); i++ { + state, err := process.WorkerProcessState(workers[i]) + if err != nil { + return nil + } + ps = append(ps, state) + } + + return ps } diff --git a/tests/plugins/service/configs/.rr-service-error.yaml b/tests/plugins/service/configs/.rr-service-error.yaml new file mode 100644 index 00000000..3b0f1eb9 --- /dev/null +++ b/tests/plugins/service/configs/.rr-service-error.yaml @@ -0,0 +1,16 @@ +service: + some_service_1: + command: "php test_files/loopo.php" + process_num: 1 + exec_timeout: 5s # s,m,h (seconds, minutes, hours) + remain_after_exit: true + restart_sec: 1 + +logs: + level: info + mode: raw + +endure: + grace_period: 120s + print_graph: false + log_level: error diff --git a/tests/plugins/service/configs/.rr-service-init.yaml b/tests/plugins/service/configs/.rr-service-init.yaml new file mode 100644 index 00000000..e32f2eda --- /dev/null +++ b/tests/plugins/service/configs/.rr-service-init.yaml @@ -0,0 +1,22 @@ +service: + some_service_1: + command: "php test_files/loop.php" + process_num: 1 + exec_timeout: 5s # s,m,h (seconds, minutes, hours) + remain_after_exit: true + restart_sec: 1 + some_service_2: + command: "test_files/test_binary" + process_num: 1 + remain_after_exit: true + restart_delay: 1s + exec_timeout: 5s + +logs: + level: info + mode: raw + +endure: + grace_period: 120s + print_graph: false + log_level: error diff --git a/tests/plugins/service/configs/.rr-service-restarts.yaml b/tests/plugins/service/configs/.rr-service-restarts.yaml new file mode 100644 index 00000000..9095a92f --- /dev/null +++ b/tests/plugins/service/configs/.rr-service-restarts.yaml @@ -0,0 +1,16 @@ +service: + some_service_2: + command: "test_files/test_binary" + process_num: 1 + remain_after_exit: true + restart_delay: 1s + exec_timeout: 1s + +logs: + level: info + mode: raw + +endure: + grace_period: 120s + print_graph: false + log_level: error diff --git a/tests/plugins/service/service_plugin_test.go b/tests/plugins/service/service_plugin_test.go new file mode 100644 index 00000000..2c4e53fe --- /dev/null +++ b/tests/plugins/service/service_plugin_test.go @@ -0,0 +1,255 @@ +package service + +import ( + "os" + "os/signal" + "sync" + "syscall" + "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/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.ErrorLevel)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-service-init.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() + 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, + 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() +} diff --git a/tests/plugins/service/test_files/loop.php b/tests/plugins/service/test_files/loop.php new file mode 100644 index 00000000..6ba488ef --- /dev/null +++ b/tests/plugins/service/test_files/loop.php @@ -0,0 +1,6 @@ +<?php +for ($x = 0; $x <= 1000; $x++) { + sleep(1); + error_log("The number is: $x", 0); +} +?> diff --git a/tests/plugins/service/test_files/test_binary b/tests/plugins/service/test_files/test_binary Binary files differnew file mode 100755 index 00000000..480fb7e2 --- /dev/null +++ b/tests/plugins/service/test_files/test_binary |