summaryrefslogtreecommitdiff
path: root/plugins/server/tests/plugin_tcp.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-11-16 15:46:08 +0300
committerGitHub <[email protected]>2020-11-16 15:46:08 +0300
commit6236aac37bd1661b20400689f66d1e92283c5111 (patch)
treeeb8a9a4e4717fb4cd6c971b5ce67c53b5f6a0f8c /plugins/server/tests/plugin_tcp.go
parent0874bcb2f6b284a940ba4f3507eb8c4619c27868 (diff)
parent38f6925db27dd94cfbca873901bf932ed1456906 (diff)
Merge pull request #392 from spiral/plugin/metricsv2.0.0-alpha18
[RR2] Metrics plugin 2.0
Diffstat (limited to 'plugins/server/tests/plugin_tcp.go')
-rw-r--r--plugins/server/tests/plugin_tcp.go112
1 files changed, 112 insertions, 0 deletions
diff --git a/plugins/server/tests/plugin_tcp.go b/plugins/server/tests/plugin_tcp.go
new file mode 100644
index 00000000..39044577
--- /dev/null
+++ b/plugins/server/tests/plugin_tcp.go
@@ -0,0 +1,112 @@
+package tests
+
+import (
+ "context"
+
+ "github.com/spiral/errors"
+ "github.com/spiral/roadrunner/v2"
+ "github.com/spiral/roadrunner/v2/interfaces/server"
+ "github.com/spiral/roadrunner/v2/plugins/config"
+ plugin "github.com/spiral/roadrunner/v2/plugins/server"
+)
+
+type Foo3 struct {
+ configProvider config.Configurer
+ wf server.WorkerFactory
+ pool roadrunner.Pool
+}
+
+func (f *Foo3) Init(p config.Configurer, workerFactory server.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 := &plugin.Config{}
+
+ // test payload for echo
+ r := roadrunner.Payload{
+ Context: nil,
+ Body: []byte(Response),
+ }
+
+ 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) != Response {
+ 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) != Response {
+ 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
+}