summaryrefslogtreecommitdiff
path: root/service/limit/service_test.go
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/service_test.go
parentee12d7b834b501beed56945a54fdabe8bbdd4570 (diff)
testing limits
Diffstat (limited to 'service/limit/service_test.go')
-rw-r--r--service/limit/service_test.go182
1 files changed, 182 insertions, 0 deletions
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)