Skip to content

Commit

Permalink
refactor: Return a single test object from makeTestTail.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaqx0r committed Apr 22, 2024
1 parent a01b512 commit d0e422b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
96 changes: 56 additions & 40 deletions internal/tailer/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()

Expand All @@ -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) {
Expand All @@ -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"},
Expand All @@ -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)
Expand All @@ -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"},
Expand All @@ -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"},
}
Expand All @@ -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))

Expand All @@ -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: ""},
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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"},
Expand Down
12 changes: 6 additions & 6 deletions internal/tailer/tail_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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: ""},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/tailer/tail_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

func TestWindowsPath(t *testing.T) {
ta, _, _, _, stop := makeTestTail(t)
ta := makeTestTail(t)

testutil.FatalIfErr(t, ta.AddPattern("C:\\somefile"))

if _, ok := ta.globPatterns["C:\\somefile"]; !ok {
t.Errorf("path not found in files map: %+#v", ta.globPatterns)
}

stop()
ta.stop()
}

0 comments on commit d0e422b

Please sign in to comment.