Fixed the test for writing only N amount of bytes as much as capacity

This commit is contained in:
Diptanu Choudhury 2016-01-14 18:30:53 -08:00
parent b474810134
commit 97dca83866
2 changed files with 46 additions and 15 deletions

View file

@ -18,13 +18,13 @@ type LogRotator struct {
logFileIdx int
}
func NewLogRotor(path string, fileName string, maxFiles int, fileSize int64) (*LogRotator, error) {
func NewLogRotator(path string, fileName string, maxFiles int, fileSize int64) (*LogRotator, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
logFileIdx := 1
logFileIdx := 0
for _, f := range files {
if strings.HasPrefix(f.Name(), fileName) {
fileIdx := strings.TrimPrefix(f.Name(), fmt.Sprintf("%s.", fileName))
@ -48,22 +48,39 @@ func NewLogRotor(path string, fileName string, maxFiles int, fileSize int64) (*L
}
func (l *LogRotator) Start(r io.Reader) error {
buf := make([]byte, 32 * 1024)
for {
logFileName := filepath.Join(l.path, fmt.Sprintf("%s.%d", l.fileName, l.logFileIdx))
f, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 066)
f, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0066)
if err != nil {
return err
}
remainingSize := l.fileSize
if finfo, err := os.Stat(logFileName); err == nil {
remainingSize = l.fileSize - finfo.Size()
remainingSize -= finfo.Size()
}
if remainingSize < 1 {
l.logFileIdx = l.logFileIdx + 1
f.Close()
continue
}
if _, err := io.Copy(f, io.LimitReader(r, remainingSize)); err != nil {
return err
for {
nr, err := io.LimitReader(r, remainingSize).Read(buf)
if err != nil {
f.Close()
return err
}
nw, err := f.Write(buf[:nr])
if err != nil {
f.Close()
return err
}
if nr != nw {
f.Close()
return fmt.Errorf("Failed to write data R: %d W: %d", nr, nw)
}
remainingSize -= int64(nr)
}
l.logFileIdx = l.logFileIdx + 1
}

View file

@ -1,7 +1,7 @@
package driver
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
@ -10,7 +10,7 @@ import (
func TestLogRotator_IncorrectPath(t *testing.T) {
incorrectPath := "/foo"
if _, err := NewLogRotor(incorrectPath, "redis.stdout", 10, 10); err == nil {
if _, err := NewLogRotator(incorrectPath, "redis.stdout", 10, 10); err == nil {
t.Fatal("expected err")
}
}
@ -32,7 +32,7 @@ func TestLogRotator_FindCorrectIndex(t *testing.T) {
f.Close()
}
r, err := NewLogRotor(path, "redis.stdout", 10, 10)
r, err := NewLogRotator(path, "redis.stdout", 10, 10)
if err != nil {
t.Fatalf("test setup err: %v", err)
}
@ -43,21 +43,35 @@ func TestLogRotator_FindCorrectIndex(t *testing.T) {
func TestLogRotator_AppendToCurrentFile(t *testing.T) {
path := "/tmp/tmplogrator"
defer os.RemoveAll(path)
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")
fname := filepath.Join(path, "redis.stdout.0")
if f, err := os.Create(fname); err == nil {
f.WriteString("abcde")
f.Close()
}
r, err := NewLogRotor(path, "redis.stdout", 10, 10)
l, err := NewLogRotator(path, "redis.stdout", 10, 6)
if err != nil {
t.Fatalf("test setup err: %v", err)
}
var buf bytes.Buffer
buf.WriteString("hello")
r.Start(&buf)
r, w := io.Pipe()
go func() {
w.Write([]byte("fg"))
w.Close()
}()
err = l.Start(r)
if err != nil && err != io.EOF{
t.Fatal(err)
}
finfo, err := os.Stat(fname)
if err != nil {
t.Fatal(err)
}
if finfo.Size() != 6 {
t.Fatalf("Expected size of file: %v, actual: %v", 6, finfo.Size())
}
}