-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers_test.go
121 lines (105 loc) · 2.77 KB
/
helpers_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package arbo
import (
"bytes"
"io"
"os"
"testing"
"time"
qt "github.com/frankban/quicktest"
"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/db/pebbledb"
)
func checkRoots(c *qt.C, tree1, tree2 *Tree) {
root1, err := tree1.Root()
c.Assert(err, qt.IsNil)
root2, err := tree2.Root()
c.Assert(err, qt.IsNil)
if !bytes.Equal(root2, root1) {
dir := "err-dump"
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, os.ModePerm)
c.Assert(err, qt.IsNil)
}
// store tree1
storeTree(c, tree1, dir+"/tree1")
// store tree2
storeTree(c, tree2, dir+"/tree2")
root1, err := tree1.Root()
c.Assert(err, qt.IsNil)
root2, err := tree2.Root()
c.Assert(err, qt.IsNil)
c.Check(root2, qt.DeepEquals, root1)
}
}
func storeTree(c *qt.C, tree *Tree, path string) {
dump, err := tree.Dump(nil)
c.Assert(err, qt.IsNil)
err = os.WriteFile(path+"-"+time.Now().String()+".debug", dump, 0600)
c.Assert(err, qt.IsNil)
}
// nolint:unused
func readTree(c *qt.C, tree *Tree, path string) {
b, err := os.ReadFile(path) //nolint:gosec
c.Assert(err, qt.IsNil)
err = tree.ImportDump(b)
c.Assert(err, qt.IsNil)
}
// nolint:unused
func importDumpLoopAdd(tree *Tree, b []byte) error {
r := bytes.NewReader(b)
var err error
for {
l := make([]byte, 2)
_, err = io.ReadFull(r, l)
if err == io.EOF {
break
} else if err != nil {
return err
}
k := make([]byte, l[0])
_, err = io.ReadFull(r, k)
if err != nil {
return err
}
v := make([]byte, l[1])
_, err = io.ReadFull(r, v)
if err != nil {
return err
}
err = tree.Add(k, v)
if err != nil {
return err
}
}
return nil
}
func TestReadTreeDBG(t *testing.T) {
t.Skip() // test just for debugging purposes, disabled by default
c := qt.New(t)
database1, err := pebbledb.New(db.Options{Path: c.TempDir()})
c.Assert(err, qt.IsNil)
tree1, err := NewTree(Config{Database: database1, MaxLevels: 100,
HashFunction: HashFunctionBlake2b})
c.Assert(err, qt.IsNil)
database2, err := pebbledb.New(db.Options{Path: c.TempDir()})
c.Assert(err, qt.IsNil)
tree2, err := NewTree(Config{Database: database2, MaxLevels: 100,
HashFunction: HashFunctionBlake2b})
c.Assert(err, qt.IsNil)
// tree1 is generated by a loop of .Add
path := "err-dump/tree1-2021-06-03 16:45:54.104449306 +0200 CEST m=+0.073874545.debug"
b, err := os.ReadFile(path)
c.Assert(err, qt.IsNil)
err = importDumpLoopAdd(tree1, b)
c.Assert(err, qt.IsNil)
// tree2 is generated by .AddBatch
path = "err-dump/tree2-2021-06-03 16:45:54.104525519 +0200 CEST m=+0.073950756.debug"
readTree(c, tree2, path)
// tree1.PrintGraphvizFirstNLevels(nil, 6)
// tree2.PrintGraphvizFirstNLevels(nil, 6)
root1, err := tree1.Root()
c.Assert(err, qt.IsNil)
root2, err := tree2.Root()
c.Assert(err, qt.IsNil)
c.Check(root2, qt.DeepEquals, root1)
}