Merge pull request #24 from masahide/add-test-over4096byte-line
Support lines of length more than 4096 bytes
This commit is contained in:
commit
9f7054d896
27
tail.go
27
tail.go
|
@ -170,8 +170,17 @@ func (tail *Tail) reopen() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tail *Tail) readLine() ([]byte, error) {
|
func (tail *Tail) readLine() ([]byte, error) {
|
||||||
line, _, err := tail.reader.ReadLine()
|
line, isPrefix, err := tail.reader.ReadLine()
|
||||||
return line, err
|
if !isPrefix || tail.MaxLineSize > 0 {
|
||||||
|
return line, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := append([]byte(nil), line...)
|
||||||
|
for isPrefix && err == nil {
|
||||||
|
line, isPrefix, err = tail.reader.ReadLine()
|
||||||
|
buf = append(buf, line...)
|
||||||
|
}
|
||||||
|
return buf, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tail *Tail) tailFileSync() {
|
func (tail *Tail) tailFileSync() {
|
||||||
|
@ -199,7 +208,7 @@ func (tail *Tail) tailFileSync() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tail.reader = bufio.NewReader(tail.file)
|
tail.reader = tail.newReader()
|
||||||
|
|
||||||
// Read line by line.
|
// Read line by line.
|
||||||
for {
|
for {
|
||||||
|
@ -280,7 +289,7 @@ func (tail *Tail) waitForChanges() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tail.Logger.Printf("Successfully reopened %s", tail.Filename)
|
tail.Logger.Printf("Successfully reopened %s", tail.Filename)
|
||||||
tail.reader = bufio.NewReader(tail.file)
|
tail.reader = tail.newReader()
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
tail.Logger.Printf("Stopping tail as file no longer exists: %s", tail.Filename)
|
tail.Logger.Printf("Stopping tail as file no longer exists: %s", tail.Filename)
|
||||||
|
@ -293,7 +302,7 @@ func (tail *Tail) waitForChanges() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tail.Logger.Printf("Successfully reopened truncated %s", tail.Filename)
|
tail.Logger.Printf("Successfully reopened truncated %s", tail.Filename)
|
||||||
tail.reader = bufio.NewReader(tail.file)
|
tail.reader = tail.newReader()
|
||||||
return nil
|
return nil
|
||||||
case <-tail.Dying():
|
case <-tail.Dying():
|
||||||
return ErrStop
|
return ErrStop
|
||||||
|
@ -301,6 +310,14 @@ func (tail *Tail) waitForChanges() error {
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tail *Tail) newReader() *bufio.Reader {
|
||||||
|
if tail.MaxLineSize > 0 {
|
||||||
|
return bufio.NewReaderSize(tail.file, tail.MaxLineSize+2)
|
||||||
|
} else {
|
||||||
|
return bufio.NewReader(tail.file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// sendLine sends the line(s) to Lines channel, splitting longer lines
|
// sendLine sends the line(s) to Lines channel, splitting longer lines
|
||||||
// if necessary. Return false if rate limit is reached.
|
// if necessary. Return false if rate limit is reached.
|
||||||
func (tail *Tail) sendLine(line []byte) bool {
|
func (tail *Tail) sendLine(line []byte) bool {
|
||||||
|
|
30
tail_test.go
30
tail_test.go
|
@ -10,6 +10,7 @@ import (
|
||||||
_ "fmt"
|
_ "fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -66,6 +67,35 @@ func TestMaxLineSize(_t *testing.T) {
|
||||||
Cleanup()
|
Cleanup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOver4096ByteLine(_t *testing.T) {
|
||||||
|
t := NewTailTest("Over4096ByteLine", _t)
|
||||||
|
testString := strings.Repeat("a", 4097)
|
||||||
|
t.CreateFile("test.txt", "test\n"+testString+"\nhello\nworld\n")
|
||||||
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: nil})
|
||||||
|
go t.VerifyTailOutput(tail, []string{"test", testString, "hello", "world"})
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
Cleanup()
|
||||||
|
}
|
||||||
|
func TestOver4096ByteLineWithSetMaxLineSize(_t *testing.T) {
|
||||||
|
t := NewTailTest("Over4096ByteLineMaxLineSize", _t)
|
||||||
|
testString := strings.Repeat("a", 4097)
|
||||||
|
t.CreateFile("test.txt", "test\r\n"+testString+"\r\nhello\r\nworld\r\n")
|
||||||
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: nil, MaxLineSize: 4097})
|
||||||
|
go t.VerifyTailOutput(tail, []string{"test", testString, "hello", "world"})
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
Cleanup()
|
||||||
|
}
|
||||||
|
|
||||||
func TestLocationFull(_t *testing.T) {
|
func TestLocationFull(_t *testing.T) {
|
||||||
t := NewTailTest("location-full", _t)
|
t := NewTailTest("location-full", _t)
|
||||||
t.CreateFile("test.txt", "hello\nworld\n")
|
t.CreateFile("test.txt", "hello\nworld\n")
|
||||||
|
|
Loading…
Reference in New Issue