2014-10-10 13:57:18 +04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/siddontang/ledisdb/config"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testSnapshotDumper struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *testSnapshotDumper) Dump(w io.Writer) error {
|
|
|
|
w.Write([]byte("hello world"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshot(t *testing.T) {
|
|
|
|
cfg := new(config.Config)
|
|
|
|
cfg.Snapshot.MaxNum = 2
|
|
|
|
cfg.Snapshot.Path = path.Join(os.TempDir(), "snapshot")
|
|
|
|
defer os.RemoveAll(cfg.Snapshot.Path)
|
|
|
|
|
|
|
|
d := new(testSnapshotDumper)
|
|
|
|
|
|
|
|
s, err := newSnapshotStore(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-10-11 06:14:23 +04:00
|
|
|
if f, _, err := s.Create(d, false); err != nil {
|
2014-10-10 13:57:18 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
defer f.Close()
|
|
|
|
if b, _ := ioutil.ReadAll(f); string(b) != "hello world" {
|
|
|
|
t.Fatal("invalid read snapshot")
|
|
|
|
}
|
2014-10-10 18:45:22 +04:00
|
|
|
|
|
|
|
if len(s.names) != 1 {
|
|
|
|
t.Fatal("must 1 snapshot")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 06:14:23 +04:00
|
|
|
if f, _, err := s.Create(d, false); err != nil {
|
2014-10-10 18:45:22 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
defer f.Close()
|
|
|
|
if b, _ := ioutil.ReadAll(f); string(b) != "hello world" {
|
|
|
|
t.Fatal("invalid read snapshot")
|
|
|
|
}
|
|
|
|
if len(s.names) != 2 {
|
|
|
|
t.Fatal("must 2 snapshot")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 06:14:23 +04:00
|
|
|
if f, _, err := s.Create(d, false); err != nil {
|
2014-10-10 18:45:22 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
defer f.Close()
|
|
|
|
if b, _ := ioutil.ReadAll(f); string(b) != "hello world" {
|
|
|
|
t.Fatal("invalid read snapshot")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.names) != 2 {
|
|
|
|
t.Fatal("must 2 snapshot")
|
|
|
|
}
|
2014-10-10 13:57:18 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 18:45:22 +04:00
|
|
|
fs, _ := ioutil.ReadDir(cfg.Snapshot.Path)
|
|
|
|
if len(fs) != 2 {
|
|
|
|
t.Fatal("must 2 snapshot")
|
2014-10-10 13:57:18 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 06:14:23 +04:00
|
|
|
if f, _, err := s.Create(d, true); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
if b, _ := ioutil.ReadAll(f); string(b) != "hello world" {
|
|
|
|
t.Fatal("invalid read snapshot")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.names) != 2 {
|
|
|
|
t.Fatal("must 2 snapshot")
|
|
|
|
}
|
|
|
|
|
|
|
|
fs, _ = ioutil.ReadDir(cfg.Snapshot.Path)
|
|
|
|
if len(fs) != 3 {
|
|
|
|
t.Fatal("must 3 snapshot")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
fs, _ = ioutil.ReadDir(cfg.Snapshot.Path)
|
|
|
|
if len(fs) != 2 {
|
|
|
|
t.Fatal("must 2 snapshot")
|
|
|
|
}
|
|
|
|
|
2014-10-10 13:57:18 +04:00
|
|
|
s.Close()
|
|
|
|
}
|