From df4c11fb59b8ffca369f14eededda82884ee4dad Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Wed, 13 Jun 2018 22:24:47 +0300 Subject: more tests + rpc tests --- cmd/rr/http/reset.go | 2 +- cmd/rr/http/workers.go | 2 +- cmd/rr/main.go | 8 +-- service/http/rpc_test.go | 115 +++++++++++++++++++++++++++++++++++++++++++ service/http/service.go | 8 +-- service/http/service_test.go | 69 +++++++++++++++----------- service/http/uploads.go | 2 +- service/rpc/service.go | 4 +- service/static/service.go | 6 +-- 9 files changed, 170 insertions(+), 46 deletions(-) create mode 100644 service/http/rpc_test.go diff --git a/cmd/rr/http/reset.go b/cmd/rr/http/reset.go index 907022c3..d0a42b64 100644 --- a/cmd/rr/http/reset.go +++ b/cmd/rr/http/reset.go @@ -38,7 +38,7 @@ func init() { } func reloadHandler(cmd *cobra.Command, args []string) error { - svc, st := rr.Container.Get(rpc.Name) + svc, st := rr.Container.Get(rpc.ID) if st < service.StatusConfigured { return errors.New("RPC service is not configured") } diff --git a/cmd/rr/http/workers.go b/cmd/rr/http/workers.go index 871d8c86..728c415e 100644 --- a/cmd/rr/http/workers.go +++ b/cmd/rr/http/workers.go @@ -73,7 +73,7 @@ func workersHandler(cmd *cobra.Command, args []string) (err error) { } }() - svc, st := rr.Container.Get(rrpc.Name) + svc, st := rr.Container.Get(rrpc.ID) if st < service.StatusConfigured { return errors.New("RPC service is not configured") } diff --git a/cmd/rr/main.go b/cmd/rr/main.go index 68311da2..5b73d326 100644 --- a/cmd/rr/main.go +++ b/cmd/rr/main.go @@ -42,13 +42,13 @@ var debugMode bool func main() { // provides ability to make local connection to services - rr.Container.Register(rpc.Name, &rpc.Service{}) + rr.Container.Register(rpc.ID, &rpc.Service{}) // http serving - rr.Container.Register(http.Name, &http.Service{}) + rr.Container.Register(http.ID, &http.Service{}) // serving static files - rr.Container.Register(static.Name, &static.Service{}) + rr.Container.Register(static.ID, &static.Service{}) // provides additional verbosity @@ -56,7 +56,7 @@ func main() { rr.CLI.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "debug mode", ) cobra.OnInitialize(func() { if debugMode { - service, _ := rr.Container.Get(http.Name) + service, _ := rr.Container.Get(http.ID) service.(*http.Service).AddListener(debug.NewListener(rr.Logger).Listener) } }) diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go new file mode 100644 index 00000000..ba71f7e1 --- /dev/null +++ b/service/http/rpc_test.go @@ -0,0 +1,115 @@ +package http + +import ( + "testing" + "github.com/sirupsen/logrus/hooks/test" + "github.com/sirupsen/logrus" + "github.com/spiral/roadrunner/service" + "github.com/stretchr/testify/assert" + "time" + "github.com/spiral/roadrunner/service/rpc" + "strconv" +) + +func Test_RPC(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(rpc.ID, &rpc.Service{}) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{ + rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, + httpCfg: `{ + "enable": true, + "address": ":6029", + "maxRequest": 1024, + "uploads": { + "dir": ` + tmpDir() + `, + "forbid": [] + }, + "workers":{ + "command": "php ../../php-src/tests/http/client.php pid pipes", + "relay": "pipes", + "pool": { + "numWorkers": 1, + "allocateTimeout": 10000000, + "destroyTimeout": 10000000 + } + } + }`})) + + s, _ := c.Get(ID) + ss := s.(*Service) + + s2, _ := c.Get(rpc.ID) + rs := s2.(*rpc.Service) + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + res, _, _ := get("http://localhost:6029") + assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), res) + + cl, err := rs.Client() + assert.NoError(t, err) + + r := "" + assert.NoError(t, cl.Call("http.Reset", true, &r)) + assert.Equal(t, "OK", r) + + res2, _, _ := get("http://localhost:6029") + assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), res2) + assert.NotEqual(t, res, res2) +} + +func Test_Workers(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(rpc.ID, &rpc.Service{}) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{ + rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, + httpCfg: `{ + "enable": true, + "address": ":6029", + "maxRequest": 1024, + "uploads": { + "dir": ` + tmpDir() + `, + "forbid": [] + }, + "workers":{ + "command": "php ../../php-src/tests/http/client.php pid pipes", + "relay": "pipes", + "pool": { + "numWorkers": 1, + "allocateTimeout": 10000000, + "destroyTimeout": 10000000 + } + } + }`})) + + s, _ := c.Get(ID) + ss := s.(*Service) + + s2, _ := c.Get(rpc.ID) + rs := s2.(*rpc.Service) + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + cl, err := rs.Client() + assert.NoError(t, err) + + r := &WorkerList{} + assert.NoError(t, cl.Call("http.Workers", true, &r)) + assert.Len(t, r.Workers, 1) + + assert.Equal(t, *ss.rr.Workers()[0].Pid, r.Workers[0].Pid) +} diff --git a/service/http/service.go b/service/http/service.go index 1823df53..8485cba6 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -9,8 +9,8 @@ import ( "sync" ) -// Name contains default svc name. -const Name = "http" +// ID contains default svc name. +const ID = "http" // must return true if request/response pair is handled withing the middleware. type middleware func(w http.ResponseWriter, r *http.Request) bool @@ -56,9 +56,9 @@ func (s *Service) Init(cfg service.Config, c service.Container) (bool, error) { s.cfg = config // registering http RPC interface - if r, ok := c.Get(rpc.Name); ok >= service.StatusConfigured { + if r, ok := c.Get(rpc.ID); ok >= service.StatusConfigured { if h, ok := r.(*rpc.Service); ok { - h.Register(Name, &rpcServer{s}) + h.Register(ID, &rpcServer{s}) } } diff --git a/service/http/service_test.go b/service/http/service_test.go index 773bcb24..ebcd0f5a 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -12,18 +12,27 @@ import ( "net/http" "io/ioutil" "github.com/spiral/roadrunner" + "github.com/spiral/roadrunner/service/rpc" ) -type testCfg struct{ httpCfg string } +type testCfg struct { + httpCfg string + rpcCfg string + target string +} func (cfg *testCfg) Get(name string) service.Config { - if name == Name { - return &testCfg{cfg.httpCfg} + if name == ID { + return &testCfg{target: cfg.httpCfg} + } + + if name == rpc.ID { + return &testCfg{target: cfg.rpcCfg} } return nil } func (cfg *testCfg) Unmarshal(out interface{}) error { - return json.Unmarshal([]byte(cfg.httpCfg), out) + return json.Unmarshal([]byte(cfg.target), out) } func Test_Service_NoConfig(t *testing.T) { @@ -31,11 +40,11 @@ func Test_Service_NoConfig(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{}`})) + assert.NoError(t, c.Init(&testCfg{httpCfg: `{}`})) - s, st := c.Get(Name) + s, st := c.Get(ID) assert.NotNil(t, s) assert.Equal(t, service.StatusRegistered, st) } @@ -45,9 +54,9 @@ func Test_Service_Configure_Disable(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{ + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ "enable": false, "address": ":8070", "maxRequest": 1024, @@ -66,7 +75,7 @@ func Test_Service_Configure_Disable(t *testing.T) { } }`})) - s, st := c.Get(Name) + s, st := c.Get(ID) assert.NotNil(t, s) assert.Equal(t, service.StatusRegistered, st) } @@ -76,9 +85,9 @@ func Test_Service_Configure_Enable(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{ + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": ":8070", "maxRequest": 1024, @@ -97,7 +106,7 @@ func Test_Service_Configure_Enable(t *testing.T) { } }`})) - s, st := c.Get(Name) + s, st := c.Get(ID) assert.NotNil(t, s) assert.Equal(t, service.StatusConfigured, st) } @@ -107,9 +116,9 @@ func Test_Service_Echo(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{ + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": ":6029", "maxRequest": 1024, @@ -128,7 +137,7 @@ func Test_Service_Echo(t *testing.T) { } }`})) - s, st := c.Get(Name) + s, st := c.Get(ID) assert.NotNil(t, s) assert.Equal(t, service.StatusConfigured, st) @@ -159,9 +168,9 @@ func Test_Service_Middleware(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{ + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": ":6029", "maxRequest": 1024, @@ -180,7 +189,7 @@ func Test_Service_Middleware(t *testing.T) { } }`})) - s, st := c.Get(Name) + s, st := c.Get(ID) assert.NotNil(t, s) assert.Equal(t, service.StatusConfigured, st) @@ -232,9 +241,9 @@ func Test_Service_Listener(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{ + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": ":6029", "maxRequest": 1024, @@ -253,7 +262,7 @@ func Test_Service_Listener(t *testing.T) { } }`})) - s, st := c.Get(Name) + s, st := c.Get(ID) assert.NotNil(t, s) assert.Equal(t, service.StatusConfigured, st) @@ -276,9 +285,9 @@ func Test_Service_Error(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{ + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": ":6029", "maxRequest": 1024, @@ -305,9 +314,9 @@ func Test_Service_Error2(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{`{ + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": ":6029", "maxRequest": 1024, @@ -334,9 +343,9 @@ func Test_Service_Error3(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.Error(t, c.Init(&testCfg{`{ + assert.Error(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": ":6029", "maxRequest": 1024, @@ -361,9 +370,9 @@ func Test_Service_Error4(t *testing.T) { logger.SetLevel(logrus.DebugLevel) c := service.NewContainer(logger) - c.Register(Name, &Service{}) + c.Register(ID, &Service{}) - assert.Error(t, c.Init(&testCfg{`{ + assert.Error(t, c.Init(&testCfg{httpCfg: `{ "enable": true, "address": "----", "maxRequest": 1024, diff --git a/service/http/uploads.go b/service/http/uploads.go index beb1a946..f8334c30 100644 --- a/service/http/uploads.go +++ b/service/http/uploads.go @@ -70,7 +70,7 @@ func (u *Uploads) Clear() { // FileUpload represents singular file NewUpload. type FileUpload struct { - // Name contains filename specified by the client. + // ID contains filename specified by the client. Name string `json:"name"` // Mime contains mime-type provided by the client. diff --git a/service/rpc/service.go b/service/rpc/service.go index 6e26d3c2..e1147754 100644 --- a/service/rpc/service.go +++ b/service/rpc/service.go @@ -8,8 +8,8 @@ import ( "sync" ) -// Name contains default service name. -const Name = "rpc" +// ID contains default service name. +const ID = "rpc" // Service is RPC service. type Service struct { diff --git a/service/static/service.go b/service/static/service.go index 8d383fcf..1631f610 100644 --- a/service/static/service.go +++ b/service/static/service.go @@ -8,8 +8,8 @@ import ( "github.com/spiral/roadrunner/service" ) -// Name contains default service name. -const Name = "static" +// ID contains default service name. +const ID = "static" // Service serves static files. Potentially convert into middleware? type Service struct { @@ -40,7 +40,7 @@ func (s *Service) Init(cfg service.Config, c service.Container) (enabled bool, e s.root = http.Dir(s.cfg.Dir) // registering as middleware - if h, ok := c.Get(rrttp.Name); ok >= service.StatusConfigured { + if h, ok := c.Get(rrttp.ID); ok >= service.StatusConfigured { if h, ok := h.(*rrttp.Service); ok { h.AddMiddleware(s.middleware) } -- cgit v1.2.3