summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-13 22:24:47 +0300
committerWolfy-J <[email protected]>2018-06-13 22:24:47 +0300
commitdf4c11fb59b8ffca369f14eededda82884ee4dad (patch)
treec59b4af544cf11008739313a54b878cc20db793d /service
parent235adef3eef67888e4c391b28cedadfb3f8874e3 (diff)
more tests + rpc tests
Diffstat (limited to 'service')
-rw-r--r--service/http/rpc_test.go115
-rw-r--r--service/http/service.go8
-rw-r--r--service/http/service_test.go69
-rw-r--r--service/http/uploads.go2
-rw-r--r--service/rpc/service.go4
-rw-r--r--service/static/service.go6
6 files changed, 164 insertions, 40 deletions
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)
}