Browse code

remove some uneeded sleeps in tests

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2015/04/22 10:51:41
Showing 2 changed files
... ...
@@ -1,28 +1,46 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"bufio"
4 5
 	"bytes"
5 6
 	"fmt"
6 7
 	"net/http"
7 8
 	"os/exec"
9
+	"strings"
10
+	"time"
8 11
 
9 12
 	"github.com/go-check/check"
10 13
 )
11 14
 
12 15
 func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {
13
-	name := "logs_test"
14
-
15
-	runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "--name", name, "busybox", "bin/sh", "-c", "sleep 10 && echo "+name)
16
-	if out, _, err := runCommandWithOutput(runCmd); err != nil {
17
-		c.Fatal(out, err)
16
+	out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
17
+	id := strings.TrimSpace(out)
18
+	if err := waitRun(id); err != nil {
19
+		c.Fatal(err)
18 20
 	}
19 21
 
20
-	status, body, err := sockRequest("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", name), nil)
21
-	c.Assert(status, check.Equals, http.StatusOK)
22
-	c.Assert(err, check.IsNil)
23
-
24
-	if !bytes.Contains(body, []byte(name)) {
25
-		c.Fatalf("Expected %s, got %s", name, string(body[:]))
22
+	type logOut struct {
23
+		out    string
24
+		status int
25
+		err    error
26
+	}
27
+	chLog := make(chan logOut)
28
+
29
+	go func() {
30
+		statusCode, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", id), nil, "")
31
+		out, _ := bufio.NewReader(body).ReadString('\n')
32
+		chLog <- logOut{strings.TrimSpace(out), statusCode, err}
33
+	}()
34
+
35
+	select {
36
+	case l := <-chLog:
37
+		c.Assert(l.status, check.Equals, http.StatusOK)
38
+		c.Assert(l.err, check.IsNil)
39
+		if !strings.HasSuffix(l.out, "hello") {
40
+			c.Fatalf("expected log output to container 'hello', but it does not")
41
+		}
42
+	case <-time.After(2 * time.Second):
43
+		c.Fatal("timeout waiting for logs to exit")
26 44
 	}
27 45
 }
28 46
 
... ...
@@ -44,19 +44,29 @@ func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
44 44
 
45 45
 // blocking wait with 0 exit code
46 46
 func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
47
+	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' SIGTERM; while true; do sleep 0.01; done")
48
+	containerID := strings.TrimSpace(out)
47 49
 
48
-	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
49
-	out, _, err := runCommandWithOutput(runCmd)
50
-	if err != nil {
51
-		c.Fatal(out, err)
50
+	if err := waitRun(containerID); err != nil {
51
+		c.Fatal(err)
52 52
 	}
53
-	containerID := strings.TrimSpace(out)
54 53
 
55
-	runCmd = exec.Command(dockerBinary, "wait", containerID)
56
-	out, _, err = runCommandWithOutput(runCmd)
54
+	chWait := make(chan string)
55
+	go func() {
56
+		out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
57
+		chWait <- out
58
+	}()
57 59
 
58
-	if err != nil || strings.TrimSpace(out) != "0" {
59
-		c.Fatal("failed to set up container", out, err)
60
+	time.Sleep(100 * time.Millisecond)
61
+	dockerCmd(c, "stop", containerID)
62
+
63
+	select {
64
+	case status := <-chWait:
65
+		if strings.TrimSpace(status) != "0" {
66
+			c.Fatalf("expected exit 0, got %s", status)
67
+		}
68
+	case <-time.After(2 * time.Second):
69
+		c.Fatal("timeout waiting for `docker wait` to exit")
60 70
 	}
61 71
 
62 72
 }
... ...
@@ -97,19 +107,30 @@ func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
97 97
 
98 98
 // blocking wait with random exit code
99 99
 func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
100
-
101
-	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10; exit 99")
102
-	out, _, err := runCommandWithOutput(runCmd)
103
-	if err != nil {
104
-		c.Fatal(out, err)
105
-	}
100
+	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "trap 'exit 99' SIGTERM; while true; do sleep 0.01; done")
106 101
 	containerID := strings.TrimSpace(out)
102
+	if err := waitRun(containerID); err != nil {
103
+		c.Fatal(err)
104
+	}
105
+	if err := waitRun(containerID); err != nil {
106
+		c.Fatal(err)
107
+	}
107 108
 
108
-	runCmd = exec.Command(dockerBinary, "wait", containerID)
109
-	out, _, err = runCommandWithOutput(runCmd)
109
+	chWait := make(chan string)
110
+	go func() {
111
+		out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
112
+		chWait <- out
113
+	}()
110 114
 
111
-	if err != nil || strings.TrimSpace(out) != "99" {
112
-		c.Fatal("failed to set up container", out, err)
113
-	}
115
+	time.Sleep(100 * time.Millisecond)
116
+	dockerCmd(c, "stop", containerID)
114 117
 
118
+	select {
119
+	case status := <-chWait:
120
+		if strings.TrimSpace(status) != "99" {
121
+			c.Fatalf("expected exit 99, got %s", status)
122
+		}
123
+	case <-time.After(2 * time.Second):
124
+		c.Fatal("timeout waiting for `docker wait` to exit")
125
+	}
115 126
 }