Skip to content

Commit

Permalink
Merge pull request #79 from yowcow/fix-findany-to-update-cur-prev-index
Browse files Browse the repository at this point in the history
Update l.curindex/l.previndex at FindAny
  • Loading branch information
yowcow authored Jun 27, 2018
2 parents 77e12f1 + 557b0ba commit 710e20a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
- docker
language: go
go:
- 1.x
- "1.10"
addons:
apt:
packages:
Expand Down
25 changes: 20 additions & 5 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,39 @@ func buildDirs(basedir string, count int) ([]string, error) {
}

// FindAny tries finding a file to load in any existing subdirectiries, and returns its filepath
func (l Loader) FindAny() (string, bool) {
func (l *Loader) FindAny() (string, bool) {
for i := 0; i < DirCount; i++ {
file := filepath.Join(l.dirs[i], l.filename)
if _, err := os.Stat(file); err == nil {
l.curindex = i
l.previndex = decrIndex(i)
return file, true
}
}
return "", false
}

func incrIndex(i int) int {
n := i + 1
if n >= DirCount {
return 0
}
return n
}

func decrIndex(i int) int {
n := i - 1
if n < 0 {
return DirCount - 1
}
return n
}

// DropIn drops given file into next subdirectory, and returns the filepath
func (l *Loader) DropIn(file string) (string, error) {
defer syscall.Sync() // make sure write is in sync

nextindex := l.curindex + 1
if nextindex >= len(l.dirs) {
nextindex = 0
}
nextindex := incrIndex(l.curindex)
nextdir := l.dirs[nextindex]
nextfile := filepath.Join(nextdir, l.filename)
if err := os.Rename(file, nextfile); err != nil {
Expand Down
69 changes: 49 additions & 20 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package loader

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -74,30 +75,58 @@ func TestNew(t *testing.T) {
}

func TestFindAny(t *testing.T) {
dir := testutil.CreateTmpDir()
defer os.RemoveAll(dir)
type Case struct {
subtest string
dirToCreateFile string
expectedOK bool
expectedPrevindex int
expectedCurindex int
}
cases := []Case{
{
"no pre-existing file",
"",
false,
-1,
-1,
},
{
"pre-existing file in data00",
"data00",
true,
1,
0,
},
{
"pre-existing file in data01",
"data01",
true,
0,
1,
},
}

loader, _ := New(dir, "test.data")
file00 := filepath.Join(dir, "data00", "test.data")
file01 := filepath.Join(dir, "data01", "test.data")
for _, c := range cases {
t.Run(c.subtest, func(t *testing.T) {
dir := testutil.CreateTmpDir()
defer os.RemoveAll(dir)

{
_, ok := loader.FindAny()
assert.False(t, ok)
}
l, _ := New(dir, "test.data")

{
testutil.CopyFile(file01, "loader_test.go")
file, ok := loader.FindAny()
assert.True(t, ok)
assert.Equal(t, file01, file)
}
expectedFile := ""
if c.dirToCreateFile != "" {
expectedFile = filepath.Join(dir, c.dirToCreateFile, "test.data")
testutil.CopyFile(expectedFile, "loader_test.go")
}
fmt.Println(expectedFile)

{
testutil.CopyFile(file00, "loader_test.go")
file, ok := loader.FindAny()
assert.True(t, ok)
assert.Equal(t, file00, file)
file, ok := l.FindAny()

assert.Equal(t, c.expectedOK, ok)
assert.Equal(t, expectedFile, file)
assert.Equal(t, c.expectedPrevindex, l.previndex)
assert.Equal(t, c.expectedCurindex, l.curindex)
})
}
}

Expand Down

0 comments on commit 710e20a

Please sign in to comment.