92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
|
package fstore
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"math/rand"
|
||
|
"path/filepath"
|
||
|
"strconv"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func (s *Store) ReadString(t *testing.T, filePath string) string {
|
||
|
buf := &bytes.Buffer{}
|
||
|
if err := s.WriteTo(buf, filePath); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
return buf.String()
|
||
|
}
|
||
|
|
||
|
func (s *Store) AssertState(t *testing.T, in map[string]string) {
|
||
|
state := NewStoreState(in)
|
||
|
s.AssertStateDir(t, state)
|
||
|
}
|
||
|
|
||
|
func (s *Store) AssertStateDir(t *testing.T, dir StoreState) {
|
||
|
dirs, files, err := s.List(dir.Path)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// check file lengths.
|
||
|
if len(files) != len(dir.Files) {
|
||
|
t.Fatal(files, dir.Files)
|
||
|
}
|
||
|
|
||
|
// check dir lengths.
|
||
|
if len(dirs) != len(dir.Dirs) {
|
||
|
t.Fatal(dirs, dir.Dirs)
|
||
|
}
|
||
|
|
||
|
for _, file := range dir.Files {
|
||
|
expectedContent := file.FileData
|
||
|
actualContent := s.ReadString(t, file.Path)
|
||
|
if expectedContent != actualContent {
|
||
|
t.Fatal(expectedContent, actualContent)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for _, dir := range dir.Dirs {
|
||
|
s.AssertStateDir(t, dir)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Store) WriteRandomFor(dt time.Duration) map[string]string {
|
||
|
state := map[string]string{}
|
||
|
tStart := time.Now()
|
||
|
for time.Since(tStart) < dt {
|
||
|
slug1 := strconv.FormatInt(rand.Int63n(10), 10)
|
||
|
slug2 := strconv.FormatInt(rand.Int63n(10), 10)
|
||
|
|
||
|
path := filepath.Join("/", slug1, slug2)
|
||
|
|
||
|
if rand.Float32() < 0.05 {
|
||
|
if err := s.Remove(path); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
delete(state, path)
|
||
|
} else {
|
||
|
data := randString()
|
||
|
state[path] = data
|
||
|
if err := s.StoreString(data, path); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
time.Sleep(time.Millisecond)
|
||
|
}
|
||
|
|
||
|
return state
|
||
|
}
|
||
|
|
||
|
func (s *Store) WaitForParity(rhs *Store) {
|
||
|
for {
|
||
|
i1 := s.rep.Info()
|
||
|
i2 := rhs.rep.Info()
|
||
|
if i1.AppSeqNum == i2.AppSeqNum {
|
||
|
return
|
||
|
}
|
||
|
time.Sleep(time.Millisecond)
|
||
|
}
|
||
|
}
|