From 4fde1cb6956a5dab266a745235d12f4442eedf7c Mon Sep 17 00:00:00 2001 From: David Calavera Date: Mon, 1 Jun 2015 13:33:03 -0700 Subject: [PATCH] Fix stats flaky test. Signed-off-by: David Calavera --- integration-cli/docker_api_stats.go | 47 ++++++++++------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/integration-cli/docker_api_stats.go b/integration-cli/docker_api_stats.go index 41d2f86083..a2be352e2b 100644 --- a/integration-cli/docker_api_stats.go +++ b/integration-cli/docker_api_stats.go @@ -3,46 +3,31 @@ package main import ( "encoding/json" "fmt" + "strings" + "github.com/docker/docker/api/types" "github.com/go-check/check" - "strings" - "time" ) func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) { out, _ := dockerCmd(c, "run", "-d", "--cpu-quota=2000", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello';done") id := strings.TrimSpace(out) - if err := waitRun(id); err != nil { - c.Fatal(err) - } - ch := make(chan error) + err := waitRun(id) + c.Assert(err, check.IsNil) + + _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "") + c.Assert(err, check.IsNil) + var v *types.Stats - go func() { - _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=1", id), nil, "") - if err != nil { - ch <- err - } - dec := json.NewDecoder(body) - if err := dec.Decode(&v); err != nil { - ch <- err - } - ch <- nil - }() - select { - case e := <-ch: - if e == nil { - var cpuPercent = 0.0 - cpuDelta := float64(v.CpuStats.CpuUsage.TotalUsage - v.PreCpuStats.CpuUsage.TotalUsage) - systemDelta := float64(v.CpuStats.SystemUsage - v.PreCpuStats.SystemUsage) - cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CpuStats.CpuUsage.PercpuUsage)) * 100.0 - if cpuPercent < 1.8 || cpuPercent > 2.2 { - c.Fatal("docker stats with no-stream get cpu usage failed") - } + err = json.NewDecoder(body).Decode(&v) + c.Assert(err, check.IsNil) - } - case <-time.After(4 * time.Second): - c.Fatal("docker stats with no-stream timeout") + var cpuPercent = 0.0 + cpuDelta := float64(v.CpuStats.CpuUsage.TotalUsage - v.PreCpuStats.CpuUsage.TotalUsage) + systemDelta := float64(v.CpuStats.SystemUsage - v.PreCpuStats.SystemUsage) + cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CpuStats.CpuUsage.PercpuUsage)) * 100.0 + if cpuPercent == 0 { + c.Fatalf("docker stats with no-stream get cpu usage failed: was %v", cpuPercent) } - }