add tail.Tell's test

This commit is contained in:
QLeelulu 2013-08-24 15:43:25 +08:00
parent 40bd571489
commit 1a8af20665
2 changed files with 39 additions and 1 deletions

View File

@ -93,7 +93,12 @@ func TailFile(filename string, config Config) (*Tail, error) {
// Return the file's current position, like stdio's ftell(). // Return the file's current position, like stdio's ftell().
// But this value is not very accurate. // But this value is not very accurate.
func (tail *Tail) Tell() (offser int64, err error) { // it may readed one line in the chan(tail.Lines),
// so it may lost one line.
func (tail *Tail) Tell() (offset int64, err error) {
if tail.file == nil {
return
}
offset, err = tail.file.Seek(0, os.SEEK_CUR) offset, err = tail.file.Seek(0, os.SEEK_CUR)
if err == nil { if err == nil {
offset -= int64(tail.reader.Buffered()) offset -= int64(tail.reader.Buffered())

View File

@ -225,6 +225,39 @@ func TestRateLimiting(_t *testing.T) {
tail.Stop() tail.Stop()
} }
func TestTell(_t *testing.T) {
t := NewTailTest("tell-position", _t)
t.CreateFile("test.txt", "hello\nworld\nagain\nmore\n")
config := Config{
Follow: false,
Location: &SeekInfo{0, os.SEEK_SET}}
tail := t.StartTail("test.txt", config)
// read noe line
<-tail.Lines
offset, err := tail.Tell()
if err != nil {
t.Errorf("Tell return error: %s", err.Error())
}
tail.Done()
// tail.close()
config = Config{
Follow: false,
Location: &SeekInfo{offset, os.SEEK_SET}}
tail = t.StartTail("test.txt", config)
for l := range tail.Lines {
// it may readed one line in the chan(tail.Lines),
// so it may lost one line.
if l.Text != "world" && l.Text != "again" {
t.Fatalf("mismatch; expected world or again, but got %s",
l.Text)
}
break
}
t.RemoveFile("test.txt")
tail.Done()
}
// Test library // Test library
type TailTest struct { type TailTest struct {