integration-cli: move goroutines info helpers to separate funcs

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov 2016-03-14 15:52:50 -07:00
parent 90ce409e35
commit 0c7c9df804
3 changed files with 51 additions and 84 deletions

View file

@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"fmt"
"io"
"os/exec"
@ -265,20 +264,8 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
id := strings.TrimSpace(out)
c.Assert(waitRun(id), checker.IsNil)
type info struct {
NGoroutines int
}
getNGoroutines := func() int {
var i info
status, b, err := sockRequest("GET", "/info", nil)
c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, 200)
c.Assert(json.Unmarshal(b, &i), checker.IsNil)
return i.NGoroutines
}
nroutines := getNGoroutines()
nroutines, err := getGoroutineNumber()
c.Assert(err, checker.IsNil)
cmd := exec.Command(dockerBinary, "logs", "-f", id)
r, w := io.Pipe()
cmd.Stdout = w
@ -295,20 +282,7 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
c.Assert(cmd.Process.Kill(), checker.IsNil)
// NGoroutines is not updated right away, so we need to wait before failing
t := time.After(30 * time.Second)
for {
select {
case <-t:
n := getNGoroutines()
c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
default:
if n := getNGoroutines(); n <= nroutines {
return
}
time.Sleep(200 * time.Millisecond)
}
}
c.Assert(waitForGoroutines(nroutines), checker.IsNil)
}
func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
@ -316,40 +290,15 @@ func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
id := strings.TrimSpace(out)
c.Assert(waitRun(id), checker.IsNil)
type info struct {
NGoroutines int
}
getNGoroutines := func() int {
var i info
status, b, err := sockRequest("GET", "/info", nil)
c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, 200)
c.Assert(json.Unmarshal(b, &i), checker.IsNil)
return i.NGoroutines
}
nroutines := getNGoroutines()
nroutines, err := getGoroutineNumber()
c.Assert(err, checker.IsNil)
cmd := exec.Command(dockerBinary, "logs", "-f", id)
c.Assert(cmd.Start(), checker.IsNil)
time.Sleep(200 * time.Millisecond)
c.Assert(cmd.Process.Kill(), checker.IsNil)
// NGoroutines is not updated right away, so we need to wait before failing
t := time.After(30 * time.Second)
for {
select {
case <-t:
n := getNGoroutines()
c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
default:
if n := getNGoroutines(); n <= nroutines {
return
}
time.Sleep(200 * time.Millisecond)
}
}
c.Assert(waitForGoroutines(nroutines), checker.IsNil)
}
func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {

View file

@ -3,7 +3,6 @@ package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net"
@ -4223,18 +4222,8 @@ func (s *DockerSuite) TestRunNamedVolumesFromNotRemoved(c *check.C) {
}
func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
type info struct {
NGoroutines int
}
getNGoroutines := func() int {
var i info
status, b, err := sockRequest("GET", "/info", nil)
c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, 200)
c.Assert(json.Unmarshal(b, &i), checker.IsNil)
return i.NGoroutines
}
nroutines := getNGoroutines()
nroutines, err := getGoroutineNumber()
c.Assert(err, checker.IsNil)
runSleepingContainer(c, "--name=test", "-p", "8000:8000")
@ -4245,18 +4234,5 @@ func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
dockerCmd(c, "rm", "-f", "test")
// NGoroutines is not updated right away, so we need to wait before failing
t := time.After(30 * time.Second)
for {
select {
case <-t:
n := getNGoroutines()
c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
default:
if n := getNGoroutines(); n <= nroutines {
return
}
time.Sleep(200 * time.Millisecond)
}
}
c.Assert(waitForGoroutines(nroutines), checker.IsNil)
}

View file

@ -1435,3 +1435,45 @@ func minimalBaseImage() string {
}
return "scratch"
}
func getGoroutineNumber() (int, error) {
i := struct {
NGoroutines int
}{}
status, b, err := sockRequest("GET", "/info", nil)
if err != nil {
return 0, err
}
if status != http.StatusOK {
return 0, fmt.Errorf("http status code: %d", status)
}
if err := json.Unmarshal(b, &i); err != nil {
return 0, err
}
return i.NGoroutines, nil
}
func waitForGoroutines(expected int) error {
t := time.After(30 * time.Second)
for {
select {
case <-t:
n, err := getGoroutineNumber()
if err != nil {
return err
}
if n > expected {
return fmt.Errorf("leaked goroutines: expected less than or equal to %d, got: %d", expected, n)
}
default:
n, err := getGoroutineNumber()
if err != nil {
return err
}
if n <= expected {
return nil
}
time.Sleep(200 * time.Millisecond)
}
}
}