summaryrefslogtreecommitdiff
path: root/tests/plugins/service
diff options
context:
space:
mode:
Diffstat (limited to 'tests/plugins/service')
-rw-r--r--tests/plugins/service/configs/.rr-service-init.yaml21
-rw-r--r--tests/plugins/service/php/loop.php6
-rw-r--r--tests/plugins/service/service_plugin_test.go81
3 files changed, 108 insertions, 0 deletions
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..9ca0bde5
--- /dev/null
+++ b/tests/plugins/service/configs/.rr-service-init.yaml
@@ -0,0 +1,21 @@
+service:
+ some_service_1: #<-- user defined name
+ command: "php php/loop.php" # command, can be any command (php, script, binary, exe)"
+ process_num: 10 # number of copies (processes)
+ exec_timeout: 5s # how long process allowed to run (until restart or stop), default - unlimited
+ restart_after_exit: false # run and restart after finish
+ restart_delay: 1s # delay between binary/script restarts, default 1minute
+# some_service_2: # exec_timeout is not set, by default - unlimited
+# command: "./test_binary"
+# process_num: 1
+# restart_after_exit: false
+# restart_delay: 10 # <--- ignored when restart_after_exit is false
+
+logs:
+ level: debug
+ mode: raw
+
+endure:
+ grace_period: 120s
+ print_graph: false
+ log_level: debug
diff --git a/tests/plugins/service/php/loop.php b/tests/plugins/service/php/loop.php
new file mode 100644
index 00000000..6ba488ef
--- /dev/null
+++ b/tests/plugins/service/php/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/service_plugin_test.go b/tests/plugins/service/service_plugin_test.go
new file mode 100644
index 00000000..add6d374
--- /dev/null
+++ b/tests/plugins/service/service_plugin_test.go
@@ -0,0 +1,81 @@
+package service
+
+import (
+ "os"
+ "os/signal"
+ "sync"
+ "syscall"
+ "testing"
+ "time"
+
+ 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/stretchr/testify/assert"
+)
+
+func TestServiceInit(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel))
+ assert.NoError(t, err)
+
+ cfg := &config.Viper{
+ Path: "configs/.rr-service-init.yaml",
+ Prefix: "rr",
+ }
+
+ err = cont.RegisterAll(
+ cfg,
+ &logger.ZapLogger{},
+ &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()
+}