Skip to content

Commit

Permalink
add snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
geekres committed Sep 4, 2019
1 parent bce42bb commit 6148755
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,83 @@ func TestSnapshot(t *testing.T) {

timestamp, err := snapshotManager.GetLastSnapshotTime()
if err != nil {
t.Error("set snapshot err", err)
t.Error("get snapshot err", err)
}

if timestamp != 100000000 {
t.Error("set snapshot err", err)
t.Error("get snapshot time err", err)
}

timestamp, err = snapshotManager.GetPrevSnapshotTime(100000000)
if err != nil {
t.Error("set snapshot err", err)
t.Error("get prev snapshot err", err)
}

_, _, err = snapshotManager.GetCurrentSnapshotHash()
if err != nil {
t.Error("set snapshot err", err)
t.Error("get current snapshot err", err)
}

_, err = snapshotManager.GetSnapshotMsg(addr, key, 100000000)
if err != nil {
t.Error("set snapshot err", err)
t.Error("get snapshot msg err", err)
}

_, err = snapshotManager.GetSnapshotState(100000000)
if err != nil {
t.Error("set snapshot err", err)
t.Error("get snapshot state err", err)
}
}

func TestSnapshotError(t *testing.T) {
db := mdb.NewMemDatabase()
batch := db.NewBatch()
cachedb := state.NewDatabase(db)
prevHash := common.Hash{}
state1, _ := state.New(prevHash, cachedb)

addr := "snapshot01"
key := "aaaaaa"
value := []byte("1")
state1.Put(addr, key, value)

root, err := state1.Commit(batch, prevHash, 0)
if err != nil {
t.Error("commit trie err", err)
}

triedb := state1.Database().TrieDB()
triedb.Reference(root, common.Hash{})
if err := triedb.Commit(root, false); err != nil {
t.Error("commit db err", err)
}
triedb.Dereference(root)

state2, _ := state.New(root, cachedb)
snapshotManager := NewSnapshotManager(state2)

_, err = snapshotManager.GetLastSnapshotTime()
if err == nil {
t.Error("get snapshot err", err)
}

_, err = snapshotManager.GetPrevSnapshotTime(100000000)
if err == nil {
t.Error("get prev snapshot err", err)
}

_, _, err = snapshotManager.GetCurrentSnapshotHash()
if err == nil {
t.Error("get current snapshot err", err)
}

_, err = snapshotManager.GetSnapshotMsg(addr, key, 100000000)
if err == nil {
t.Error("get snapshot msg err", err)
}

_, err = snapshotManager.GetSnapshotState(100000000)
if err == nil {
t.Error("get snapshot state err", err)
}
}

0 comments on commit 6148755

Please sign in to comment.