diff options
author | Valery Piashchynski <[email protected]> | 2020-11-24 18:43:01 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-11-24 18:43:01 +0300 |
commit | 31559f018b2e9ee7eb53e635e8570259ff4d57e6 (patch) | |
tree | 1ce85ff673821dcf90c05511b184bf31fd454964 /plugins/http | |
parent | 7b68e0bdf0b7756ab1925ec0fbc71e08e7040c5a (diff) |
Informer and Resetter tests
Diffstat (limited to 'plugins/http')
-rw-r--r-- | plugins/http/plugin.go | 27 | ||||
-rw-r--r-- | plugins/http/tests/configs/.rr-http.yaml | 8 | ||||
-rw-r--r-- | plugins/http/tests/http_test.go | 126 |
3 files changed, 155 insertions, 6 deletions
diff --git a/plugins/http/plugin.go b/plugins/http/plugin.go index 38539d52..b37a9700 100644 --- a/plugins/http/plugin.go +++ b/plugins/http/plugin.go @@ -95,7 +95,7 @@ func (s *Plugin) Init(cfg config.Configurer, log log.Logger, server factory.Serv env := make(map[string]string) env["RR_HTTP"] = "true" - p, err := server.NewWorkerPool(context.Background(), roadrunner.PoolConfig{ + s.pool, err = server.NewWorkerPool(context.Background(), roadrunner.PoolConfig{ Debug: s.cfg.Pool.Debug, NumWorkers: s.cfg.Pool.NumWorkers, MaxJobs: s.cfg.Pool.MaxJobs, @@ -106,7 +106,8 @@ func (s *Plugin) Init(cfg config.Configurer, log log.Logger, server factory.Serv if err != nil { return errors.E(op, err) } - s.pool = p + + s.server = server s.AddListener(s.logCallback) @@ -117,6 +118,8 @@ func (s *Plugin) logCallback(event interface{}) { switch ev := event.(type) { case ResponseEvent: s.log.Info("response received", "elapsed", ev.Elapsed().String(), "remote address", ev.Request.RemoteAddr) + case ErrorEvent: + s.log.Error("error event received", "elapsed", ev.Elapsed().String(), "error", ev.Error) default: fmt.Println(event) } @@ -438,9 +441,15 @@ func (s *Plugin) Workers() []roadrunner.WorkerBase { return s.pool.Workers() } +func (s *Plugin) Name() string { + return ServiceName +} + func (s *Plugin) Reset() error { s.Lock() defer s.Unlock() + const op = errors.Op("http reset") + s.log.Info("Resetting http plugin") s.pool.Destroy(context.Background()) // Set needed env vars @@ -451,7 +460,7 @@ func (s *Plugin) Reset() error { // re-read the config err = s.configurer.UnmarshalKey(ServiceName, &s.cfg) if err != nil { - return err + return errors.E(op, err) } s.pool, err = s.server.NewWorkerPool(context.Background(), roadrunner.PoolConfig{ @@ -463,7 +472,17 @@ func (s *Plugin) Reset() error { Supervisor: s.cfg.Pool.Supervisor, }, env) if err != nil { - return err + return errors.E(op, err) + } + + s.handler, err = NewHandler( + s.cfg.MaxRequestSize, + *s.cfg.Uploads, + s.cfg.cidrs, + s.pool, + ) + if err != nil { + return errors.E(op, err) } // restore original listeners diff --git a/plugins/http/tests/configs/.rr-http.yaml b/plugins/http/tests/configs/.rr-http.yaml index 3c2ec5a9..c989df24 100644 --- a/plugins/http/tests/configs/.rr-http.yaml +++ b/plugins/http/tests/configs/.rr-http.yaml @@ -1,3 +1,7 @@ +rpc: + listen: tcp://127.0.0.1:6001 + disabled: false + server: command: "php psr-worker.php" user: "" @@ -10,7 +14,7 @@ server: http: debug: true address: 127.0.0.1:8084 - maxRequestSize: 200 + maxRequestSize: 1024 middleware: [ "" ] uploads: forbid: [ ".php", ".exe", ".bat" ] @@ -23,7 +27,7 @@ http: ssl: port: 8892 - redirect: true + redirect: false cert: fixtures/server.crt key: fixtures/server.key # rootCa: root.crt diff --git a/plugins/http/tests/http_test.go b/plugins/http/tests/http_test.go index ca79e94e..9ea474a3 100644 --- a/plugins/http/tests/http_test.go +++ b/plugins/http/tests/http_test.go @@ -4,8 +4,10 @@ import ( "bytes" "crypto/tls" "io/ioutil" + "net" "net/http" "net/http/httptest" + "net/rpc" "os" "os/signal" "sync" @@ -14,9 +16,13 @@ import ( "time" "github.com/spiral/endure" + "github.com/spiral/goridge/v2" + "github.com/spiral/roadrunner/v2" "github.com/spiral/roadrunner/v2/plugins/config" httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" + "github.com/spiral/roadrunner/v2/plugins/informer" "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/spiral/roadrunner/v2/plugins/resetter" "github.com/yookoala/gofast" rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" @@ -94,6 +100,126 @@ func TestHTTPInit(t *testing.T) { wg.Wait() } +func TestHTTPInformerReset(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-http.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &informer.Plugin{}, + &resetter.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) + + go func() { + tt := time.NewTimer(time.Second * 10) + 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()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + t.Run("HTTPInformerTest", informerTest) + t.Run("HTTPEchoTestBefore", echoHTTP) + t.Run("HTTPResetTest", resetTest) + t.Run("HTTPEchoTestAfter", echoHTTP) + + wg.Wait() +} + +func echoHTTP(t *testing.T) { + req, err := http.NewRequest("GET", "http://localhost:8084?hello=world", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, "hello world", string(b)) + + err = r.Body.Close() + assert.NoError(t, err) +} + +func resetTest(t *testing.T) { + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + // WorkerList contains list of workers. + + var ret bool + err = client.Call("resetter.Reset", "http", &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + var services []string + err = client.Call("resetter.List", nil, &services) + assert.NoError(t, err) + if services[0] != "http" { + t.Fatal("no enough services") + } +} + +func informerTest(t *testing.T) { + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + // WorkerList contains list of workers. + list := struct { + // Workers is list of workers. + Workers []roadrunner.ProcessState `json:"workers"` + }{} + + err = client.Call("informer.Workers", "http", &list) + assert.NoError(t, err) + assert.Len(t, list.Workers, 12) +} + func TestSSL(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) assert.NoError(t, err) |