Browse code

Merge pull request #26020 from tonistiigi/fix-eventsbefore-test

Fix flaky TestEventsContainerFilterBeforeCreate

Vincent Demeester authored on 2016/08/31 05:24:52
Showing 1 changed files
... ...
@@ -4,6 +4,7 @@ package main
4 4
 
5 5
 import (
6 6
 	"bufio"
7
+	"bytes"
7 8
 	"fmt"
8 9
 	"io/ioutil"
9 10
 	"os"
... ...
@@ -140,36 +141,27 @@ func (s *DockerSuite) TestEventsContainerFilterByName(c *check.C) {
140 140
 // #18453
141 141
 func (s *DockerSuite) TestEventsContainerFilterBeforeCreate(c *check.C) {
142 142
 	testRequires(c, DaemonIsLinux)
143
-	var (
144
-		out string
145
-		ch  chan struct{}
146
-	)
147
-	ch = make(chan struct{})
148
-
149
-	// calculate the time it takes to create and start a container and sleep 2 seconds
150
-	// this is to make sure the docker event will recevie the event of container
151
-	since := daemonTime(c)
152
-	id, _ := dockerCmd(c, "run", "-d", "busybox", "top")
143
+	buf := &bytes.Buffer{}
144
+	cmd := exec.Command(dockerBinary, "events", "-f", "container=foo", "--since=0")
145
+	cmd.Stdout = buf
146
+	c.Assert(cmd.Start(), check.IsNil)
147
+	defer cmd.Wait()
148
+	defer cmd.Process.Kill()
149
+
150
+	// Sleep for a second to make sure we are testing the case where events are listened before container starts.
151
+	time.Sleep(time.Second)
152
+	id, _ := dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
153 153
 	cID := strings.TrimSpace(id)
154
-	waitRun(cID)
155
-	time.Sleep(2 * time.Second)
156
-	duration := daemonTime(c).Sub(since)
157
-
158
-	go func() {
159
-		// start events and wait for future events to
160
-		// make sure the new container shows up even when
161
-		// the event stream was created before the container.
162
-		t := daemonTime(c).Add(2 * duration)
163
-		out, _ = dockerCmd(c, "events", "-f", "container=foo", "--since=0", "--until", parseEventTime(t))
164
-		close(ch)
165
-	}()
166
-	// Sleep 2 second to wait docker event to start
167
-	time.Sleep(2 * time.Second)
168
-	id, _ = dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
169
-	cID = strings.TrimSpace(id)
170
-	waitRun(cID)
171
-	<-ch
172
-	c.Assert(out, checker.Contains, cID, check.Commentf("Missing event of container (foo)"))
154
+	for i := 0; ; i++ {
155
+		out := buf.String()
156
+		if strings.Contains(out, cID) {
157
+			break
158
+		}
159
+		if i > 30 {
160
+			c.Fatalf("Missing event of container (foo, %v), got %q", cID, out)
161
+		}
162
+		time.Sleep(500 * time.Millisecond)
163
+	}
173 164
 }
174 165
 
175 166
 func (s *DockerSuite) TestVolumeEvents(c *check.C) {