2016-10-26 16:23:19 -07:00
|
|
|
package autofile
|
|
|
|
|
|
|
|
import (
|
2016-10-26 21:50:07 -07:00
|
|
|
"errors"
|
2017-10-30 10:48:51 -07:00
|
|
|
"fmt"
|
2016-10-26 21:50:07 -07:00
|
|
|
"io"
|
2016-10-30 02:40:39 -07:00
|
|
|
"io/ioutil"
|
2016-10-26 21:50:07 -07:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-10-26 16:23:19 -07:00
|
|
|
"testing"
|
|
|
|
|
2017-10-30 10:48:51 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2016-10-26 16:23:19 -07:00
|
|
|
)
|
|
|
|
|
2016-10-26 21:50:07 -07:00
|
|
|
// NOTE: Returned group has ticker stopped
|
|
|
|
func createTestGroup(t *testing.T, headSizeLimit int64) *Group {
|
2017-10-30 10:48:51 -07:00
|
|
|
testID := cmn.RandStr(12)
|
2016-10-26 21:50:07 -07:00
|
|
|
testDir := "_test_" + testID
|
2017-10-30 10:48:51 -07:00
|
|
|
err := cmn.EnsureDir(testDir, 0700)
|
|
|
|
require.NoError(t, err, "Error creating dir")
|
2016-10-26 21:50:07 -07:00
|
|
|
headPath := testDir + "/myfile"
|
2016-11-21 19:09:14 -08:00
|
|
|
g, err := OpenGroup(headPath)
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "Error opening Group")
|
2016-10-26 21:50:07 -07:00
|
|
|
g.SetHeadSizeLimit(headSizeLimit)
|
|
|
|
g.stopTicker()
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NotEqual(t, nil, g, "Failed to create Group")
|
2016-10-26 16:23:19 -07:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2016-10-26 21:50:07 -07:00
|
|
|
func destroyTestGroup(t *testing.T, g *Group) {
|
2018-06-06 00:52:59 -07:00
|
|
|
g.Close()
|
2016-10-26 21:50:07 -07:00
|
|
|
err := os.RemoveAll(g.Dir)
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "Error removing test Group directory")
|
2016-10-26 21:50:07 -07:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:23:19 -07:00
|
|
|
func assertGroupInfo(t *testing.T, gInfo GroupInfo, minIndex, maxIndex int, totalSize, headSize int64) {
|
2017-10-30 11:01:18 -07:00
|
|
|
assert.Equal(t, minIndex, gInfo.MinIndex)
|
|
|
|
assert.Equal(t, maxIndex, gInfo.MaxIndex)
|
|
|
|
assert.Equal(t, totalSize, gInfo.TotalSize)
|
|
|
|
assert.Equal(t, headSize, gInfo.HeadSize)
|
2016-10-26 16:23:19 -07:00
|
|
|
}
|
|
|
|
|
2016-10-26 21:50:07 -07:00
|
|
|
func TestCheckHeadSizeLimit(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 1000*1000)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// At first, there are no files.
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 0, 0)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Write 1000 bytes 999 times.
|
|
|
|
for i := 0; i < 999; i++ {
|
2017-10-30 10:48:51 -07:00
|
|
|
err := g.WriteLine(cmn.RandStr(999))
|
|
|
|
require.NoError(t, err, "Error appending to head")
|
2016-10-26 16:23:19 -07:00
|
|
|
}
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 999000, 999000)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Even calling checkHeadSizeLimit manually won't rotate it.
|
|
|
|
g.checkHeadSizeLimit()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 999000, 999000)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Write 1000 more bytes.
|
2017-10-30 10:48:51 -07:00
|
|
|
err := g.WriteLine(cmn.RandStr(999))
|
|
|
|
require.NoError(t, err, "Error appending to head")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Calling checkHeadSizeLimit this time rolls it.
|
|
|
|
g.checkHeadSizeLimit()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 1000000, 0)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Write 1000 more bytes.
|
2017-10-30 10:48:51 -07:00
|
|
|
err = g.WriteLine(cmn.RandStr(999))
|
|
|
|
require.NoError(t, err, "Error appending to head")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Calling checkHeadSizeLimit does nothing.
|
|
|
|
g.checkHeadSizeLimit()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 1001000, 1000)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Write 1000 bytes 999 times.
|
|
|
|
for i := 0; i < 999; i++ {
|
2017-10-30 10:48:51 -07:00
|
|
|
err = g.WriteLine(cmn.RandStr(999))
|
|
|
|
require.NoError(t, err, "Error appending to head")
|
2016-10-26 16:23:19 -07:00
|
|
|
}
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 2000000, 1000000)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Calling checkHeadSizeLimit rolls it again.
|
|
|
|
g.checkHeadSizeLimit()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2000000, 0)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Write 1000 more bytes.
|
2017-10-30 10:48:51 -07:00
|
|
|
_, err = g.Head.Write([]byte(cmn.RandStr(999) + "\n"))
|
|
|
|
require.NoError(t, err, "Error appending to head")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2001000, 1000)
|
2016-10-26 16:23:19 -07:00
|
|
|
|
|
|
|
// Calling checkHeadSizeLimit does nothing.
|
|
|
|
g.checkHeadSizeLimit()
|
2016-10-26 21:50:07 -07:00
|
|
|
assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2001000, 1000)
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearch(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 10*1000)
|
|
|
|
|
|
|
|
// Create some files in the group that have several INFO lines in them.
|
|
|
|
// Try to put the INFO lines in various spots.
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
// The random junk at the end ensures that this INFO linen
|
|
|
|
// is equally likely to show up at the end.
|
2017-10-30 10:48:51 -07:00
|
|
|
_, err := g.Head.Write([]byte(fmt.Sprintf("INFO %v %v\n", i, cmn.RandStr(123))))
|
|
|
|
require.NoError(t, err, "Failed to write to head")
|
2016-10-26 21:50:07 -07:00
|
|
|
g.checkHeadSizeLimit()
|
|
|
|
for j := 0; j < 10; j++ {
|
2017-10-30 10:48:51 -07:00
|
|
|
_, err1 := g.Head.Write([]byte(cmn.RandStr(123) + "\n"))
|
|
|
|
require.NoError(t, err1, "Failed to write to head")
|
2016-10-26 21:50:07 -07:00
|
|
|
g.checkHeadSizeLimit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a search func that searches for line
|
|
|
|
makeSearchFunc := func(target int) SearchFunc {
|
|
|
|
return func(line string) (int, error) {
|
|
|
|
parts := strings.Split(line, " ")
|
|
|
|
if len(parts) != 3 {
|
|
|
|
return -1, errors.New("Line did not have 3 parts")
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return -1, errors.New("Failed to parse INFO: " + err.Error())
|
|
|
|
}
|
|
|
|
if target < i {
|
|
|
|
return 1, nil
|
|
|
|
} else if target == i {
|
|
|
|
return 0, nil
|
|
|
|
} else {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now search for each number
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
t.Log("Testing for i", i)
|
|
|
|
gr, match, err := g.Search("INFO", makeSearchFunc(i))
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "Failed to search for line")
|
|
|
|
assert.True(t, match, "Expected Search to return exact match")
|
2016-10-26 21:50:07 -07:00
|
|
|
line, err := gr.ReadLine()
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "Failed to read line after search")
|
|
|
|
if !strings.HasPrefix(line, fmt.Sprintf("INFO %v ", i)) {
|
2016-10-26 21:50:07 -07:00
|
|
|
t.Fatal("Failed to get correct line")
|
|
|
|
}
|
|
|
|
// Make sure we can continue to read from there.
|
|
|
|
cur := i + 1
|
|
|
|
for {
|
|
|
|
line, err := gr.ReadLine()
|
|
|
|
if err == io.EOF {
|
|
|
|
if cur == 99+1 {
|
|
|
|
// OK!
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
t.Fatal("Got EOF after the wrong INFO #")
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatal("Error reading line", err)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(line, "INFO ") {
|
|
|
|
continue
|
|
|
|
}
|
2017-10-30 10:48:51 -07:00
|
|
|
if !strings.HasPrefix(line, fmt.Sprintf("INFO %v ", cur)) {
|
2016-10-26 21:50:07 -07:00
|
|
|
t.Fatalf("Unexpected INFO #. Expected %v got:\n%v", cur, line)
|
|
|
|
}
|
2018-04-03 03:23:28 -07:00
|
|
|
cur++
|
2016-10-26 21:50:07 -07:00
|
|
|
}
|
|
|
|
gr.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now search for something that is too small.
|
|
|
|
// We should get the first available line.
|
|
|
|
{
|
|
|
|
gr, match, err := g.Search("INFO", makeSearchFunc(-999))
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "Failed to search for line")
|
|
|
|
assert.False(t, match, "Expected Search to not return exact match")
|
2016-10-26 21:50:07 -07:00
|
|
|
line, err := gr.ReadLine()
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "Failed to read line after search")
|
2016-10-26 21:50:07 -07:00
|
|
|
if !strings.HasPrefix(line, "INFO 0 ") {
|
|
|
|
t.Error("Failed to fetch correct line, which is the earliest INFO")
|
|
|
|
}
|
2016-10-26 22:11:43 -07:00
|
|
|
err = gr.Close()
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "Failed to close GroupReader")
|
2016-10-26 21:50:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now search for something that is too large.
|
|
|
|
// We should get an EOF error.
|
|
|
|
{
|
|
|
|
gr, _, err := g.Search("INFO", makeSearchFunc(999))
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.Equal(t, io.EOF, err)
|
|
|
|
assert.Nil(t, gr)
|
2016-10-26 21:50:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
2016-10-26 16:23:19 -07:00
|
|
|
}
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
func TestRotateFile(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
g.WriteLine("Line 1")
|
|
|
|
g.WriteLine("Line 2")
|
|
|
|
g.WriteLine("Line 3")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
g.RotateFile()
|
|
|
|
g.WriteLine("Line 4")
|
|
|
|
g.WriteLine("Line 5")
|
|
|
|
g.WriteLine("Line 6")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
// Read g.Head.Path+"000"
|
|
|
|
body1, err := ioutil.ReadFile(g.Head.Path + ".000")
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.NoError(t, err, "Failed to read first rolled file")
|
2016-10-30 02:40:39 -07:00
|
|
|
if string(body1) != "Line 1\nLine 2\nLine 3\n" {
|
|
|
|
t.Errorf("Got unexpected contents: [%v]", string(body1))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read g.Head.Path
|
|
|
|
body2, err := ioutil.ReadFile(g.Head.Path)
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.NoError(t, err, "Failed to read first rolled file")
|
2016-10-30 02:40:39 -07:00
|
|
|
if string(body2) != "Line 4\nLine 5\nLine 6\n" {
|
|
|
|
t.Errorf("Got unexpected contents: [%v]", string(body2))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindLast1(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
|
|
|
g.WriteLine("Line 1")
|
|
|
|
g.WriteLine("Line 2")
|
|
|
|
g.WriteLine("# a")
|
|
|
|
g.WriteLine("Line 3")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
g.RotateFile()
|
|
|
|
g.WriteLine("Line 4")
|
|
|
|
g.WriteLine("Line 5")
|
|
|
|
g.WriteLine("Line 6")
|
|
|
|
g.WriteLine("# b")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
match, found, err := g.FindLast("#")
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "# b", match)
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindLast2(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
|
|
|
g.WriteLine("Line 1")
|
|
|
|
g.WriteLine("Line 2")
|
|
|
|
g.WriteLine("Line 3")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
g.RotateFile()
|
|
|
|
g.WriteLine("# a")
|
|
|
|
g.WriteLine("Line 4")
|
|
|
|
g.WriteLine("Line 5")
|
|
|
|
g.WriteLine("# b")
|
|
|
|
g.WriteLine("Line 6")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
match, found, err := g.FindLast("#")
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "# b", match)
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindLast3(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
|
|
|
g.WriteLine("Line 1")
|
|
|
|
g.WriteLine("# a")
|
|
|
|
g.WriteLine("Line 2")
|
|
|
|
g.WriteLine("# b")
|
|
|
|
g.WriteLine("Line 3")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
g.RotateFile()
|
|
|
|
g.WriteLine("Line 4")
|
|
|
|
g.WriteLine("Line 5")
|
|
|
|
g.WriteLine("Line 6")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
match, found, err := g.FindLast("#")
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "# b", match)
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindLast4(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
|
|
|
g.WriteLine("Line 1")
|
|
|
|
g.WriteLine("Line 2")
|
|
|
|
g.WriteLine("Line 3")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
g.RotateFile()
|
|
|
|
g.WriteLine("Line 4")
|
|
|
|
g.WriteLine("Line 5")
|
|
|
|
g.WriteLine("Line 6")
|
2016-11-21 19:09:14 -08:00
|
|
|
g.Flush()
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
match, found, err := g.FindLast("#")
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, found)
|
|
|
|
assert.Empty(t, match)
|
2016-10-30 02:40:39 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
2017-10-24 12:20:17 -07:00
|
|
|
|
|
|
|
func TestWrite(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
|
|
|
written := []byte("Medusa")
|
|
|
|
g.Write(written)
|
|
|
|
g.Flush()
|
|
|
|
|
|
|
|
read := make([]byte, len(written))
|
|
|
|
gr, err := g.NewReader(0)
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "failed to create reader")
|
2017-10-24 12:20:17 -07:00
|
|
|
|
2017-10-30 10:48:51 -07:00
|
|
|
_, err = gr.Read(read)
|
|
|
|
assert.NoError(t, err, "failed to read data")
|
|
|
|
assert.Equal(t, written, read)
|
2017-10-24 12:20:17 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
2017-10-30 10:48:51 -07:00
|
|
|
// test that Read reads the required amount of bytes from all the files in the
|
|
|
|
// group and returns no error if n == size of the given slice.
|
2017-10-24 12:20:17 -07:00
|
|
|
func TestGroupReaderRead(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
|
|
|
professor := []byte("Professor Monster")
|
|
|
|
g.Write(professor)
|
|
|
|
g.Flush()
|
|
|
|
g.RotateFile()
|
|
|
|
frankenstein := []byte("Frankenstein's Monster")
|
|
|
|
g.Write(frankenstein)
|
|
|
|
g.Flush()
|
|
|
|
|
|
|
|
totalWrittenLength := len(professor) + len(frankenstein)
|
|
|
|
read := make([]byte, totalWrittenLength)
|
|
|
|
gr, err := g.NewReader(0)
|
2017-10-30 10:48:51 -07:00
|
|
|
require.NoError(t, err, "failed to create reader")
|
2017-10-24 12:20:17 -07:00
|
|
|
|
2017-10-30 10:48:51 -07:00
|
|
|
n, err := gr.Read(read)
|
|
|
|
assert.NoError(t, err, "failed to read data")
|
|
|
|
assert.Equal(t, totalWrittenLength, n, "not enough bytes read")
|
2017-10-24 12:20:17 -07:00
|
|
|
professorPlusFrankenstein := professor
|
|
|
|
professorPlusFrankenstein = append(professorPlusFrankenstein, frankenstein...)
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.Equal(t, professorPlusFrankenstein, read)
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that Read returns an error if number of bytes read < size of
|
|
|
|
// the given slice. Subsequent call should return 0, io.EOF.
|
|
|
|
func TestGroupReaderRead2(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
|
|
|
professor := []byte("Professor Monster")
|
|
|
|
g.Write(professor)
|
|
|
|
g.Flush()
|
|
|
|
g.RotateFile()
|
|
|
|
frankenstein := []byte("Frankenstein's Monster")
|
|
|
|
frankensteinPart := []byte("Frankenstein")
|
|
|
|
g.Write(frankensteinPart) // note writing only a part
|
|
|
|
g.Flush()
|
|
|
|
|
|
|
|
totalLength := len(professor) + len(frankenstein)
|
|
|
|
read := make([]byte, totalLength)
|
|
|
|
gr, err := g.NewReader(0)
|
|
|
|
require.NoError(t, err, "failed to create reader")
|
|
|
|
|
|
|
|
// 1) n < (size of the given slice), io.EOF
|
|
|
|
n, err := gr.Read(read)
|
|
|
|
assert.Equal(t, io.EOF, err)
|
|
|
|
assert.Equal(t, len(professor)+len(frankensteinPart), n, "Read more/less bytes than it is in the group")
|
|
|
|
|
|
|
|
// 2) 0, io.EOF
|
|
|
|
n, err = gr.Read([]byte("0"))
|
|
|
|
assert.Equal(t, io.EOF, err)
|
|
|
|
assert.Equal(t, 0, n)
|
2017-10-24 12:20:17 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinIndex(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.Zero(t, g.MinIndex(), "MinIndex should be zero at the beginning")
|
2017-10-24 12:20:17 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaxIndex(t *testing.T) {
|
|
|
|
g := createTestGroup(t, 0)
|
|
|
|
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.Zero(t, g.MaxIndex(), "MaxIndex should be zero at the beginning")
|
2017-10-24 12:20:17 -07:00
|
|
|
|
|
|
|
g.WriteLine("Line 1")
|
|
|
|
g.Flush()
|
|
|
|
g.RotateFile()
|
|
|
|
|
2017-10-30 10:48:51 -07:00
|
|
|
assert.Equal(t, 1, g.MaxIndex(), "MaxIndex should point to the last file")
|
2017-10-24 12:20:17 -07:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
destroyTestGroup(t, g)
|
|
|
|
}
|