From 4e53f618e23e3e1436f184a0ec0a1bd9f44dfeea Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 13 Oct 2012 11:20:04 -0700 Subject: [PATCH] first test case for tail tests "-n -1" need to abstract operations for multiple workflow --- Makefile | 2 +- tail.go | 2 +- tail_test.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 8c6bd41..f5cc885 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ default: test test: *.go - GOPATH=~/as/logyard go test + GOPATH=~/as/logyard go test -v diff --git a/tail.go b/tail.go index 66279c1..6ca0a66 100644 --- a/tail.go +++ b/tail.go @@ -193,7 +193,7 @@ func (tail *Tail) tailFileSync() { } log.Printf("Successfully reopened %s", tail.Filename) tail.reader = bufio.NewReaderSize(tail.file, tail.MaxLineSize) - changes = nil + changes = nil // XXX: how to kill changes' goroutine? continue } else { log.Printf("Finishing because file has been moved/deleted: %s", tail.Filename) diff --git a/tail_test.go b/tail_test.go index ba7ced1..b69af5a 100644 --- a/tail_test.go +++ b/tail_test.go @@ -2,20 +2,88 @@ package tail import ( "testing" + "time" + "os" + "io/ioutil" + _ "fmt" ) +func init() { + err := os.RemoveAll(".test") + if err != nil { + panic(err) + } +} + // Test Config.MustExist func TestMissingFile(t *testing.T) { - _, err := TailFile("/no/such/file", Config{Follow: true, MustExist: true}) + tail, err := TailFile("/no/such/file", Config{Follow: true, MustExist: true}) if err == nil { t.Error("MustExist:true is violated") + tail.Stop() } - _, err = TailFile("/no/such/file", Config{Follow: true, MustExist: false}) + tail, err = TailFile("/no/such/file", Config{Follow: true, MustExist: false}) if err != nil { t.Error("MustExist:false is violated") } + tail.Stop() _, err = TailFile("README.md", Config{Follow: true, MustExist: true}) if err != nil { t.Error("MustExist:true on an existing file is violated") } + tail.Stop() +} + +func TestLocationMinusOne(t *testing.T) { + fix := NewFixture("simple") + err := ioutil.WriteFile(fix.path + "/test.txt", []byte("hello\nworld\n"), 0777) + if err != nil { + t.Error(err) + } + tail, err := TailFile(fix.path + "/test.txt", Config{Follow: true, Location: -1}) + if err != nil { + t.Error(err) + } + go CompareTailWithLines(t, tail, []string{"hello", "world"}) + + // delete after 1 second, to give tail sufficient time to read all lines. + <-time.After(1 * time.Second) + err = os.Remove(fix.path + "/test.txt") + if err != nil { + t.Error(err) + } + tail.Stop() +} + +type Fixture struct { + Name string + path string +} + +func NewFixture(name string) Fixture { + fix := Fixture{name, ".test/" + name} + os.MkdirAll(fix.path, os.ModeTemporary | 0700) + return fix +} + +func CompareTailWithLines(t *testing.T, tail *Tail, lines []string) { + for _, line := range lines { + tailedLine, ok := <-tail.Lines + if !ok { + t.Error("insufficient lines from tail") + return + } + if tailedLine == nil { + t.Errorf("tail.Lines returned nil; not possible") + return + } + if tailedLine.Text != line { + t.Errorf("mismatch; %s != %s", tailedLine.Text, line) + return + } + } + line, ok := <-tail.Lines + if ok { + t.Errorf("more content from tail: %s", line.Text) + } }