From e0a0c0d31cf9724754c63c67865403af7fceb1a4 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Wed, 4 Nov 2020 15:32:55 +0300 Subject: Update structure, add tests for RPC --- plugins/app/tests/.rr.yaml | 2 +- plugins/app/tests/factory_test.go | 16 +++++++++++----- plugins/app/tests/hello.php | 20 +++++++++++++++++++- plugins/app/tests/plugin_1.go | 4 ++-- plugins/app/tests/plugin_2.go | 4 ++-- 5 files changed, 35 insertions(+), 11 deletions(-) (limited to 'plugins/app/tests') diff --git a/plugins/app/tests/.rr.yaml b/plugins/app/tests/.rr.yaml index 171f51dc..c9d1bd40 100644 --- a/plugins/app/tests/.rr.yaml +++ b/plugins/app/tests/.rr.yaml @@ -1,5 +1,5 @@ app: - command: "php hello.php" + command: "php plugins/app/tests/hello.php hello_echo" user: "" group: "" env: diff --git a/plugins/app/tests/factory_test.go b/plugins/app/tests/factory_test.go index 7c885797..969c361c 100644 --- a/plugins/app/tests/factory_test.go +++ b/plugins/app/tests/factory_test.go @@ -9,24 +9,25 @@ import ( "github.com/spiral/endure" "github.com/spiral/roadrunner/v2/plugins/app" "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/stretchr/testify/assert" ) func TestFactory(t *testing.T) { - container, err := endure.NewContainer(endure.DebugLevel, endure.RetryOnFail(true)) + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) if err != nil { t.Fatal(err) } // config plugin - vp := &config.ViperProvider{} - vp.Path = ".rr.yaml" + vp := &config.Viper{} + vp.Path = "plugins/app/tests/.rr.yaml" vp.Prefix = "rr" err = container.Register(vp) if err != nil { t.Fatal(err) } - err = container.Register(&app.App{}) + err = container.Register(&app.Plugin{}) if err != nil { t.Fatal(err) } @@ -41,6 +42,11 @@ func TestFactory(t *testing.T) { t.Fatal(err) } + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + err = container.Init() if err != nil { t.Fatal(err) @@ -55,7 +61,7 @@ func TestFactory(t *testing.T) { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) - tt := time.NewTicker(time.Second * 2) + tt := time.NewTicker(time.Second * 200) for { select { diff --git a/plugins/app/tests/hello.php b/plugins/app/tests/hello.php index bf9e82cc..4219dcf4 100644 --- a/plugins/app/tests/hello.php +++ b/plugins/app/tests/hello.php @@ -1 +1,19 @@ -receive($ctx)) { + try { + $rr->send((string)$in); + } catch (\Throwable $e) { + $rr->error((string)$e); + } +} \ No newline at end of file diff --git a/plugins/app/tests/plugin_1.go b/plugins/app/tests/plugin_1.go index 7259ea9d..c6287f5c 100644 --- a/plugins/app/tests/plugin_1.go +++ b/plugins/app/tests/plugin_1.go @@ -9,11 +9,11 @@ import ( ) type Foo struct { - configProvider config.Provider + configProvider config.Configurer spawner app.WorkerFactory } -func (f *Foo) Init(p config.Provider, spw app.WorkerFactory) error { +func (f *Foo) Init(p config.Configurer, spw app.WorkerFactory) error { f.configProvider = p f.spawner = spw return nil diff --git a/plugins/app/tests/plugin_2.go b/plugins/app/tests/plugin_2.go index fbb9ca11..51fb83a4 100644 --- a/plugins/app/tests/plugin_2.go +++ b/plugins/app/tests/plugin_2.go @@ -12,11 +12,11 @@ import ( ) type Foo2 struct { - configProvider config.Provider + configProvider config.Configurer wf app.WorkerFactory } -func (f *Foo2) Init(p config.Provider, workerFactory app.WorkerFactory) error { +func (f *Foo2) Init(p config.Configurer, workerFactory app.WorkerFactory) error { f.configProvider = p f.wf = workerFactory return nil -- cgit v1.2.3 From bca69d968ef76a6260956bc169cb07996fbb7724 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Thu, 5 Nov 2020 15:45:18 +0300 Subject: App test --- plugins/app/tests/.rr.yaml | 2 +- plugins/app/tests/factory_test.go | 10 ++-- plugins/app/tests/hello.php | 3 +- plugins/app/tests/plugin.go | 98 +++++++++++++++++++++++++++++++++++++++ plugins/app/tests/plugin_1.go | 55 ---------------------- plugins/app/tests/plugin_2.go | 88 ----------------------------------- 6 files changed, 104 insertions(+), 152 deletions(-) create mode 100644 plugins/app/tests/plugin.go delete mode 100644 plugins/app/tests/plugin_1.go delete mode 100644 plugins/app/tests/plugin_2.go (limited to 'plugins/app/tests') diff --git a/plugins/app/tests/.rr.yaml b/plugins/app/tests/.rr.yaml index c9d1bd40..171f51dc 100644 --- a/plugins/app/tests/.rr.yaml +++ b/plugins/app/tests/.rr.yaml @@ -1,5 +1,5 @@ app: - command: "php plugins/app/tests/hello.php hello_echo" + command: "php hello.php" user: "" group: "" env: diff --git a/plugins/app/tests/factory_test.go b/plugins/app/tests/factory_test.go index 969c361c..878050a8 100644 --- a/plugins/app/tests/factory_test.go +++ b/plugins/app/tests/factory_test.go @@ -20,7 +20,7 @@ func TestFactory(t *testing.T) { } // config plugin vp := &config.Viper{} - vp.Path = "plugins/app/tests/.rr.yaml" + vp.Path = ".rr.yaml" vp.Prefix = "rr" err = container.Register(vp) if err != nil { @@ -37,11 +37,6 @@ func TestFactory(t *testing.T) { t.Fatal(err) } - err = container.Register(&Foo2{}) - if err != nil { - t.Fatal(err) - } - err = container.Register(&logger.ZapLogger{}) if err != nil { t.Fatal(err) @@ -61,7 +56,8 @@ func TestFactory(t *testing.T) { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) - tt := time.NewTicker(time.Second * 200) + // stop after 10 seconds + tt := time.NewTicker(time.Second * 10) for { select { diff --git a/plugins/app/tests/hello.php b/plugins/app/tests/hello.php index 4219dcf4..2591b77f 100644 --- a/plugins/app/tests/hello.php +++ b/plugins/app/tests/hello.php @@ -6,8 +6,9 @@ use Spiral\Goridge; use Spiral\RoadRunner; -require "/vendor_php/autoload.php"; +require dirname(__DIR__) . "/../../vendor_php/autoload.php"; +$relay = new Goridge\StreamRelay(STDIN, STDOUT); $rr = new RoadRunner\Worker($relay); while ($in = $rr->receive($ctx)) { diff --git a/plugins/app/tests/plugin.go b/plugins/app/tests/plugin.go new file mode 100644 index 00000000..e37aca01 --- /dev/null +++ b/plugins/app/tests/plugin.go @@ -0,0 +1,98 @@ +package tests + +import ( + "context" + "time" + + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2" + "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/plugins/config" +) + +const ConfigSection = "app" + +var testPoolConfig = roadrunner.Config{ + NumWorkers: 10, + MaxJobs: 100, + AllocateTimeout: time.Second * 10, + DestroyTimeout: time.Second * 10, + Supervisor: &roadrunner.SupervisorConfig{ + WatchTick: 60, + TTL: 1000, + IdleTTL: 10, + ExecTTL: 10, + MaxWorkerMemory: 1000, + }, +} + +type Foo struct { + configProvider config.Configurer + wf app.WorkerFactory + pool roadrunner.Pool +} + +func (f *Foo) Init(p config.Configurer, workerFactory app.WorkerFactory) error { + f.configProvider = p + f.wf = workerFactory + return nil +} + +func (f *Foo) Serve() chan error { + const op = errors.Op("serve") + errCh := make(chan error, 1) + + conf := &app.Config{} + var err error + err = f.configProvider.UnmarshalKey(ConfigSection, conf) + if err != nil { + errCh <- err + return errCh + } + + // test CMDFactory + cmd, err := f.wf.CmdFactory(nil) + if err != nil { + errCh <- err + return errCh + } + if cmd == nil { + errCh <- errors.E(op, "command is nil") + return errCh + } + + // test worker creation + _, err = f.wf.NewWorker(context.Background(), nil) + if err != nil { + errCh <- err + return errCh + } + + f.pool, err = f.wf.NewWorkerPool(context.Background(), testPoolConfig, nil) + if err != nil { + errCh <- err + return errCh + } + + r := roadrunner.Payload{ + Context: nil, + Body: []byte("test"), + } + rsp, err := f.pool.Exec(r) + if err != nil { + errCh <- err + return errCh + } + + if string(rsp.Body) != "test" { + errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) + return errCh + } + + return errCh +} + +func (f *Foo) Stop() error { + f.pool.Destroy(context.Background()) + return nil +} diff --git a/plugins/app/tests/plugin_1.go b/plugins/app/tests/plugin_1.go deleted file mode 100644 index c6287f5c..00000000 --- a/plugins/app/tests/plugin_1.go +++ /dev/null @@ -1,55 +0,0 @@ -package tests - -import ( - "errors" - "fmt" - - "github.com/spiral/roadrunner/v2/plugins/app" - "github.com/spiral/roadrunner/v2/plugins/config" -) - -type Foo struct { - configProvider config.Configurer - spawner app.WorkerFactory -} - -func (f *Foo) Init(p config.Configurer, spw app.WorkerFactory) error { - f.configProvider = p - f.spawner = spw - return nil -} - -func (f *Foo) Serve() chan error { - errCh := make(chan error, 1) - - r := &app.Config{} - err := f.configProvider.UnmarshalKey("app", r) - if err != nil { - errCh <- err - return errCh - } - - cmd, err := f.spawner.CmdFactory(nil) - if err != nil { - errCh <- err - return errCh - } - if cmd == nil { - errCh <- errors.New("command is nil") - return errCh - } - a := cmd() - out, err := a.Output() - if err != nil { - errCh <- err - return errCh - } - - fmt.Println(string(out)) - - return errCh -} - -func (f *Foo) Stop() error { - return nil -} diff --git a/plugins/app/tests/plugin_2.go b/plugins/app/tests/plugin_2.go deleted file mode 100644 index 51fb83a4..00000000 --- a/plugins/app/tests/plugin_2.go +++ /dev/null @@ -1,88 +0,0 @@ -package tests - -import ( - "context" - "errors" - "fmt" - "time" - - "github.com/spiral/roadrunner/v2" - "github.com/spiral/roadrunner/v2/plugins/app" - "github.com/spiral/roadrunner/v2/plugins/config" -) - -type Foo2 struct { - configProvider config.Configurer - wf app.WorkerFactory -} - -func (f *Foo2) Init(p config.Configurer, workerFactory app.WorkerFactory) error { - f.configProvider = p - f.wf = workerFactory - return nil -} - -func (f *Foo2) Serve() chan error { - errCh := make(chan error, 1) - - r := &app.Config{} - err := f.configProvider.UnmarshalKey("app", r) - if err != nil { - errCh <- err - return errCh - } - - cmd, err := f.wf.CmdFactory(nil) - if err != nil { - errCh <- err - return errCh - } - if cmd == nil { - errCh <- errors.New("command is nil") - return errCh - } - a := cmd() - out, err := a.Output() - if err != nil { - errCh <- err - return errCh - } - - w, err := f.wf.NewWorker(context.Background(), nil) - if err != nil { - errCh <- err - return errCh - } - - _ = w - - poolConfig := roadrunner.Config{ - NumWorkers: 10, - MaxJobs: 100, - AllocateTimeout: time.Second * 10, - DestroyTimeout: time.Second * 10, - Supervisor: &roadrunner.SupervisorConfig{ - WatchTick: 60, - TTL: 1000, - IdleTTL: 10, - ExecTTL: 10, - MaxWorkerMemory: 1000, - }, - } - - pool, err := f.wf.NewWorkerPool(context.Background(), poolConfig, nil) - if err != nil { - errCh <- err - return errCh - } - - _ = pool - - fmt.Println(string(out)) - - return errCh -} - -func (f *Foo2) Stop() error { - return nil -} -- cgit v1.2.3 From 3759df801089973edb1116df69f310aaa2d703dc Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Thu, 5 Nov 2020 16:53:51 +0300 Subject: Add App tests --- plugins/app/tests/.rr.yaml | 9 - plugins/app/tests/app_test.go | 358 ++++++++++++++++++++++ plugins/app/tests/configs/.rr-no-app-section.yaml | 9 + plugins/app/tests/configs/.rr-sockets.yaml | 9 + plugins/app/tests/configs/.rr-tcp.yaml | 9 + plugins/app/tests/configs/.rr-wrong-command.yaml | 9 + plugins/app/tests/configs/.rr-wrong-relay.yaml | 9 + plugins/app/tests/configs/.rr.yaml | 9 + plugins/app/tests/factory_test.go | 80 ----- plugins/app/tests/hello.php | 20 -- plugins/app/tests/plugin.go | 98 ------ plugins/app/tests/plugin_pipes.go | 129 ++++++++ plugins/app/tests/plugin_sockets.go | 111 +++++++ plugins/app/tests/plugin_tcp.go | 111 +++++++ plugins/app/tests/socket.php | 25 ++ plugins/app/tests/tcp.php | 20 ++ 16 files changed, 808 insertions(+), 207 deletions(-) delete mode 100644 plugins/app/tests/.rr.yaml create mode 100644 plugins/app/tests/app_test.go create mode 100644 plugins/app/tests/configs/.rr-no-app-section.yaml create mode 100644 plugins/app/tests/configs/.rr-sockets.yaml create mode 100644 plugins/app/tests/configs/.rr-tcp.yaml create mode 100644 plugins/app/tests/configs/.rr-wrong-command.yaml create mode 100644 plugins/app/tests/configs/.rr-wrong-relay.yaml create mode 100644 plugins/app/tests/configs/.rr.yaml delete mode 100644 plugins/app/tests/factory_test.go delete mode 100644 plugins/app/tests/hello.php delete mode 100644 plugins/app/tests/plugin.go create mode 100644 plugins/app/tests/plugin_pipes.go create mode 100644 plugins/app/tests/plugin_sockets.go create mode 100644 plugins/app/tests/plugin_tcp.go create mode 100644 plugins/app/tests/socket.php create mode 100644 plugins/app/tests/tcp.php (limited to 'plugins/app/tests') diff --git a/plugins/app/tests/.rr.yaml b/plugins/app/tests/.rr.yaml deleted file mode 100644 index 171f51dc..00000000 --- a/plugins/app/tests/.rr.yaml +++ /dev/null @@ -1,9 +0,0 @@ -app: - command: "php hello.php" - user: "" - group: "" - env: - "RR_CONFIG": "/some/place/on/the/C134" - "RR_CONFIG2": "C138" - relay: "pipes" - relayTimeout: "20s" \ No newline at end of file diff --git a/plugins/app/tests/app_test.go b/plugins/app/tests/app_test.go new file mode 100644 index 00000000..3c416b59 --- /dev/null +++ b/plugins/app/tests/app_test.go @@ -0,0 +1,358 @@ +package tests + +import ( + "os" + "os/signal" + "testing" + "time" + + "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/stretchr/testify/assert" +) + +func TestAppPipes(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rr.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&app.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + errCh, err := container.Serve() + if err != nil { + t.Fatal(err) + } + + // stop by CTRL+C + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + + // stop after 10 seconds + tt := time.NewTicker(time.Second * 10) + + for { + select { + case e := <-errCh: + assert.NoError(t, e.Error) + assert.NoError(t, container.Stop()) + return + case <-c: + er := container.Stop() + if er != nil { + panic(er) + } + return + case <-tt.C: + tt.Stop() + assert.NoError(t, container.Stop()) + return + } + } +} + +func TestAppSockets(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rr-sockets.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&app.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo2{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + errCh, err := container.Serve() + if err != nil { + t.Fatal(err) + } + + // stop by CTRL+C + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + + // stop after 10 seconds + tt := time.NewTicker(time.Second * 10) + + for { + select { + case e := <-errCh: + assert.NoError(t, e.Error) + assert.NoError(t, container.Stop()) + return + case <-c: + er := container.Stop() + if er != nil { + panic(er) + } + return + case <-tt.C: + tt.Stop() + assert.NoError(t, container.Stop()) + return + } + } +} + +func TestAppTCP(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rr-tcp.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&app.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo3{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + errCh, err := container.Serve() + if err != nil { + t.Fatal(err) + } + + // stop by CTRL+C + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + + // stop after 10 seconds + tt := time.NewTicker(time.Second * 10) + + for { + select { + case e := <-errCh: + assert.NoError(t, e.Error) + assert.NoError(t, container.Stop()) + return + case <-c: + er := container.Stop() + if er != nil { + panic(er) + } + return + case <-tt.C: + tt.Stop() + assert.NoError(t, container.Stop()) + return + } + } +} + +func TestAppWrongConfig(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rrrrrrrrrr.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&app.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo3{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + assert.Error(t, container.Init()) +} + +func TestAppWrongRelay(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rr-wrong-relay.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&app.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo3{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + _, err = container.Serve() + assert.Error(t, err) +} + +func TestAppWrongCommand(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rr-wrong-command.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&app.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo3{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + _, err = container.Serve() + assert.Error(t, err) +} + +func TestAppNoAppSectionInConfig(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rr-wrong-command.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&app.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo3{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + _, err = container.Serve() + assert.Error(t, err) +} diff --git a/plugins/app/tests/configs/.rr-no-app-section.yaml b/plugins/app/tests/configs/.rr-no-app-section.yaml new file mode 100644 index 00000000..d129ae8a --- /dev/null +++ b/plugins/app/tests/configs/.rr-no-app-section.yaml @@ -0,0 +1,9 @@ +upp: + command: "php ../../../tests/client.php echo pipes" + user: "" + group: "" + env: + "RR_CONFIG": "/some/place/on/the/C134" + "RR_CONFIG2": "C138" + relay: "pipes" + relayTimeout: "20s" \ No newline at end of file diff --git a/plugins/app/tests/configs/.rr-sockets.yaml b/plugins/app/tests/configs/.rr-sockets.yaml new file mode 100644 index 00000000..9bd62693 --- /dev/null +++ b/plugins/app/tests/configs/.rr-sockets.yaml @@ -0,0 +1,9 @@ +app: + command: "php socket.php" + user: "" + group: "" + env: + "RR_CONFIG": "/some/place/on/the/C134" + "RR_CONFIG2": "C138" + relay: "unix://unix.sock" + relayTimeout: "20s" \ No newline at end of file diff --git a/plugins/app/tests/configs/.rr-tcp.yaml b/plugins/app/tests/configs/.rr-tcp.yaml new file mode 100644 index 00000000..c5a26d37 --- /dev/null +++ b/plugins/app/tests/configs/.rr-tcp.yaml @@ -0,0 +1,9 @@ +app: + command: "php tcp.php" + user: "" + group: "" + env: + "RR_CONFIG": "/some/place/on/the/C134" + "RR_CONFIG2": "C138" + relay: "tcp://localhost:9999" + relayTimeout: "20s" \ No newline at end of file diff --git a/plugins/app/tests/configs/.rr-wrong-command.yaml b/plugins/app/tests/configs/.rr-wrong-command.yaml new file mode 100644 index 00000000..4bd019d3 --- /dev/null +++ b/plugins/app/tests/configs/.rr-wrong-command.yaml @@ -0,0 +1,9 @@ +app: + command: "php some_absent_file.php" + user: "" + group: "" + env: + "RR_CONFIG": "/some/place/on/the/C134" + "RR_CONFIG2": "C138" + relay: "pipes" + relayTimeout: "20s" diff --git a/plugins/app/tests/configs/.rr-wrong-relay.yaml b/plugins/app/tests/configs/.rr-wrong-relay.yaml new file mode 100644 index 00000000..d8ffe8f8 --- /dev/null +++ b/plugins/app/tests/configs/.rr-wrong-relay.yaml @@ -0,0 +1,9 @@ +app: + command: "php ../../../tests/client.php echo pipes" + user: "" + group: "" + env: + "RR_CONFIG": "/some/place/on/the/C134" + "RR_CONFIG2": "C138" + relay: "pupes" + relayTimeout: "20s" \ No newline at end of file diff --git a/plugins/app/tests/configs/.rr.yaml b/plugins/app/tests/configs/.rr.yaml new file mode 100644 index 00000000..221aff92 --- /dev/null +++ b/plugins/app/tests/configs/.rr.yaml @@ -0,0 +1,9 @@ +app: + command: "php ../../../tests/client.php echo pipes" + user: "" + group: "" + env: + "RR_CONFIG": "/some/place/on/the/C134" + "RR_CONFIG2": "C138" + relay: "pipes" + relayTimeout: "20s" \ No newline at end of file diff --git a/plugins/app/tests/factory_test.go b/plugins/app/tests/factory_test.go deleted file mode 100644 index 878050a8..00000000 --- a/plugins/app/tests/factory_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package tests - -import ( - "os" - "os/signal" - "testing" - "time" - - "github.com/spiral/endure" - "github.com/spiral/roadrunner/v2/plugins/app" - "github.com/spiral/roadrunner/v2/plugins/config" - "github.com/spiral/roadrunner/v2/plugins/logger" - "github.com/stretchr/testify/assert" -) - -func TestFactory(t *testing.T) { - container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) - if err != nil { - t.Fatal(err) - } - // config plugin - vp := &config.Viper{} - vp.Path = ".rr.yaml" - vp.Prefix = "rr" - err = container.Register(vp) - if err != nil { - t.Fatal(err) - } - - err = container.Register(&app.Plugin{}) - if err != nil { - t.Fatal(err) - } - - err = container.Register(&Foo{}) - if err != nil { - t.Fatal(err) - } - - err = container.Register(&logger.ZapLogger{}) - if err != nil { - t.Fatal(err) - } - - err = container.Init() - if err != nil { - t.Fatal(err) - } - - errCh, err := container.Serve() - if err != nil { - t.Fatal(err) - } - - // stop by CTRL+C - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - - // stop after 10 seconds - tt := time.NewTicker(time.Second * 10) - - for { - select { - case e := <-errCh: - assert.NoError(t, e.Error) - assert.NoError(t, container.Stop()) - return - case <-c: - er := container.Stop() - if er != nil { - panic(er) - } - return - case <-tt.C: - tt.Stop() - assert.NoError(t, container.Stop()) - return - } - } -} diff --git a/plugins/app/tests/hello.php b/plugins/app/tests/hello.php deleted file mode 100644 index 2591b77f..00000000 --- a/plugins/app/tests/hello.php +++ /dev/null @@ -1,20 +0,0 @@ -receive($ctx)) { - try { - $rr->send((string)$in); - } catch (\Throwable $e) { - $rr->error((string)$e); - } -} \ No newline at end of file diff --git a/plugins/app/tests/plugin.go b/plugins/app/tests/plugin.go deleted file mode 100644 index e37aca01..00000000 --- a/plugins/app/tests/plugin.go +++ /dev/null @@ -1,98 +0,0 @@ -package tests - -import ( - "context" - "time" - - "github.com/spiral/errors" - "github.com/spiral/roadrunner/v2" - "github.com/spiral/roadrunner/v2/plugins/app" - "github.com/spiral/roadrunner/v2/plugins/config" -) - -const ConfigSection = "app" - -var testPoolConfig = roadrunner.Config{ - NumWorkers: 10, - MaxJobs: 100, - AllocateTimeout: time.Second * 10, - DestroyTimeout: time.Second * 10, - Supervisor: &roadrunner.SupervisorConfig{ - WatchTick: 60, - TTL: 1000, - IdleTTL: 10, - ExecTTL: 10, - MaxWorkerMemory: 1000, - }, -} - -type Foo struct { - configProvider config.Configurer - wf app.WorkerFactory - pool roadrunner.Pool -} - -func (f *Foo) Init(p config.Configurer, workerFactory app.WorkerFactory) error { - f.configProvider = p - f.wf = workerFactory - return nil -} - -func (f *Foo) Serve() chan error { - const op = errors.Op("serve") - errCh := make(chan error, 1) - - conf := &app.Config{} - var err error - err = f.configProvider.UnmarshalKey(ConfigSection, conf) - if err != nil { - errCh <- err - return errCh - } - - // test CMDFactory - cmd, err := f.wf.CmdFactory(nil) - if err != nil { - errCh <- err - return errCh - } - if cmd == nil { - errCh <- errors.E(op, "command is nil") - return errCh - } - - // test worker creation - _, err = f.wf.NewWorker(context.Background(), nil) - if err != nil { - errCh <- err - return errCh - } - - f.pool, err = f.wf.NewWorkerPool(context.Background(), testPoolConfig, nil) - if err != nil { - errCh <- err - return errCh - } - - r := roadrunner.Payload{ - Context: nil, - Body: []byte("test"), - } - rsp, err := f.pool.Exec(r) - if err != nil { - errCh <- err - return errCh - } - - if string(rsp.Body) != "test" { - errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) - return errCh - } - - return errCh -} - -func (f *Foo) Stop() error { - f.pool.Destroy(context.Background()) - return nil -} diff --git a/plugins/app/tests/plugin_pipes.go b/plugins/app/tests/plugin_pipes.go new file mode 100644 index 00000000..704ec988 --- /dev/null +++ b/plugins/app/tests/plugin_pipes.go @@ -0,0 +1,129 @@ +package tests + +import ( + "context" + "time" + + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2" + "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/plugins/config" +) + +const ConfigSection = "app" + +var testPoolConfig = roadrunner.Config{ + NumWorkers: 10, + MaxJobs: 100, + AllocateTimeout: time.Second * 10, + DestroyTimeout: time.Second * 10, + Supervisor: &roadrunner.SupervisorConfig{ + WatchTick: 60, + TTL: 1000, + IdleTTL: 10, + ExecTTL: 10, + MaxWorkerMemory: 1000, + }, +} + +type Foo struct { + configProvider config.Configurer + wf app.WorkerFactory + pool roadrunner.Pool +} + +func (f *Foo) Init(p config.Configurer, workerFactory app.WorkerFactory) error { + f.configProvider = p + f.wf = workerFactory + return nil +} + +func (f *Foo) Serve() chan error { + const op = errors.Op("serve") + + // test payload for echo + r := roadrunner.Payload{ + Context: nil, + Body: []byte("test"), + } + + errCh := make(chan error, 1) + + conf := &app.Config{} + var err error + err = f.configProvider.UnmarshalKey(ConfigSection, conf) + if err != nil { + errCh <- err + return errCh + } + + // test CMDFactory + cmd, err := f.wf.CmdFactory(nil) + if err != nil { + errCh <- err + return errCh + } + if cmd == nil { + errCh <- errors.E(op, "command is nil") + return errCh + } + + // test worker creation + w, err := f.wf.NewWorker(context.Background(), nil) + if err != nil { + errCh <- err + return errCh + } + + // test that our worker is functional + sw, err := roadrunner.NewSyncWorker(w) + if err != nil { + errCh <- err + return errCh + } + + rsp, err := sw.Exec(r) + if err != nil { + errCh <- err + return errCh + } + + if string(rsp.Body) != "test" { + errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) + return errCh + } + + // should not be errors + err = sw.Stop(context.Background()) + if err != nil { + errCh <- err + return errCh + } + + // test pool + f.pool, err = f.wf.NewWorkerPool(context.Background(), testPoolConfig, nil) + if err != nil { + errCh <- err + return errCh + } + + // test pool execution + rsp, err = f.pool.Exec(r) + if err != nil { + errCh <- err + return errCh + } + + // echo of the "test" should be -> test + if string(rsp.Body) != "test" { + errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) + return errCh + } + + return errCh +} + +func (f *Foo) Stop() error { + f.pool.Destroy(context.Background()) + return nil +} diff --git a/plugins/app/tests/plugin_sockets.go b/plugins/app/tests/plugin_sockets.go new file mode 100644 index 00000000..2b7da312 --- /dev/null +++ b/plugins/app/tests/plugin_sockets.go @@ -0,0 +1,111 @@ +package tests + +import ( + "context" + + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2" + "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/plugins/config" +) + +type Foo2 struct { + configProvider config.Configurer + wf app.WorkerFactory + pool roadrunner.Pool +} + +func (f *Foo2) Init(p config.Configurer, workerFactory app.WorkerFactory) error { + f.configProvider = p + f.wf = workerFactory + return nil +} + +func (f *Foo2) Serve() chan error { + const op = errors.Op("serve") + var err error + errCh := make(chan error, 1) + conf := &app.Config{} + + // test payload for echo + r := roadrunner.Payload{ + Context: nil, + Body: []byte("test"), + } + + err = f.configProvider.UnmarshalKey(ConfigSection, conf) + if err != nil { + errCh <- err + return errCh + } + + // test CMDFactory + cmd, err := f.wf.CmdFactory(nil) + if err != nil { + errCh <- err + return errCh + } + if cmd == nil { + errCh <- errors.E(op, "command is nil") + return errCh + } + + // test worker creation + w, err := f.wf.NewWorker(context.Background(), nil) + if err != nil { + errCh <- err + return errCh + } + + // test that our worker is functional + sw, err := roadrunner.NewSyncWorker(w) + if err != nil { + errCh <- err + return errCh + } + + rsp, err := sw.Exec(r) + if err != nil { + errCh <- err + return errCh + } + + if string(rsp.Body) != "test" { + errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) + return errCh + } + + // should not be errors + err = sw.Stop(context.Background()) + if err != nil { + errCh <- err + return errCh + } + + // test pool + f.pool, err = f.wf.NewWorkerPool(context.Background(), testPoolConfig, nil) + if err != nil { + errCh <- err + return errCh + } + + // test pool execution + rsp, err = f.pool.Exec(r) + if err != nil { + errCh <- err + return errCh + } + + // echo of the "test" should be -> test + if string(rsp.Body) != "test" { + errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) + return errCh + } + + return errCh +} + +func (f *Foo2) Stop() error { + f.pool.Destroy(context.Background()) + return nil +} diff --git a/plugins/app/tests/plugin_tcp.go b/plugins/app/tests/plugin_tcp.go new file mode 100644 index 00000000..1a236db0 --- /dev/null +++ b/plugins/app/tests/plugin_tcp.go @@ -0,0 +1,111 @@ +package tests + +import ( + "context" + + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2" + "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/plugins/config" +) + +type Foo3 struct { + configProvider config.Configurer + wf app.WorkerFactory + pool roadrunner.Pool +} + +func (f *Foo3) Init(p config.Configurer, workerFactory app.WorkerFactory) error { + f.configProvider = p + f.wf = workerFactory + return nil +} + +func (f *Foo3) Serve() chan error { + const op = errors.Op("serve") + var err error + errCh := make(chan error, 1) + conf := &app.Config{} + + // test payload for echo + r := roadrunner.Payload{ + Context: nil, + Body: []byte("test"), + } + + err = f.configProvider.UnmarshalKey(ConfigSection, conf) + if err != nil { + errCh <- err + return errCh + } + + // test CMDFactory + cmd, err := f.wf.CmdFactory(nil) + if err != nil { + errCh <- err + return errCh + } + if cmd == nil { + errCh <- errors.E(op, "command is nil") + return errCh + } + + // test worker creation + w, err := f.wf.NewWorker(context.Background(), nil) + if err != nil { + errCh <- err + return errCh + } + + // test that our worker is functional + sw, err := roadrunner.NewSyncWorker(w) + if err != nil { + errCh <- err + return errCh + } + + rsp, err := sw.Exec(r) + if err != nil { + errCh <- err + return errCh + } + + if string(rsp.Body) != "test" { + errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) + return errCh + } + + // should not be errors + err = sw.Stop(context.Background()) + if err != nil { + errCh <- err + return errCh + } + + // test pool + f.pool, err = f.wf.NewWorkerPool(context.Background(), testPoolConfig, nil) + if err != nil { + errCh <- err + return errCh + } + + // test pool execution + rsp, err = f.pool.Exec(r) + if err != nil { + errCh <- err + return errCh + } + + // echo of the "test" should be -> test + if string(rsp.Body) != "test" { + errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) + return errCh + } + + return errCh +} + +func (f *Foo3) Stop() error { + f.pool.Destroy(context.Background()) + return nil +} diff --git a/plugins/app/tests/socket.php b/plugins/app/tests/socket.php new file mode 100644 index 00000000..143c3ce4 --- /dev/null +++ b/plugins/app/tests/socket.php @@ -0,0 +1,25 @@ +receive($ctx)) { + try { + $rr->send((string)$in); + } catch (\Throwable $e) { + $rr->error((string)$e); + } +} diff --git a/plugins/app/tests/tcp.php b/plugins/app/tests/tcp.php new file mode 100644 index 00000000..2d6fb00a --- /dev/null +++ b/plugins/app/tests/tcp.php @@ -0,0 +1,20 @@ +receive($ctx)) { + try { + $rr->send((string)$in); + } catch (\Throwable $e) { + $rr->error((string)$e); + } +} \ No newline at end of file -- cgit v1.2.3 From e76536ce9310552547bdae3ad46ebd08dbd2bb4a Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Thu, 5 Nov 2020 17:10:10 +0300 Subject: Move magic string to the constants (test) --- plugins/app/tests/plugin_pipes.go | 7 ++++--- plugins/app/tests/plugin_sockets.go | 6 +++--- plugins/app/tests/plugin_tcp.go | 6 +++--- 3 files changed, 10 insertions(+), 9 deletions(-) (limited to 'plugins/app/tests') diff --git a/plugins/app/tests/plugin_pipes.go b/plugins/app/tests/plugin_pipes.go index 704ec988..fc999718 100644 --- a/plugins/app/tests/plugin_pipes.go +++ b/plugins/app/tests/plugin_pipes.go @@ -11,6 +11,7 @@ import ( ) const ConfigSection = "app" +const Response = "test" var testPoolConfig = roadrunner.Config{ NumWorkers: 10, @@ -44,7 +45,7 @@ func (f *Foo) Serve() chan error { // test payload for echo r := roadrunner.Payload{ Context: nil, - Body: []byte("test"), + Body: []byte(Response), } errCh := make(chan error, 1) @@ -88,7 +89,7 @@ func (f *Foo) Serve() chan error { return errCh } - if string(rsp.Body) != "test" { + if string(rsp.Body) != Response { errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) return errCh } @@ -115,7 +116,7 @@ func (f *Foo) Serve() chan error { } // echo of the "test" should be -> test - if string(rsp.Body) != "test" { + if string(rsp.Body) != Response { errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) return errCh } diff --git a/plugins/app/tests/plugin_sockets.go b/plugins/app/tests/plugin_sockets.go index 2b7da312..585264f6 100644 --- a/plugins/app/tests/plugin_sockets.go +++ b/plugins/app/tests/plugin_sockets.go @@ -30,7 +30,7 @@ func (f *Foo2) Serve() chan error { // test payload for echo r := roadrunner.Payload{ Context: nil, - Body: []byte("test"), + Body: []byte(Response), } err = f.configProvider.UnmarshalKey(ConfigSection, conf) @@ -70,7 +70,7 @@ func (f *Foo2) Serve() chan error { return errCh } - if string(rsp.Body) != "test" { + if string(rsp.Body) != Response { errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) return errCh } @@ -97,7 +97,7 @@ func (f *Foo2) Serve() chan error { } // echo of the "test" should be -> test - if string(rsp.Body) != "test" { + if string(rsp.Body) != Response { errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) return errCh } diff --git a/plugins/app/tests/plugin_tcp.go b/plugins/app/tests/plugin_tcp.go index 1a236db0..6abc533d 100644 --- a/plugins/app/tests/plugin_tcp.go +++ b/plugins/app/tests/plugin_tcp.go @@ -30,7 +30,7 @@ func (f *Foo3) Serve() chan error { // test payload for echo r := roadrunner.Payload{ Context: nil, - Body: []byte("test"), + Body: []byte(Response), } err = f.configProvider.UnmarshalKey(ConfigSection, conf) @@ -70,7 +70,7 @@ func (f *Foo3) Serve() chan error { return errCh } - if string(rsp.Body) != "test" { + if string(rsp.Body) != Response { errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) return errCh } @@ -97,7 +97,7 @@ func (f *Foo3) Serve() chan error { } // echo of the "test" should be -> test - if string(rsp.Body) != "test" { + if string(rsp.Body) != Response { errCh <- errors.E("response from worker is wrong", errors.Errorf("response: %s", rsp.Body)) return errCh } -- cgit v1.2.3