2015-02-22 02:34:58 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-05-25 17:05:48 +02:00
|
|
|
"sync"
|
2015-02-22 02:34:58 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-02-15 23:20:59 +01:00
|
|
|
func TestBackupState_locker(t *testing.T) {
|
|
|
|
var _ Locker = new(BackupState)
|
|
|
|
}
|
|
|
|
|
2015-02-22 02:34:58 +01:00
|
|
|
func TestBackupState(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
ls := testLocalState(t)
|
|
|
|
defer os.Remove(ls.Path)
|
|
|
|
TestState(t, &BackupState{
|
|
|
|
Real: ls,
|
|
|
|
Path: f.Name(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if fi, err := os.Stat(f.Name()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
} else if fi.Size() == 0 {
|
|
|
|
t.Fatalf("bad: %d", fi.Size())
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 17:05:48 +02:00
|
|
|
|
|
|
|
func TestBackupStateRace(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
ls := testLocalState(t)
|
|
|
|
defer os.Remove(ls.Path)
|
|
|
|
bs := &BackupState{
|
|
|
|
Real: ls,
|
|
|
|
Path: f.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
current := TestStateInitial()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
bs.WriteState(current)
|
|
|
|
bs.PersistState()
|
|
|
|
bs.RefreshState()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|