summaryrefslogtreecommitdiff
path: root/service/limit
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-05-05 12:29:12 +0300
committerWolfy-J <[email protected]>2019-05-05 12:29:12 +0300
commite6acf9d57099c69ce58b1121716528825095814f (patch)
treee072766b682fa40521aeb4661094ea0779bb1123 /service/limit
parentee12d7b834b501beed56945a54fdabe8bbdd4570 (diff)
testing limits
Diffstat (limited to 'service/limit')
-rw-r--r--service/limit/controller.go18
-rw-r--r--service/limit/service_test.go182
2 files changed, 191 insertions, 9 deletions
diff --git a/service/limit/controller.go b/service/limit/controller.go
index 706197fa..c11f4b91 100644
--- a/service/limit/controller.go
+++ b/service/limit/controller.go
@@ -11,14 +11,14 @@ const (
// EventMaxMemory caused when worker consumes more memory than allowed.
EventMaxMemory = iota + 8000
- // EventMaxTTL thrown when worker is removed due TTL being reached. Context is rr.WorkerError
- EventMaxTTL
+ // EventTTL thrown when worker is removed due TTL being reached. Context is rr.WorkerError
+ EventTTL
- // EventMaxIdleTTL triggered when worker spends too much time at rest.
- EventMaxIdleTTL
+ // EventIdleTTL triggered when worker spends too much time at rest.
+ EventIdleTTL
- // EventMaxExecTTL triggered when worker spends too much time doing the task (max_execution_time).
- EventMaxExecTTL
+ // EventExecTTL triggered when worker spends too much time doing the task (max_execution_time).
+ EventExecTTL
)
// handles controller events
@@ -67,7 +67,7 @@ func (c *controller) control(p roadrunner.Pool) {
// make sure worker still on initial request
if p.Remove(w, err) && w.State().NumExecs() == eID {
go w.Kill()
- c.report(EventMaxExecTTL, w, err)
+ c.report(EventExecTTL, w, err)
}
}
}
@@ -80,7 +80,7 @@ func (c *controller) control(p roadrunner.Pool) {
) {
err := fmt.Errorf("max idle time reached (%vs)", c.cfg.IdleTTL)
if p.Remove(w, err) {
- c.report(EventMaxIdleTTL, w, err)
+ c.report(EventIdleTTL, w, err)
}
}
}
@@ -103,7 +103,7 @@ func (c *controller) loadWorkers(p roadrunner.Pool) {
if c.cfg.TTL != 0 && now.Sub(w.Created).Seconds() >= float64(c.cfg.TTL) {
err := fmt.Errorf("max TTL reached (%vs)", c.cfg.TTL)
if p.Remove(w, err) {
- c.report(EventMaxTTL, w, err)
+ c.report(EventTTL, w, err)
}
continue
}
diff --git a/service/limit/service_test.go b/service/limit/service_test.go
index 87c8046b..1d751442 100644
--- a/service/limit/service_test.go
+++ b/service/limit/service_test.go
@@ -93,6 +93,188 @@ func Test_Service_PidEcho(t *testing.T) {
assert.Equal(t, getPID(s), string(b))
}
+func Test_Service_ListenerPlusTTL(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)
+
+ l, _ := c.Get(ID)
+ captured := make(chan interface{})
+ l.(*Service).AddListener(func(event int, ctx interface{}) {
+ if event == EventTTL {
+ close(captured)
+ }
+ })
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ lastPID := getPID(s)
+
+ 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.Equal(t, lastPID, string(b))
+
+ <-captured
+
+ // clean state
+ req, err = http.NewRequest("GET", "http://localhost:6029?new", nil)
+ assert.NoError(t, err)
+
+ _, err = http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+
+ assert.NotEqual(t, lastPID, getPID(s))
+}
+
+func Test_Service_ListenerPlusIdleTTL(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": {
+ "idleTtl": 1
+ }
+ }
+ }`,
+ }))
+
+ s, _ := c.Get(rrhttp.ID)
+ assert.NotNil(t, s)
+
+ l, _ := c.Get(ID)
+ captured := make(chan interface{})
+ l.(*Service).AddListener(func(event int, ctx interface{}) {
+ if event == EventIdleTTL {
+ close(captured)
+ }
+ })
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ lastPID := getPID(s)
+
+ 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, lastPID, string(b))
+
+ <-captured
+
+ // clean state
+ req, err = http.NewRequest("GET", "http://localhost:6029?new", nil)
+ assert.NoError(t, err)
+
+ _, err = http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+
+ assert.NotEqual(t, lastPID, getPID(s))
+}
+
+func Test_Service_Listener_MaxExecTTL(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 stuck pipes",
+ "pool": {"numWorkers": 1}
+ }
+ }`,
+ limitCfg: `{
+ "services": {
+ "http": {
+ "execTTL": 1
+ }
+ }
+ }`,
+ }))
+
+ s, _ := c.Get(rrhttp.ID)
+ assert.NotNil(t, s)
+
+ l, _ := c.Get(ID)
+ captured := make(chan interface{})
+ l.(*Service).AddListener(func(event int, ctx interface{}) {
+ if event == EventExecTTL {
+ close(captured)
+ }
+ })
+
+ 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)
+ assert.Equal(t, 500, r.StatusCode)
+
+ <-captured
+}
+
func getPID(s interface{}) string {
w := s.(*rrhttp.Service).Server().Workers()[0]
return fmt.Sprintf("%v", *w.Pid)