From e9c3c07fbbf7fab3e74b46181e3da680c2523b1f Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Tue, 28 May 2013 11:46:49 -0700 Subject: [PATCH] add (failing) tests for copytruncate detection --- tail_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/tail_test.go b/tail_test.go index 56af1e5..a4f214e 100644 --- a/tail_test.go +++ b/tail_test.go @@ -128,14 +128,60 @@ func _TestReOpen(_t *testing.T, poll bool) { // The use of polling file watcher could affect file rotation // (detected via renames), so test these explicitly. +func TestReOpenWithoutPoll(_t *testing.T) { + _TestReOpen(_t, false) +} + func TestReOpenWithPoll(_t *testing.T) { _TestReOpen(_t, true) } -func TestReOpenWithoutPoll(_t *testing.T) { - _TestReOpen(_t, false) + +func _TestReSeek(_t *testing.T, poll bool) { + var name string + if poll { + name = "reseek-polling" + }else { + name = "reseek-inotify" + } + t := NewTailTest(name, _t) + t.CreateFile("test.txt", "hello\nworld\n") + tail := t.StartTail( + "test.txt", + Config{Follow: true, ReOpen: true, Poll: poll, Location: -1}) + + go t.VerifyTailOutput(tail, []string{"hello", "world", "h311o", "w0r1d", "endofworld"}) + + // truncate now + <-time.After(100 * time.Millisecond) + t.TruncateFile("test.txt", "h311o\nw0r1d\nendofworld\n") + // XXX: is this required for this test function? + if poll { + // Give polling a chance to read the just-written lines (more; + // data), before we recreate the file again below. + <-time.After(POLL_DURATION) + } + + // Delete after a reasonable delay, to give tail sufficient time + // to read all lines. + <-time.After(100 * time.Millisecond) + t.RemoveFile("test.txt") + + tail.Stop() } +// The use of polling file watcher could affect file rotation +// (detected via renames), so test these explicitly. + +func TestReSeekWithoutPoll(_t *testing.T) { + _TestReSeek(_t, false) +} + +func TestReSeekWithPoll(_t *testing.T) { + _TestReSeek(_t, true) +} + + // Test library type TailTest struct { @@ -188,6 +234,18 @@ func (t TailTest) AppendFile(name string, contents string) { } } +func (t TailTest) TruncateFile(name string, contents string) { + f, err := os.OpenFile(t.path+"/"+name, os.O_WRONLY, 0600) + if err != nil { + t.Fatal(err) + } + defer f.Close() + _, err = f.WriteString(contents) + if err != nil { + t.Fatal(err) + } +} + func (t TailTest) StartTail(name string, config Config) *Tail { tail, err := TailFile(t.path+"/"+name, config) if err != nil { @@ -211,7 +269,8 @@ func (t TailTest) VerifyTailOutput(tail *Tail, lines []string) { t.Fatalf("tail.Lines returned nil; not possible") } if tailedLine.Text != line { - t.Fatalf("mismatch; %s != %s", tailedLine.Text, line) + t.Fatalf("mismatch; %s (actual) != %s (expected)", + tailedLine.Text, line) } } line, ok := <-tail.Lines