Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit deb4910

Browse files
committed
integration-cli: Fix hanging TestLogsFollowGoroutines*
cmd.Wait is called twice from different goroutines which can cause the test to hang completely. Fix by calling Wait only once and sending its return value over a channel. In TestLogsFollowGoroutinesWithStdout also added additional closes and process kills to ensure that we don't leak anything in case test returns early because of failed test assertion. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent fcb5245 commit deb4910

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

integration-cli/docker_cli_logs_test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,33 @@ func (s *DockerCLILogsSuite) TestLogsFollowGoroutinesWithStdout(c *testing.T) {
290290
assert.NilError(c, err)
291291
cmd := exec.Command(dockerBinary, "logs", "-f", id)
292292
r, w := io.Pipe()
293+
defer r.Close()
294+
defer w.Close()
295+
293296
cmd.Stdout = w
294297
assert.NilError(c, cmd.Start())
295-
go cmd.Wait()
298+
defer cmd.Process.Kill()
299+
300+
finished := make(chan error)
301+
go func() {
302+
finished <- cmd.Wait()
303+
}()
296304

297305
// Make sure pipe is written to
298306
chErr := make(chan error)
299307
go func() {
300308
b := make([]byte, 1)
301309
_, err := r.Read(b)
302310
chErr <- err
311+
r.Close()
303312
}()
313+
314+
// Check read from pipe succeeded
304315
assert.NilError(c, <-chErr)
316+
305317
assert.NilError(c, cmd.Process.Kill())
306-
r.Close()
307-
cmd.Wait()
318+
<-finished
319+
308320
// NGoroutines is not updated right away, so we need to wait before failing
309321
assert.NilError(c, waitForGoroutines(nroutines))
310322
}
@@ -318,10 +330,16 @@ func (s *DockerCLILogsSuite) TestLogsFollowGoroutinesNoOutput(c *testing.T) {
318330
assert.NilError(c, err)
319331
cmd := exec.Command(dockerBinary, "logs", "-f", id)
320332
assert.NilError(c, cmd.Start())
321-
go cmd.Wait()
333+
334+
finished := make(chan error)
335+
go func() {
336+
finished <- cmd.Wait()
337+
}()
338+
322339
time.Sleep(200 * time.Millisecond)
323340
assert.NilError(c, cmd.Process.Kill())
324-
cmd.Wait()
341+
342+
<-finished
325343

326344
// NGoroutines is not updated right away, so we need to wait before failing
327345
assert.NilError(c, waitForGoroutines(nroutines))

0 commit comments

Comments
 (0)