mirror of https://github.com/docker/docs.git
Merge pull request #12616 from cpuguy83/remove_uneeded_sleeps
remove some uneeded sleeps in tests
This commit is contained in:
commit
325c0404c8
|
@ -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"
|
out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
|
||||||
|
id := strings.TrimSpace(out)
|
||||||
runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "--name", name, "busybox", "bin/sh", "-c", "sleep 10 && echo "+name)
|
if err := waitRun(id); err != nil {
|
||||||
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
c.Fatal(err)
|
||||||
c.Fatal(out, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
status, body, err := sockRequest("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", name), nil)
|
type logOut struct {
|
||||||
c.Assert(status, check.Equals, http.StatusOK)
|
out string
|
||||||
c.Assert(err, check.IsNil)
|
status int
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
chLog := make(chan logOut)
|
||||||
|
|
||||||
if !bytes.Contains(body, []byte(name)) {
|
go func() {
|
||||||
c.Fatalf("Expected %s, got %s", name, string(body[:]))
|
statusCode, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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")
|
||||||
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
|
|
||||||
out, _, err := runCommandWithOutput(runCmd)
|
|
||||||
if err != nil {
|
|
||||||
c.Fatal(out, err)
|
|
||||||
}
|
|
||||||
containerID := strings.TrimSpace(out)
|
containerID := strings.TrimSpace(out)
|
||||||
|
|
||||||
runCmd = exec.Command(dockerBinary, "wait", containerID)
|
if err := waitRun(containerID); err != nil {
|
||||||
out, _, err = runCommandWithOutput(runCmd)
|
c.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil || strings.TrimSpace(out) != "0" {
|
chWait := make(chan string)
|
||||||
c.Fatal("failed to set up container", out, err)
|
go func() {
|
||||||
|
out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
|
||||||
|
chWait <- out
|
||||||
|
}()
|
||||||
|
|
||||||
|
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) {
|
||||||
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "trap 'exit 99' SIGTERM; while true; do sleep 0.01; done")
|
||||||
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10; exit 99")
|
|
||||||
out, _, err := runCommandWithOutput(runCmd)
|
|
||||||
if err != nil {
|
|
||||||
c.Fatal(out, err)
|
|
||||||
}
|
|
||||||
containerID := strings.TrimSpace(out)
|
containerID := strings.TrimSpace(out)
|
||||||
|
if err := waitRun(containerID); err != nil {
|
||||||
runCmd = exec.Command(dockerBinary, "wait", containerID)
|
c.Fatal(err)
|
||||||
out, _, err = runCommandWithOutput(runCmd)
|
}
|
||||||
|
if err := waitRun(containerID); err != nil {
|
||||||
if err != nil || strings.TrimSpace(out) != "99" {
|
c.Fatal(err)
|
||||||
c.Fatal("failed to set up container", out, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chWait := make(chan string)
|
||||||
|
go func() {
|
||||||
|
out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
|
||||||
|
chWait <- out
|
||||||
|
}()
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue