Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
daemon/logger/loggerutils: add TestFollowLogsClose
Browse files Browse the repository at this point in the history
This test case checks that followLogs() exits once the reader is gone.
Currently it does not (i.e. this test is supposed to fail) due to moby#37391.

[kolyshkin@: test case Brian Goff, changelog and all bugs are by me]
Source: https://gist.github.com/cpuguy83/e538793de18c762608358ee0eaddc197

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
cpuguy83 authored and kolyshkin committed Sep 6, 2018
1 parent 2e4c2a6 commit d37a11b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions daemon/logger/loggerutils/logfile_test.go
Expand Up @@ -4,6 +4,8 @@ import (
"bufio"
"context"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -74,3 +76,44 @@ func TestTailFiles(t *testing.T) {
assert.Assert(t, string(msg.Line) == "Where we're going we don't need roads.", string(msg.Line))
}
}

func TestFollowLogsClose(t *testing.T) {
lw := logger.NewLogWatcher()

f, err := ioutil.TempFile("", t.Name())
assert.NilError(t, err)
defer func() {
f.Close()
os.Remove(f.Name())
}()

makeDecoder := func(rdr io.Reader) func() (*logger.Message, error) {
return func() (*logger.Message, error) {
return &logger.Message{}, nil
}
}

followLogsDone := make(chan struct{})
var since, until time.Time
go func() {
followLogs(f, lw, make(chan interface{}), makeDecoder, since, until)
close(followLogsDone)
}()

select {
case <-lw.Msg:
case err := <-lw.Err:
assert.NilError(t, err)
case <-followLogsDone:
t.Fatal("follow logs finished unexpectedly")
case <-time.After(10 * time.Second):
t.Fatal("timeout waiting for log message")
}

lw.Close()
select {
case <-followLogsDone:
case <-time.After(20 * time.Second):
t.Fatal("timeout waiting for followLogs() to finish")
}
}

0 comments on commit d37a11b

Please sign in to comment.