diff --git a/http_client.go b/http_client.go index 3ae3745..1ed8f88 100644 --- a/http_client.go +++ b/http_client.go @@ -8,7 +8,7 @@ import ( "log" "net" "net/url" - _ "runtime/debug" + "runtime/debug" "strconv" "strings" "sync" diff --git a/output_file.go b/output_file.go index 209d563..8b19c29 100644 --- a/output_file.go +++ b/output_file.go @@ -235,7 +235,7 @@ func (o *FileOutput) flush() { o.writer.(*bufio.Writer).Flush() } - if stat, err := o.file.Stat(); err != nil { + if stat, err := o.file.Stat(); err == nil { o.chunkSize = int(stat.Size()) } } diff --git a/output_file_test.go b/output_file_test.go index 7fbc579..9a81102 100644 --- a/output_file_test.go +++ b/output_file_test.go @@ -312,3 +312,37 @@ func TestFileOutputSort(t *testing.T) { t.Error("Should properly sort file names using indexes", files, expected) } } + +func TestFileOutputAppendSizeLimitOverflow(t *testing.T) { + rnd := rand.Int63() + name := fmt.Sprintf("/tmp/%d", rnd) + + message := []byte("1 1 1\r\ntest") + + messageSize := len(message) + len(payloadSeparator) + + output := NewFileOutput(name, &FileOutputConfig{append: false, flushInterval: time.Minute, sizeLimit: unitSizeVar(2 * messageSize) }) + + output.Write([]byte("1 1 1\r\ntest")) + name1 := output.file.Name() + + output.Write([]byte("1 1 1\r\ntest")) + name2 := output.file.Name() + + output.flush() + output.updateName() + + output.Write([]byte("1 1 1\r\ntest")) + name3 := output.file.Name() + + if name2 != name1 || name1 != fmt.Sprintf("/tmp/%d_0", rnd) { + t.Error("Fast changes should happen in same file:", name1, name2, name3) + } + + if name3 == name1 || name3 != fmt.Sprintf("/tmp/%d_1", rnd) { + t.Error("File name should change:", name1, name2, name3) + } + + os.Remove(name1) + os.Remove(name3) +} \ No newline at end of file