diff options
Diffstat (limited to 'service/limit/service_test.go')
-rw-r--r-- | service/limit/service_test.go | 91 |
1 files changed, 53 insertions, 38 deletions
diff --git a/service/limit/service_test.go b/service/limit/service_test.go index 8cb3d7dc..abc03e69 100644 --- a/service/limit/service_test.go +++ b/service/limit/service_test.go @@ -57,7 +57,7 @@ func Test_Service_PidEcho(t *testing.T) { assert.NoError(t, c.Init(&testCfg{ httpCfg: `{ - "address": ":6029", + "address": ":7029", "workers":{ "command": "php ../../tests/http/client.php pid pipes", "pool": {"numWorkers": 1} @@ -81,26 +81,26 @@ func Test_Service_PidEcho(t *testing.T) { t.Errorf("error during the Serve: error %v", err) } }() - time.Sleep(time.Millisecond * 100) - defer c.Stop() - req, err := http.NewRequest("GET", "http://localhost:6029", nil) + time.Sleep(time.Millisecond * 100) + req, err := http.NewRequest("GET", "http://localhost:7029", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer func() { - err := r.Body.Close() - if err != nil { - t.Errorf("error during the body closing: error %v", err) - } - }() + b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) assert.NoError(t, err) assert.Equal(t, getPID(s), string(b)) + + err2 := r.Body.Close() + if err2 != nil { + t.Errorf("error during the body closing: error %v", err2) + } + c.Stop() } func Test_Service_ListenerPlusTTL(t *testing.T) { @@ -113,7 +113,7 @@ func Test_Service_ListenerPlusTTL(t *testing.T) { assert.NoError(t, c.Init(&testCfg{ httpCfg: `{ - "address": ":6029", + "address": ":7030", "workers":{ "command": "php ../../tests/http/client.php pid pipes", "pool": {"numWorkers": 1} @@ -145,22 +145,18 @@ func Test_Service_ListenerPlusTTL(t *testing.T) { t.Errorf("error during the Serve: error %v", err) } }() + + time.Sleep(time.Millisecond * 100) - defer c.Stop() lastPID := getPID(s) - req, err := http.NewRequest("GET", "http://localhost:6029", nil) + req, err := http.NewRequest("GET", "http://localhost:7030", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer func() { - err := r.Body.Close() - if err != nil { - t.Errorf("error during the body closing: error %v", err) - } - }() + b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -169,13 +165,20 @@ func Test_Service_ListenerPlusTTL(t *testing.T) { <-captured // clean state - req, err = http.NewRequest("GET", "http://localhost:6029?new", nil) + req, err = http.NewRequest("GET", "http://localhost:7030?new", nil) assert.NoError(t, err) _, err = http.DefaultClient.Do(req) assert.NoError(t, err) assert.NotEqual(t, lastPID, getPID(s)) + + c.Stop() + + err2 := r.Body.Close() + if err2 != nil { + t.Errorf("error during the body closing: error %v", err2) + } } func Test_Service_ListenerPlusIdleTTL(t *testing.T) { @@ -188,7 +191,7 @@ func Test_Service_ListenerPlusIdleTTL(t *testing.T) { assert.NoError(t, c.Init(&testCfg{ httpCfg: `{ - "address": ":6029", + "address": ":7031", "workers":{ "command": "php ../../tests/http/client.php pid pipes", "pool": {"numWorkers": 1} @@ -220,22 +223,17 @@ func Test_Service_ListenerPlusIdleTTL(t *testing.T) { t.Errorf("error during the Serve: error %v", err) } }() + + time.Sleep(time.Millisecond * 100) - defer c.Stop() lastPID := getPID(s) - req, err := http.NewRequest("GET", "http://localhost:6029", nil) + req, err := http.NewRequest("GET", "http://localhost:7031", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) assert.NoError(t, err) - defer func() { - err := r.Body.Close() - if err != nil { - t.Errorf("error during the body closing: error %v", err) - } - }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -246,13 +244,19 @@ func Test_Service_ListenerPlusIdleTTL(t *testing.T) { <-captured // clean state - req, err = http.NewRequest("GET", "http://localhost:6029?new", nil) + req, err = http.NewRequest("GET", "http://localhost:7031?new", nil) assert.NoError(t, err) _, err = http.DefaultClient.Do(req) assert.NoError(t, err) assert.NotEqual(t, lastPID, getPID(s)) + + c.Stop() + err2 := r.Body.Close() + if err2 != nil { + t.Errorf("error during the body closing: error %v", err2) + } } func Test_Service_Listener_MaxExecTTL(t *testing.T) { @@ -265,7 +269,7 @@ func Test_Service_Listener_MaxExecTTL(t *testing.T) { assert.NoError(t, c.Init(&testCfg{ httpCfg: `{ - "address": ":6029", + "address": ":7032", "workers":{ "command": "php ../../tests/http/client.php stuck pipes", "pool": {"numWorkers": 1} @@ -297,10 +301,10 @@ func Test_Service_Listener_MaxExecTTL(t *testing.T) { t.Errorf("error during the Serve: error %v", err) } }() + time.Sleep(time.Millisecond * 100) - defer c.Stop() - req, err := http.NewRequest("GET", "http://localhost:6029", nil) + req, err := http.NewRequest("GET", "http://localhost:7032", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) @@ -308,6 +312,8 @@ func Test_Service_Listener_MaxExecTTL(t *testing.T) { assert.Equal(t, 500, r.StatusCode) <-captured + + c.Stop() } func Test_Service_Listener_MaxMemoryUsage(t *testing.T) { @@ -320,7 +326,7 @@ func Test_Service_Listener_MaxMemoryUsage(t *testing.T) { assert.NoError(t, c.Init(&testCfg{ httpCfg: `{ - "address": ":6029", + "address": ":7033", "workers":{ "command": "php ../../tests/http/client.php memleak pipes", "pool": {"numWorkers": 1} @@ -354,12 +360,12 @@ func Test_Service_Listener_MaxMemoryUsage(t *testing.T) { t.Errorf("error during the Serve: error %v", err) } }() + time.Sleep(time.Millisecond * 100) - defer c.Stop() lastPID := getPID(s) - req, err := http.NewRequest("GET", "http://localhost:6029", nil) + req, err := http.NewRequest("GET", "http://localhost:7033", nil) assert.NoError(t, err) for { @@ -367,19 +373,28 @@ func Test_Service_Listener_MaxMemoryUsage(t *testing.T) { case <-captured: _, err := http.DefaultClient.Do(req) if err != nil { + c.Stop() t.Errorf("error during sending the http request: error %v", err) } assert.NotEqual(t, lastPID, getPID(s)) + c.Stop() return default: _, err := http.DefaultClient.Do(req) if err != nil { + c.Stop() t.Errorf("error during sending the http request: error %v", err) } + c.Stop() + return } } } func getPID(s interface{}) string { - w := s.(*rrhttp.Service).Server().Workers()[0] - return fmt.Sprintf("%v", *w.Pid) + if len(s.(*rrhttp.Service).Server().Workers()) > 0 { + w := s.(*rrhttp.Service).Server().Workers()[0] + return fmt.Sprintf("%v", *w.Pid) + } else { + panic("no workers") + } } |