Procházet zdrojové kódy

Merge pull request #12616 from cpuguy83/remove_uneeded_sleeps

remove some uneeded sleeps in tests
Doug Davis před 10 roky
rodič
revize
325c0404c8

+ 29 - 11
integration-cli/docker_api_logs_test.go

@@ -1,28 +1,46 @@
 package main
 package main
 
 
 import (
 import (
+	"bufio"
 	"bytes"
 	"bytes"
 	"fmt"
 	"fmt"
 	"net/http"
 	"net/http"
 	"os/exec"
 	"os/exec"
+	"strings"
+	"time"
 
 
 	"github.com/go-check/check"
 	"github.com/go-check/check"
 )
 )
 
 
 func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {
 func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {
-	name := "logs_test"
-
-	runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "--name", name, "busybox", "bin/sh", "-c", "sleep 10 && echo "+name)
-	if out, _, err := runCommandWithOutput(runCmd); err != nil {
-		c.Fatal(out, err)
+	out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
+	id := strings.TrimSpace(out)
+	if err := waitRun(id); err != nil {
+		c.Fatal(err)
 	}
 	}
 
 
-	status, body, err := sockRequest("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", name), nil)
-	c.Assert(status, check.Equals, http.StatusOK)
-	c.Assert(err, check.IsNil)
-
-	if !bytes.Contains(body, []byte(name)) {
-		c.Fatalf("Expected %s, got %s", name, string(body[:]))
+	type logOut struct {
+		out    string
+		status int
+		err    error
+	}
+	chLog := make(chan logOut)
+
+	go func() {
+		statusCode, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", id), nil, "")
+		out, _ := bufio.NewReader(body).ReadString('\n')
+		chLog <- logOut{strings.TrimSpace(out), statusCode, err}
+	}()
+
+	select {
+	case l := <-chLog:
+		c.Assert(l.status, check.Equals, http.StatusOK)
+		c.Assert(l.err, check.IsNil)
+		if !strings.HasSuffix(l.out, "hello") {
+			c.Fatalf("expected log output to container 'hello', but it does not")
+		}
+	case <-time.After(2 * time.Second):
+		c.Fatal("timeout waiting for logs to exit")
 	}
 	}
 }
 }
 
 

+ 41 - 20
integration-cli/docker_cli_wait_test.go

@@ -44,19 +44,29 @@ func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
 
 
 // blocking wait with 0 exit code
 // blocking wait with 0 exit code
 func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
 func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
+	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' SIGTERM; while true; do sleep 0.01; done")
+	containerID := strings.TrimSpace(out)
 
 
-	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
-	out, _, err := runCommandWithOutput(runCmd)
-	if err != nil {
-		c.Fatal(out, err)
+	if err := waitRun(containerID); err != nil {
+		c.Fatal(err)
 	}
 	}
-	containerID := strings.TrimSpace(out)
 
 
-	runCmd = exec.Command(dockerBinary, "wait", containerID)
-	out, _, err = runCommandWithOutput(runCmd)
+	chWait := make(chan string)
+	go func() {
+		out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
+		chWait <- out
+	}()
 
 
-	if err != nil || strings.TrimSpace(out) != "0" {
-		c.Fatal("failed to set up container", out, err)
+	time.Sleep(100 * time.Millisecond)
+	dockerCmd(c, "stop", containerID)
+
+	select {
+	case status := <-chWait:
+		if strings.TrimSpace(status) != "0" {
+			c.Fatalf("expected exit 0, got %s", status)
+		}
+	case <-time.After(2 * time.Second):
+		c.Fatal("timeout waiting for `docker wait` to exit")
 	}
 	}
 
 
 }
 }
@@ -97,19 +107,30 @@ func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
 
 
 // blocking wait with random exit code
 // blocking wait with random exit code
 func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
 func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
-
-	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10; exit 99")
-	out, _, err := runCommandWithOutput(runCmd)
-	if err != nil {
-		c.Fatal(out, err)
-	}
+	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "trap 'exit 99' SIGTERM; while true; do sleep 0.01; done")
 	containerID := strings.TrimSpace(out)
 	containerID := strings.TrimSpace(out)
+	if err := waitRun(containerID); err != nil {
+		c.Fatal(err)
+	}
+	if err := waitRun(containerID); err != nil {
+		c.Fatal(err)
+	}
 
 
-	runCmd = exec.Command(dockerBinary, "wait", containerID)
-	out, _, err = runCommandWithOutput(runCmd)
+	chWait := make(chan string)
+	go func() {
+		out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
+		chWait <- out
+	}()
 
 
-	if err != nil || strings.TrimSpace(out) != "99" {
-		c.Fatal("failed to set up container", out, err)
-	}
+	time.Sleep(100 * time.Millisecond)
+	dockerCmd(c, "stop", containerID)
 
 
+	select {
+	case status := <-chWait:
+		if strings.TrimSpace(status) != "99" {
+			c.Fatalf("expected exit 99, got %s", status)
+		}
+	case <-time.After(2 * time.Second):
+		c.Fatal("timeout waiting for `docker wait` to exit")
+	}
 }
 }