From d0e422bda5d8613c7a1555ad1280ba34763717a4 Mon Sep 17 00:00:00 2001 From: Jamie Wilkinson Date: Mon, 22 Apr 2024 19:38:33 +1000 Subject: [PATCH] refactor: Return a single test object from `makeTestTail`. --- internal/tailer/tail_test.go | 96 ++++++++++++++++------------ internal/tailer/tail_unix_test.go | 12 ++-- internal/tailer/tail_windows_test.go | 4 +- 3 files changed, 64 insertions(+), 48 deletions(-) diff --git a/internal/tailer/tail_test.go b/internal/tailer/tail_test.go index ef7c296e..95f5f4d2 100644 --- a/internal/tailer/tail_test.go +++ b/internal/tailer/tail_test.go @@ -16,7 +16,23 @@ import ( "github.com/google/mtail/internal/waker" ) -func makeTestTail(t *testing.T, options ...Option) (*Tailer, chan *logline.LogLine, func(int), string, func()) { +type testTail struct { + *Tailer + + // Output lnes channel + lines chan *logline.LogLine + + // Method to wake the waker + awaken func(int) + + // Temporary dir for test + tmpDir string + + // Issue a shutdown to the test tailer. + stop func() +} + +func makeTestTail(t *testing.T, options ...Option) *testTail { t.Helper() tmpDir := testutil.TestTempDir(t) @@ -27,13 +43,15 @@ func makeTestTail(t *testing.T, options ...Option) (*Tailer, chan *logline.LogLi options = append(options, LogPatterns([]string{tmpDir}), LogstreamPollWaker(waker)) ta, err := New(ctx, &wg, lines, options...) testutil.FatalIfErr(t, err) - return ta, lines, awaken, tmpDir, func() { cancel(); wg.Wait() } + stop := func() { cancel(); wg.Wait() } + t.Cleanup(stop) + return &testTail{Tailer: ta, lines: lines, awaken: awaken, tmpDir: tmpDir, stop: stop} } func TestTail(t *testing.T) { - ta, _, _, dir, stop := makeTestTail(t) + ta := makeTestTail(t) - logfile := filepath.Join(dir, "log") + logfile := filepath.Join(ta.tmpDir, "log") f := testutil.TestOpenFile(t, logfile) defer f.Close() @@ -43,8 +61,6 @@ func TestTail(t *testing.T) { if _, ok := ta.logstreams[logfile]; !ok { t.Errorf("path not found in files map: %+#v", ta.logstreams) } - - stop() } func TestTailErrors(t *testing.T) { @@ -63,21 +79,21 @@ func TestTailErrors(t *testing.T) { } func TestHandleLogUpdate(t *testing.T) { - ta, lines, awaken, dir, stop := makeTestTail(t) + ta := makeTestTail(t) - logfile := filepath.Join(dir, "log") + logfile := filepath.Join(ta.tmpDir, "log") f := testutil.TestOpenFile(t, logfile) defer f.Close() testutil.FatalIfErr(t, ta.TailPath(logfile)) - awaken(1) + ta.awaken(1) testutil.WriteString(t, f, "a\nb\nc\nd\n") - awaken(1) + ta.awaken(1) - stop() + ta.stop() - received := testutil.LinesReceived(lines) + received := testutil.LinesReceived(ta.lines) expected := []*logline.LogLine{ {Context: context.Background(), Filename: logfile, Line: "a"}, {Context: context.Background(), Filename: logfile, Line: "b"}, @@ -91,18 +107,18 @@ func TestHandleLogUpdate(t *testing.T) { // writes to be seen, then truncates the file and writes some more. // At the end all lines written must be reported by the tailer. func TestHandleLogTruncate(t *testing.T) { - ta, lines, awaken, dir, stop := makeTestTail(t) + ta := makeTestTail(t) - logfile := filepath.Join(dir, "log") + logfile := filepath.Join(ta.tmpDir, "log") f := testutil.OpenLogFile(t, logfile) defer f.Close() testutil.FatalIfErr(t, ta.TailPath(logfile)) // Expect to wake 1 wakee, the logstream reading `logfile`. - awaken(1) + ta.awaken(1) testutil.WriteString(t, f, "a\nb\nc\n") - awaken(1) + ta.awaken(1) if err := f.Truncate(0); err != nil { t.Fatal(err) @@ -111,14 +127,14 @@ func TestHandleLogTruncate(t *testing.T) { // "File.Truncate" does not change the file offset, force a seek to start. _, err := f.Seek(0, 0) testutil.FatalIfErr(t, err) - awaken(1) + ta.awaken(1) testutil.WriteString(t, f, "d\ne\n") - awaken(1) + ta.awaken(1) - stop() + ta.stop() - received := testutil.LinesReceived(lines) + received := testutil.LinesReceived(ta.lines) expected := []*logline.LogLine{ {Context: context.Background(), Filename: logfile, Line: "a"}, {Context: context.Background(), Filename: logfile, Line: "b"}, @@ -130,27 +146,27 @@ func TestHandleLogTruncate(t *testing.T) { } func TestHandleLogUpdatePartialLine(t *testing.T) { - ta, lines, awaken, dir, stop := makeTestTail(t) + ta := makeTestTail(t) - logfile := filepath.Join(dir, "log") + logfile := filepath.Join(ta.tmpDir, "log") f := testutil.TestOpenFile(t, logfile) defer f.Close() testutil.FatalIfErr(t, ta.TailPath(logfile)) - awaken(1) // ensure we've hit an EOF before writing starts + ta.awaken(1) // ensure we've hit an EOF before writing starts testutil.WriteString(t, f, "a") - awaken(1) + ta.awaken(1) testutil.WriteString(t, f, "b") - awaken(1) + ta.awaken(1) testutil.WriteString(t, f, "\n") - awaken(1) + ta.awaken(1) - stop() + ta.stop() - received := testutil.LinesReceived(lines) + received := testutil.LinesReceived(ta.lines) expected := []*logline.LogLine{ {Context: context.Background(), Filename: logfile, Line: "ab"}, } @@ -159,10 +175,10 @@ func TestHandleLogUpdatePartialLine(t *testing.T) { func TestTailerUnreadableFile(t *testing.T) { // Test broken files are skipped. - ta, lines, awaken, dir, stop := makeTestTail(t) + ta := makeTestTail(t) - brokenfile := filepath.Join(dir, "brokenlog") - logfile := filepath.Join(dir, "log") + brokenfile := filepath.Join(ta.tmpDir, "brokenlog") + logfile := filepath.Join(ta.tmpDir, "log") testutil.FatalIfErr(t, ta.AddPattern(brokenfile)) testutil.FatalIfErr(t, ta.AddPattern(logfile)) @@ -176,11 +192,11 @@ func TestTailerUnreadableFile(t *testing.T) { glog.Info("write string") testutil.WriteString(t, f, "\n") - awaken(1) + ta.awaken(1) - stop() + ta.stop() - received := testutil.LinesReceived(lines) + received := testutil.LinesReceived(ta.lines) expected := []*logline.LogLine{ {Context: context.Background(), Filename: logfile, Line: ""}, } @@ -225,11 +241,11 @@ func TestTailerInitErrors(t *testing.T) { func TestTailExpireStaleHandles(t *testing.T) { t.Skip("need to set lastRead on logstream to inject condition") - ta, lines, awaken, dir, stop := makeTestTail(t) + ta := makeTestTail(t) - log1 := filepath.Join(dir, "log1") + log1 := filepath.Join(ta.tmpDir, "log1") f1 := testutil.TestOpenFile(t, log1) - log2 := filepath.Join(dir, "log2") + log2 := filepath.Join(ta.tmpDir, "log2") f2 := testutil.TestOpenFile(t, log2) if err := ta.TailPath(log1); err != nil { @@ -241,11 +257,11 @@ func TestTailExpireStaleHandles(t *testing.T) { testutil.WriteString(t, f1, "1\n") testutil.WriteString(t, f2, "2\n") - awaken(1) + ta.awaken(1) - stop() + ta.stop() - received := testutil.LinesReceived(lines) + received := testutil.LinesReceived(ta.lines) expected := []*logline.LogLine{ {Context: context.Background(), Filename: log1, Line: "1"}, {Context: context.Background(), Filename: log2, Line: "2"}, diff --git a/internal/tailer/tail_unix_test.go b/internal/tailer/tail_unix_test.go index c677a112..72bd790f 100644 --- a/internal/tailer/tail_unix_test.go +++ b/internal/tailer/tail_unix_test.go @@ -22,9 +22,9 @@ func TestTailerOpenRetries(t *testing.T) { // Can't force a permission denied error if run as root. testutil.SkipIfRoot(t) - ta, lines, awaken, dir, stop := makeTestTail(t) + ta := makeTestTail(t) - logfile := filepath.Join(dir, "log") + logfile := filepath.Join(ta.tmpDir, "log") if _, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0); err != nil { t.Fatal(err) } @@ -55,14 +55,14 @@ func TestTailerOpenRetries(t *testing.T) { } testutil.FatalIfErr(t, ta.PollLogPatterns()) testutil.FatalIfErr(t, ta.PollLogStreamsForCompletion()) - awaken(1) // force sync to EOF + ta.awaken(1) // force sync to EOF glog.Info("write string") testutil.WriteString(t, f, "\n") - awaken(1) + ta.awaken(1) - stop() + ta.stop() - received := testutil.LinesReceived(lines) + received := testutil.LinesReceived(ta.lines) expected := []*logline.LogLine{ {Context: context.Background(), Filename: logfile, Line: ""}, } diff --git a/internal/tailer/tail_windows_test.go b/internal/tailer/tail_windows_test.go index 13b8e461..7a2cef44 100644 --- a/internal/tailer/tail_windows_test.go +++ b/internal/tailer/tail_windows_test.go @@ -12,7 +12,7 @@ import ( ) func TestWindowsPath(t *testing.T) { - ta, _, _, _, stop := makeTestTail(t) + ta := makeTestTail(t) testutil.FatalIfErr(t, ta.AddPattern("C:\\somefile")) @@ -20,5 +20,5 @@ func TestWindowsPath(t *testing.T) { t.Errorf("path not found in files map: %+#v", ta.globPatterns) } - stop() + ta.stop() }