diff --git a/integration-cli/docker_cli_logs_test.go b/integration-cli/docker_cli_logs_test.go index 9e4233a0fe..e18916db91 100644 --- a/integration-cli/docker_cli_logs_test.go +++ b/integration-cli/docker_cli_logs_test.go @@ -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) { diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 9bb025a05a..4a816bb6a4 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -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) } diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go index c1bb8915f6..5cf8097111 100644 --- a/integration-cli/docker_utils.go +++ b/integration-cli/docker_utils.go @@ -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) + } + } +}