-
Notifications
You must be signed in to change notification settings - Fork 43
/
heads_test.go
152 lines (132 loc) · 3.22 KB
/
heads_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package crdt
import (
"bytes"
"context"
"math/rand"
"reflect"
"sort"
"testing"
"time"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/multiformats/go-multihash"
)
var headsTestNS = ds.NewKey("headstest")
var randg = rand.New(rand.NewSource(time.Now().UnixNano()))
// TODO we should also test with a non-batching store
func newTestHeads(t *testing.T) *heads {
t.Helper()
store := dssync.MutexWrap(ds.NewMapDatastore())
heads, err := newHeads(store, headsTestNS, &testLogger{
name: t.Name(),
l: DefaultOptions().Logger,
})
if err != nil {
t.Fatal(err)
}
return heads
}
func newCID(t *testing.T) cid.Cid {
t.Helper()
var buf [32]byte
_, _ = randg.Read(buf[:])
mh, err := multihash.Sum(buf[:], multihash.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}
return cid.NewCidV1(cid.DagProtobuf, mh)
}
func TestHeadsBasic(t *testing.T) {
ctx := context.Background()
heads := newTestHeads(t)
l, err := heads.Len()
if err != nil {
t.Fatal(err)
}
if l != 0 {
t.Errorf("new heads should have Len==0, got: %d", l)
}
cidHeights := make(map[cid.Cid]uint64)
numHeads := 5
for i := 0; i < numHeads; i++ {
c, height := newCID(t), uint64(randg.Int())
cidHeights[c] = height
err := heads.Add(ctx, c, height)
if err != nil {
t.Fatal(err)
}
}
assertHeads(t, heads, cidHeights)
for c := range cidHeights {
newC, newHeight := newCID(t), uint64(randg.Int())
err := heads.Replace(ctx, c, newC, newHeight)
if err != nil {
t.Fatal(err)
}
delete(cidHeights, c)
cidHeights[newC] = newHeight
assertHeads(t, heads, cidHeights)
}
// Now try creating a new heads object and make sure what we
// stored before is still there.
err = heads.store.Sync(ctx, headsTestNS)
if err != nil {
t.Fatal(err)
}
heads, err = newHeads(heads.store, headsTestNS, &testLogger{
name: t.Name(),
l: DefaultOptions().Logger,
})
if err != nil {
t.Fatal(err)
}
assertHeads(t, heads, cidHeights)
}
func assertHeads(t *testing.T, hh *heads, cidHeights map[cid.Cid]uint64) {
t.Helper()
headCids, maxHeight, err := hh.List()
if err != nil {
t.Fatal(err)
}
var expectedMaxHeight uint64
for _, height := range cidHeights {
if height > expectedMaxHeight {
expectedMaxHeight = height
}
}
if maxHeight != expectedMaxHeight {
t.Errorf("expected max height=%d, got=%d", expectedMaxHeight, maxHeight)
}
headsLen, err := hh.Len()
if err != nil {
t.Fatal(err)
}
if len(headCids) != headsLen {
t.Errorf("expected len and list to agree, got listLen=%d, len=%d", len(headCids), headsLen)
}
cids := make([]cid.Cid, 0, len(cidHeights))
for c := range cidHeights {
cids = append(cids, c)
}
sort.Slice(cids, func(i, j int) bool {
ci := cids[i].Bytes()
cj := cids[j].Bytes()
return bytes.Compare(ci, cj) < 0
})
if !reflect.DeepEqual(cids, headCids) {
t.Errorf("given cids don't match cids returned by List: %v, %v", cids, headCids)
}
for _, c := range cids {
present, height, err := hh.IsHead(c)
if err != nil {
t.Fatal(err)
}
if !present {
t.Errorf("cid returned by List reported absent by IsHead: %v", c)
}
if height != cidHeights[c] {
t.Errorf("expected cid %v to have height %d, got: %d", c, cidHeights[c], height)
}
}
}