Skip to content

Commit

Permalink
Add tests for pyramid file
Browse files Browse the repository at this point in the history
  • Loading branch information
itaiad200 committed Dec 2, 2020
1 parent 21c6826 commit 834af95
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pyramid/eviction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"github.com/dgraph-io/ristretto"
)

// eviction is an abstraction of the eviction control for easy testing
type eviction interface {
touch(rPath relativePath)
store(rPath relativePath, filesize int64) bool
}

type lruSizeEviction struct {
cache *ristretto.Cache
}
Expand Down
2 changes: 1 addition & 1 deletion pyramid/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// File is pyramid wrapper for os.file that triggers pyramid hooks for file actions.
type File struct {
fh *os.File
eviction *lruSizeEviction
eviction eviction

readOnly bool
rPath relativePath
Expand Down
113 changes: 113 additions & 0 deletions pyramid/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package pyramid

import (
"io/ioutil"
"os"
"path"
"testing"

"github.com/stretchr/testify/require"

"github.com/google/uuid"
)

func TestPyramidWriteFile(t *testing.T) {
filename := uuid.Must(uuid.NewRandom()).String()
filepath := path.Join("/tmp", filename)
defer os.Remove(filepath)

fh, err := os.Create(filepath)
if err != nil {
panic(err)
}

storeCalled := false
mockEv := newMockEviction()

sut := File{
fh: fh,
eviction: mockEv,
readOnly: false,
rPath: relativePath(filename),
store: func() error {
storeCalled = true
return nil
},
}

content := "some content to write to file"
n, err := sut.Write([]byte(content))
require.Equal(t, len(content), n)
require.NoError(t, err)
require.NoError(t, sut.Sync())

_, err = sut.Stat()
require.NoError(t, err)

require.NoError(t, sut.Close())

require.Equal(t, 0, mockEv.touchedTimes[relativePath(filename)])
require.True(t, storeCalled)
}

func TestPyramidReadFile(t *testing.T) {
filename := uuid.Must(uuid.NewRandom()).String()
filepath := path.Join("/tmp", filename)
content := "some content to write to file"
if err := ioutil.WriteFile(filepath,[]byte(content), os.ModePerm); err != nil{
panic(err)
}
defer os.Remove(filepath)

mockEv := newMockEviction()

fh, err := os.Open(filepath)
if err != nil{
panic(err)
}

sut := File{
fh: fh,
eviction: mockEv,
readOnly: true,
rPath: relativePath(filename),
store: nil,
}

// no writes to read only file
n, err := sut.Write([]byte(content))
require.Equal(t, 0, n)
require.Error(t, err)
require.Equal(t, ErrReadOnlyFile, err)

require.NoError(t, sut.Sync())
_, err = sut.Stat()
require.NoError(t, err)

bytes := make([]byte, len(content))
n, err = sut.Read(bytes)
require.NoError(t, err)
require.Equal(t, len(content), n)
require.Equal(t, content, string(bytes))
require.NoError(t, sut.Close())

require.Equal(t, 1, mockEv.touchedTimes[relativePath(filename)])
}

type mockEviction struct {
touchedTimes map[relativePath]int
}

func newMockEviction() *mockEviction {
return &mockEviction{
touchedTimes: map[relativePath]int{},
}
}

func (me *mockEviction) touch(rPath relativePath) {
me.touchedTimes[rPath]++
}

func (me *mockEviction) store(_ relativePath, _ int64) bool {
return true
}
2 changes: 1 addition & 1 deletion pyramid/tierFS_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
)

const blockStoragePrefix = "prefix"
const allocatedDiskBytes = 16 * 1024 * 1024
const allocatedDiskBytes = 4 * 1024 * 1024
const estimatedFileBytes = 1 * 1024 * 1024

func TestMain(m *testing.M) {
Expand Down

0 comments on commit 834af95

Please sign in to comment.