diff options
author | Wolfy-J <[email protected]> | 2019-05-05 12:06:04 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-05-05 12:06:04 +0300 |
commit | ee12d7b834b501beed56945a54fdabe8bbdd4570 (patch) | |
tree | b4eb26e5fba83124f7eab3979bd4e13e7d4a190e /service | |
parent | db838d7465cc6b147c5d7f672d71c6523df54209 (diff) |
init service
Diffstat (limited to 'service')
-rw-r--r-- | service/container.go | 2 | ||||
-rw-r--r-- | service/container_test.go | 8 | ||||
-rw-r--r-- | service/entry.go | 4 | ||||
-rw-r--r-- | service/http/service_test.go | 4 | ||||
-rw-r--r-- | service/limit/controller.go | 20 | ||||
-rw-r--r-- | service/limit/service.go | 1 | ||||
-rw-r--r-- | service/limit/service_test.go | 99 | ||||
-rw-r--r-- | service/static/service_test.go | 2 |
8 files changed, 119 insertions, 21 deletions
diff --git a/service/container.go b/service/container.go index ea1819d8..1cf4a5f6 100644 --- a/service/container.go +++ b/service/container.go @@ -94,7 +94,7 @@ func (c *container) Register(name string, service interface{}) { c.services = append(c.services, &entry{ name: name, svc: service, - status: StatusRegistered, + status: StatusInactive, }) } diff --git a/service/container_test.go b/service/container_test.go index 99cb75a3..8fcaede2 100644 --- a/service/container_test.go +++ b/service/container_test.go @@ -139,7 +139,7 @@ func TestContainer_Get(t *testing.T) { s, st := c.Get("test") assert.IsType(t, &testService{}, s) - assert.Equal(t, StatusRegistered, st) + assert.Equal(t, StatusInactive, st) s, st = c.Get("another") assert.Nil(t, s) @@ -227,7 +227,7 @@ func TestContainer_ConfigureNull(t *testing.T) { s, st := c.Get("test") assert.IsType(t, &testService{}, s) - assert.Equal(t, StatusRegistered, st) + assert.Equal(t, StatusInactive, st) } func TestContainer_ConfigureDisabled(t *testing.T) { @@ -245,7 +245,7 @@ func TestContainer_ConfigureDisabled(t *testing.T) { s, st := c.Get("test") assert.IsType(t, &testService{}, s) - assert.Equal(t, StatusRegistered, st) + assert.Equal(t, StatusInactive, st) } func TestContainer_ConfigureError(t *testing.T) { @@ -268,7 +268,7 @@ func TestContainer_ConfigureError(t *testing.T) { s, st := c.Get("test") assert.IsType(t, &testService{}, s) - assert.Equal(t, StatusRegistered, st) + assert.Equal(t, StatusInactive, st) } func TestContainer_ConfigureTwice(t *testing.T) { diff --git a/service/entry.go b/service/entry.go index f2cbac28..b8da6514 100644 --- a/service/entry.go +++ b/service/entry.go @@ -8,8 +8,8 @@ const ( // StatusUndefined when service bus can not find the service. StatusUndefined = iota - // StatusRegistered hasStatus setStatus when service has been registered in container. - StatusRegistered + // StatusInactive hasStatus setStatus when service has been registered in container. + StatusInactive // StatusOK hasStatus setStatus when service has been properly configured. StatusOK diff --git a/service/http/service_test.go b/service/http/service_test.go index 5b6d60d8..b3b001c7 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -57,7 +57,7 @@ func Test_Service_NoConfig(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusRegistered, st) + assert.Equal(t, service.StatusInactive, st) } func Test_Service_Configure_Disable(t *testing.T) { @@ -71,7 +71,7 @@ func Test_Service_Configure_Disable(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusRegistered, st) + assert.Equal(t, service.StatusInactive, st) } func Test_Service_Configure_Enable(t *testing.T) { diff --git a/service/limit/controller.go b/service/limit/controller.go index 3e6aed4b..706197fa 100644 --- a/service/limit/controller.go +++ b/service/limit/controller.go @@ -32,11 +32,11 @@ type controllerConfig struct { // TTL defines maximum time worker is allowed to live. TTL int64 - // MaxIdleTTL defines maximum duration worker can spend in idle mode. - MaxIdleTTL int64 + // IdleTTL defines maximum duration worker can spend in idle mode. + IdleTTL int64 - // MaxExecTTL defines maximum lifetime per job. - MaxExecTTL int64 + // ExecTTL defines maximum lifetime per job. + ExecTTL int64 } type controller struct { @@ -56,13 +56,13 @@ func (c *controller) control(p roadrunner.Pool) { now := time.Now() - if c.cfg.MaxExecTTL != 0 { + if c.cfg.ExecTTL != 0 { for _, w := range c.sw.find( roadrunner.StateWorking, - now.Add(-time.Second*time.Duration(c.cfg.MaxExecTTL)), + now.Add(-time.Second*time.Duration(c.cfg.ExecTTL)), ) { eID := w.State().NumExecs() - err := fmt.Errorf("max exec time reached (%vs)", c.cfg.MaxExecTTL) + err := fmt.Errorf("max exec time reached (%vs)", c.cfg.ExecTTL) // make sure worker still on initial request if p.Remove(w, err) && w.State().NumExecs() == eID { @@ -73,12 +73,12 @@ func (c *controller) control(p roadrunner.Pool) { } // locale workers which are in idle mode for too long - if c.cfg.MaxIdleTTL != 0 { + if c.cfg.IdleTTL != 0 { for _, w := range c.sw.find( roadrunner.StateReady, - now.Add(-time.Second*time.Duration(c.cfg.MaxIdleTTL)), + now.Add(-time.Second*time.Duration(c.cfg.IdleTTL)), ) { - err := fmt.Errorf("max idle time reached (%vs)", c.cfg.MaxIdleTTL) + err := fmt.Errorf("max idle time reached (%vs)", c.cfg.IdleTTL) if p.Remove(w, err) { c.report(EventMaxIdleTTL, w, err) } diff --git a/service/limit/service.go b/service/limit/service.go index 99e2b1ee..6cbc22be 100644 --- a/service/limit/service.go +++ b/service/limit/service.go @@ -16,7 +16,6 @@ type controllable interface { // Service to control the state of rr service inside other services. type Service struct { - cfg *Config lsns []func(event int, ctx interface{}) } diff --git a/service/limit/service_test.go b/service/limit/service_test.go new file mode 100644 index 00000000..87c8046b --- /dev/null +++ b/service/limit/service_test.go @@ -0,0 +1,99 @@ +package limit + +import ( + "encoding/json" + "fmt" + "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus/hooks/test" + "github.com/spiral/roadrunner/service" + rrhttp "github.com/spiral/roadrunner/service/http" + "github.com/stretchr/testify/assert" + "io/ioutil" + "net/http" + "testing" + "time" +) + +type testCfg struct { + httpCfg string + limitCfg string + target string +} + +func (cfg *testCfg) Get(name string) service.Config { + if name == rrhttp.ID { + if cfg.httpCfg == "" { + return nil + } + + return &testCfg{target: cfg.httpCfg} + } + + if name == ID { + return &testCfg{target: cfg.limitCfg} + } + + return nil +} + +func (cfg *testCfg) Unmarshal(out interface{}) error { + err := json.Unmarshal([]byte(cfg.target), out) + + if cl, ok := out.(*Config); ok { + // to speed up tests + cl.Interval = time.Millisecond + } + + return err +} + +func Test_Service_PidEcho(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(rrhttp.ID, &rrhttp.Service{}) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{ + httpCfg: `{ + "address": ":6029", + "workers":{ + "command": "php ../../tests/http/client.php pid pipes", + "pool": {"numWorkers": 1} + } + }`, + limitCfg: `{ + "services": { + "http": { + "ttl": 1 + } + } + }`, + })) + + s, _ := c.Get(rrhttp.ID) + assert.NotNil(t, s) + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + req, err := http.NewRequest("GET", "http://localhost:6029", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + defer r.Body.Close() + + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + + assert.NoError(t, err) + assert.Equal(t, getPID(s), string(b)) +} + +func getPID(s interface{}) string { + w := s.(*rrhttp.Service).Server().Workers()[0] + return fmt.Sprintf("%v", *w.Pid) +} diff --git a/service/static/service_test.go b/service/static/service_test.go index d69b2fdd..d345b138 100644 --- a/service/static/service_test.go +++ b/service/static/service_test.go @@ -97,7 +97,7 @@ func Test_Disabled(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusRegistered, st) + assert.Equal(t, service.StatusInactive, st) } func Test_Files_Disable(t *testing.T) { |