Added a test for file rotation

This commit is contained in:
Diptanu Choudhury 2016-01-14 11:41:31 -08:00
parent 9749ee301d
commit b474810134
2 changed files with 24 additions and 1 deletions

View File

@ -24,7 +24,7 @@ func NewLogRotor(path string, fileName string, maxFiles int, fileSize int64) (*L
return nil, err
}
logFileIdx := 0
logFileIdx := 1
for _, f := range files {
if strings.HasPrefix(f.Name(), fileName) {
fileIdx := strings.TrimPrefix(f.Name(), fmt.Sprintf("%s.", fileName))

View File

@ -1,6 +1,7 @@
package driver
import (
"bytes"
"os"
"path/filepath"
"testing"
@ -19,6 +20,7 @@ func TestLogRotator_FindCorrectIndex(t *testing.T) {
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil {
t.Fatalf("test setup err: %v", err)
}
defer os.RemoveAll(path)
fname := filepath.Join(path, "redis.stdout.1")
if f, err := os.Create(fname); err == nil {
@ -38,3 +40,24 @@ func TestLogRotator_FindCorrectIndex(t *testing.T) {
t.Fatalf("Expected log file idx: %v, actual: %v", 2, r.logFileIdx)
}
}
func TestLogRotator_AppendToCurrentFile(t *testing.T) {
path := "/tmp/tmplogrator"
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil {
t.Fatalf("test setup err: %v", err)
}
defer os.RemoveAll(path)
fname := filepath.Join(path, "redis.stdout.1")
if f, err := os.Create(fname); err == nil {
f.Close()
}
r, err := NewLogRotor(path, "redis.stdout", 10, 10)
if err != nil {
t.Fatalf("test setup err: %v", err)
}
var buf bytes.Buffer
buf.WriteString("hello")
r.Start(&buf)
}