create basic controller
This commit is contained in:
+1
-1
@@ -75,7 +75,7 @@ func (f *fs) Lock() (func() error, error) {
|
||||
return unlock, nil
|
||||
}
|
||||
|
||||
func (f *fs) Exist(path string) (bool, error) {
|
||||
func (f *fs) Exists(path string) (bool, error) {
|
||||
return fileExists(filepath.Join(f.root, path))
|
||||
}
|
||||
|
||||
|
||||
+14
-20
@@ -6,35 +6,33 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/iliadenisov/galaxy/pkg/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewFileStorageSuccess(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
_, err := NewFileStorage(root)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestLock(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage")
|
||||
unlock, err := fs.Lock()
|
||||
assert.NoError(t, err, "acquire lock")
|
||||
exists, err := fileExists(filepath.Join(root, lockFile))
|
||||
assert.NoError(t, err, "check that the lock file should exist")
|
||||
assert.True(t, exists, "lock file must exists")
|
||||
lockPath := filepath.Join(root, lockFile)
|
||||
assert.FileExists(t, lockPath, "lock file should be created")
|
||||
err = unlock()
|
||||
assert.NoError(t, err, "unlocking existing lock")
|
||||
exists, err = fileExists(filepath.Join(root, lockFile))
|
||||
assert.NoError(t, err, "check that the lock file does not exist")
|
||||
assert.False(t, exists, "lock file must be removed")
|
||||
assert.NoFileExists(t, lockPath, "lock file must be removed")
|
||||
}
|
||||
|
||||
func TestExist(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
|
||||
fileName := "some-file.ext"
|
||||
@@ -45,17 +43,17 @@ func TestExist(t *testing.T) {
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage")
|
||||
|
||||
exist, err := fs.Exist(fileName)
|
||||
exist, err := fs.Exists(fileName)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, exist)
|
||||
|
||||
exist, err = fs.Exist("random/path")
|
||||
exist, err = fs.Exists("random/path")
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, exist)
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage: %s", err)
|
||||
@@ -85,9 +83,7 @@ func TestWrite(t *testing.T) {
|
||||
if err != nil {
|
||||
assert.Fail(t, "not expecting an error", "write to file %s: %s", tc.path, err)
|
||||
} else {
|
||||
exists, err := fileExists(filepath.Join(root, tc.path))
|
||||
assert.NoError(t, err, "check is written file exists")
|
||||
assert.True(t, exists, "the written file should exist")
|
||||
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
|
||||
}
|
||||
} else if tc.err != "" {
|
||||
if err == nil {
|
||||
@@ -104,7 +100,7 @@ func TestWrite(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage: %s", err)
|
||||
@@ -150,9 +146,7 @@ func TestRead(t *testing.T) {
|
||||
if err != nil {
|
||||
assert.Fail(t, "read: not expecting an error, got: "+err.Error())
|
||||
} else {
|
||||
exists, err := fileExists(filepath.Join(root, tc.path))
|
||||
assert.NoError(t, err, "check is written file exists")
|
||||
assert.True(t, exists, "the written file should exist")
|
||||
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
|
||||
}
|
||||
} else if tc.err != "" {
|
||||
if err == nil {
|
||||
@@ -166,7 +160,7 @@ func TestRead(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWriteErrorWithoutLock(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage")
|
||||
|
||||
@@ -1,28 +1,13 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
nonWritableDir = "/usr/lib"
|
||||
)
|
||||
|
||||
func createWorkDir(t *testing.T) (string, func()) {
|
||||
t.Helper()
|
||||
dir, err := os.MkdirTemp("", "fs-test-workdir")
|
||||
if err != nil {
|
||||
t.Fatalf("create temp dir: %s", err)
|
||||
}
|
||||
return dir, func() {
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatalf("remove temp dir: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type sampleData struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
@@ -6,30 +6,31 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/pkg/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPathExists(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
testDirExistsFunc(t, root, func(s string) (bool, error) { return pathExists(s, true) })
|
||||
testFileExistsFunc(t, root, func(s string) (bool, error) { return pathExists(s, false) })
|
||||
}
|
||||
|
||||
func TestDirExists(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
testDirExistsFunc(t, root, dirExists)
|
||||
}
|
||||
|
||||
func TestFileExists(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
testFileExistsFunc(t, root, fileExists)
|
||||
}
|
||||
|
||||
func TestWritable(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
ok, err := writable(root)
|
||||
assert.NoError(t, err, "directory writable check")
|
||||
|
||||
+26
-3
@@ -14,13 +14,17 @@ import (
|
||||
"github.com/iliadenisov/galaxy/pkg/model/game"
|
||||
)
|
||||
|
||||
const (
|
||||
statePath = "state.json"
|
||||
)
|
||||
|
||||
func (r *repo) SaveTurn(t uint, g game.Game) error {
|
||||
return saveTurn(r.s, t, g)
|
||||
}
|
||||
|
||||
func saveTurn(s Storage, t uint, g game.Game) error {
|
||||
path := fmt.Sprintf("%03d/state.json", t)
|
||||
exist, err := s.Exist(path)
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
@@ -40,9 +44,28 @@ func (r *repo) SaveState(g game.Game) error {
|
||||
}
|
||||
|
||||
func saveState(s Storage, g game.Game) error {
|
||||
path := "state.json"
|
||||
if err := s.Write(path, g); err != nil {
|
||||
if err := s.Write(statePath, g); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) LoadState() (game.Game, error) {
|
||||
return loadState(r.s)
|
||||
}
|
||||
|
||||
func loadState(s Storage) (game.Game, error) {
|
||||
var g game.Game
|
||||
path := statePath
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return g, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return g, NewStateError("latest state was never stored")
|
||||
}
|
||||
if err := s.Read(path, &g); err != nil {
|
||||
return g, NewStorageError(err)
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
+38
-2
@@ -21,13 +21,14 @@ func NewStateError(msg string) error {
|
||||
|
||||
type Storage interface {
|
||||
Lock() (func() error, error)
|
||||
Exist(string) (bool, error)
|
||||
Exists(string) (bool, error)
|
||||
Write(string, encoding.BinaryMarshaler) error
|
||||
Read(string, encoding.BinaryUnmarshaler) error
|
||||
}
|
||||
|
||||
type repo struct {
|
||||
s Storage
|
||||
s Storage
|
||||
release func() error
|
||||
}
|
||||
|
||||
func NewRepo(s Storage) (*repo, error) {
|
||||
@@ -44,3 +45,38 @@ func NewFileRepo(path string) (*repo, error) {
|
||||
}
|
||||
return NewRepo(s)
|
||||
}
|
||||
|
||||
func (r *repo) Lock() (err error) {
|
||||
if r.s == nil {
|
||||
return errors.New("storage is closed")
|
||||
}
|
||||
if r.release != nil {
|
||||
return errors.New("storage already locked")
|
||||
}
|
||||
r.release, err = r.s.Lock()
|
||||
if err != nil {
|
||||
r.close()
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) Release() (err error) {
|
||||
if r.s == nil {
|
||||
return errors.New("storage is closed")
|
||||
}
|
||||
if r.release == nil {
|
||||
return errors.New("storage was never locked")
|
||||
}
|
||||
err = r.release()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
r.close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) close() {
|
||||
r.release = nil
|
||||
r.s = nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user