daemon/logger/loggerutils: add TestFollowLogsClose
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
[kolyshkin@: test case Brian Goff, changelog and all bugs are by me]
Source:
@@ -4,6 +4,8 @@ import (
"bufio"
"context"
"io"
+ "io/ioutil"
+ "os"
"strings"
"testing"
"time"
@@ -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")
+ }
+}
