test: fixture workflow to make test functions concise

This commit is contained in:
Sridhar Ratnakumar 2012-10-13 11:44:47 -07:00
parent 4e53f618e2
commit df3f97310f
4 changed files with 51 additions and 33 deletions

View File

@ -2,3 +2,6 @@ default: test
test: *.go test: *.go
GOPATH=~/as/logyard go test -v GOPATH=~/as/logyard go test -v
fmt:
go fmt .

View File

@ -1,8 +1,8 @@
package tail package tail
import ( import (
"fmt"
"bufio" "bufio"
"fmt"
"io" "io"
"launchpad.net/tomb" "launchpad.net/tomb"
"log" "log"

View File

@ -1,11 +1,11 @@
package tail package tail
import ( import (
_ "fmt"
"io/ioutil"
"os"
"testing" "testing"
"time" "time"
"os"
"io/ioutil"
_ "fmt"
) )
func init() { func init() {
@ -35,55 +35,70 @@ func TestMissingFile(t *testing.T) {
} }
func TestLocationMinusOne(t *testing.T) { func TestLocationMinusOne(t *testing.T) {
fix := NewFixture("simple") fix := NewFixture("simple", t)
err := ioutil.WriteFile(fix.path + "/test.txt", []byte("hello\nworld\n"), 0777) fix.CreateFile("test.txt", "hello\nworld\n")
if err != nil { tail := fix.StartTail("test.txt", Config{Follow: true, Location: -1})
t.Error(err) go fix.VerifyTail(tail, []string{"hello", "world"})
}
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. // Delete after a reasonable delay, to give tail sufficient time
<-time.After(1 * time.Second) // to read all lines.
err = os.Remove(fix.path + "/test.txt") <-time.After(250 * time.Millisecond)
if err != nil { fix.RemoveFile("test.txt")
t.Error(err)
}
tail.Stop() tail.Stop()
} }
type Fixture struct { type Fixture struct {
Name string Name string
path string path string
t *testing.T
} }
func NewFixture(name string) Fixture { func NewFixture(name string, t *testing.T) Fixture {
fix := Fixture{name, ".test/" + name} fix := Fixture{name, ".test/" + name, t}
os.MkdirAll(fix.path, os.ModeTemporary | 0700) err := os.MkdirAll(fix.path, os.ModeTemporary|0700)
if err != nil {
t.Fatal(err)
}
return fix return fix
} }
func CompareTailWithLines(t *testing.T, tail *Tail, lines []string) { func (fix Fixture) CreateFile(name string, contents string) {
err := ioutil.WriteFile(fix.path+"/"+name, []byte(contents), 0777)
if err != nil {
fix.t.Fatal(err)
}
}
func (fix Fixture) RemoveFile(name string) {
err := os.Remove(fix.path + "/" + name)
if err != nil {
fix.t.Fatal(err)
}
}
func (fix Fixture) StartTail(name string, config Config) *Tail {
tail, err := TailFile(fix.path+"/"+name, config)
if err != nil {
fix.t.Fatal(err)
}
return tail
}
func (fix Fixture) VerifyTail(tail *Tail, lines []string) {
for _, line := range lines { for _, line := range lines {
tailedLine, ok := <-tail.Lines tailedLine, ok := <-tail.Lines
if !ok { if !ok {
t.Error("insufficient lines from tail") fix.t.Fatal("insufficient lines from tail")
return
} }
if tailedLine == nil { if tailedLine == nil {
t.Errorf("tail.Lines returned nil; not possible") fix.t.Fatalf("tail.Lines returned nil; not possible")
return
} }
if tailedLine.Text != line { if tailedLine.Text != line {
t.Errorf("mismatch; %s != %s", tailedLine.Text, line) fix.t.Fatalf("mismatch; %s != %s", tailedLine.Text, line)
return
} }
} }
line, ok := <-tail.Lines line, ok := <-tail.Lines
if ok { if ok {
t.Errorf("more content from tail: %s", line.Text) fix.t.Fatalf("more content from tail: %s", line.Text)
} }
} }

View File

@ -121,11 +121,11 @@ func (fw *PollingFileWatcher) ChangeEvents() chan bool {
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
// below goroutine (every2Seconds) will catch up eventually and stop us. // below goroutine (every2Seconds) will catch up eventually and stop us.
continue continue
} }
panic(err) panic(err)
} }
modTime := fi.ModTime() modTime := fi.ModTime()
if modTime != prevModTime { if modTime != prevModTime {
prevModTime = modTime prevModTime = modTime