forked from sei-protocol/sei-iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orphandb_test.go
73 lines (67 loc) · 1.54 KB
/
orphandb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package iavl
import (
"fmt"
"io/ioutil"
"path"
"testing"
"github.com/stretchr/testify/require"
)
func TestOrphanDBSaveGet(t *testing.T) {
dir := t.TempDir()
db := NewOrphanDB(&Options{
NumOrphansPerFile: 2,
OrphanDirectory: dir,
})
err := db.SaveOrphans(123, map[string]int64{
"o1": 123,
"o2": 123,
"o3": 123,
})
require.Nil(t, err)
files, err := ioutil.ReadDir(path.Join(dir, fmt.Sprintf("%d", 123)))
require.Nil(t, err)
require.Equal(t, 2, len(files)) // 3 orphans would result in 2 files
orphans := db.GetOrphans(123)
require.Equal(t, map[string]int64{
"o1": 123,
"o2": 123,
"o3": 123,
}, orphans)
orphans = db.GetOrphans(456) // not exist
require.Equal(t, map[string]int64{}, orphans)
// flush cache
db = NewOrphanDB(&Options{
NumOrphansPerFile: 2,
OrphanDirectory: dir,
})
orphans = db.GetOrphans(123) // would load from disk
require.Equal(t, map[string]int64{
"o1": 123,
"o2": 123,
"o3": 123,
}, orphans)
}
func TestOrphanDelete(t *testing.T) {
dir := t.TempDir()
db := NewOrphanDB(&Options{
NumOrphansPerFile: 2,
OrphanDirectory: dir,
})
err := db.SaveOrphans(123, map[string]int64{
"o1": 123,
"o2": 123,
"o3": 123,
})
require.Nil(t, err)
err = db.DeleteOrphans(123)
require.Nil(t, err)
orphans := db.GetOrphans(123) // not exist in cache
require.Equal(t, map[string]int64{}, orphans)
// flush cache
db = NewOrphanDB(&Options{
NumOrphansPerFile: 2,
OrphanDirectory: dir,
})
orphans = db.GetOrphans(123) // would load from disk
require.Equal(t, map[string]int64{}, orphans)
}