-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathclock_test.go
181 lines (155 loc) · 4.61 KB
/
clock_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package clock
import (
"context"
"testing"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/core/crdt"
"github.com/sourcenetwork/defradb/store"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
mh "github.com/multiformats/go-multihash"
)
func newDS() ds.Datastore {
return ds.NewMapDatastore()
}
func newTestMerkleClock() *MerkleClock {
s := newDS()
rw := store.AsDSReaderWriter(s)
multistore := store.MultiStoreFrom(rw)
reg := crdt.NewLWWRegister(rw, core.DataStoreKey{})
return NewMerkleClock(multistore.Headstore(), multistore.DAGstore(), core.HeadStoreKey{DocKey: "dockey", FieldId: "1"}, reg).(*MerkleClock)
}
func TestNewMerkleClock(t *testing.T) {
s := newDS()
rw := store.AsDSReaderWriter(s)
multistore := store.MultiStoreFrom(rw)
reg := crdt.NewLWWRegister(rw, core.DataStoreKey{})
clk := NewMerkleClock(multistore.Headstore(), multistore.DAGstore(), core.HeadStoreKey{}, reg).(*MerkleClock)
if clk.headstore != multistore.Headstore() {
t.Error("MerkleClock store not correctly set")
} else if clk.headset.store == nil {
t.Error("MerkleClock head set not correctly set")
} else if clk.crdt == nil {
t.Error("MerkleClock CRDT not correctly set")
}
}
func TestMerkleClockPutBlock(t *testing.T) {
ctx := context.Background()
clk := newTestMerkleClock()
delta := &crdt.LWWRegDelta{
Data: []byte("test"),
}
node, err := clk.putBlock(ctx, nil, 0, delta)
if err != nil {
t.Errorf("Failed to putBlock, err: %v", err)
}
if len(node.Links()) != 0 {
t.Errorf("Node links should be empty. Have %v, want %v", len(node.Links()), 0)
return
}
// @todo Add DagSyncer check to tests
// @body Once we add the DagSyncer to the MerkleClock implementation it needs to be
// tested as well here.
}
func TetMerkleClockPutBlockWithHeads(t *testing.T) {
ctx := context.Background()
clk := newTestMerkleClock()
delta := &crdt.LWWRegDelta{
Data: []byte("test"),
}
pref := cid.Prefix{
Version: 1,
Codec: cid.Raw,
MhType: mh.SHA2_256,
MhLength: -1, // default length
}
// And then feed it some data
c, err := pref.Sum([]byte("Hello World!"))
if err != nil {
t.Error("Failed to create new head CID:", err)
return
}
heads := []cid.Cid{c}
node, err := clk.putBlock(ctx, heads, 0, delta)
if err != nil {
t.Error("Failed to putBlock with heads:", err)
return
}
if len(node.Links()) != 1 {
t.Errorf("putBlock has incorrect number of heads. Have %v, want %v", len(node.Links()), 1)
}
}
func TestMerkleClockAddDAGNode(t *testing.T) {
ctx := context.Background()
clk := newTestMerkleClock()
delta := &crdt.LWWRegDelta{
Data: []byte("test"),
}
_, _, err := clk.AddDAGNode(ctx, delta)
if err != nil {
t.Error("Failed to add dag node:", err)
return
}
}
func TestMerkleClockAddDAGNodeWithHeads(t *testing.T) {
ctx := context.Background()
clk := newTestMerkleClock()
delta := &crdt.LWWRegDelta{
Data: []byte("test1"),
}
_, _, err := clk.AddDAGNode(ctx, delta)
if err != nil {
t.Error("Failed to add dag node:", err)
return
}
delta2 := &crdt.LWWRegDelta{
Data: []byte("test2"),
}
_, _, err = clk.AddDAGNode(ctx, delta2)
if err != nil {
t.Error("Failed to add second dag node with err:", err)
return
}
if delta.GetPriority() != 1 && delta2.GetPriority() != 2 {
t.Errorf("AddDAGNOde failed with incorrect delta priority vals, want (%v) (%v), have (%v) (%v)", 1, 2, delta.GetPriority(), delta2.GetPriority())
}
// check if lww state is correct (val is test2)
// check if head/blockstore state is correct (one head, two blocks)
nHeads, err := clk.headset.Len(ctx)
if err != nil {
t.Error("Error getting MerkleClock heads size:", err)
return
}
if nHeads != 1 {
t.Errorf("Incorrect number of heads of current clock state, have %v, want %v", nHeads, 1)
return
}
numBlocks := 0
cids, err := clk.dagstore.AllKeysChan(ctx)
if err != nil {
t.Error("Failed to get blockstore content for merkle clock:", err)
return
}
for range cids {
numBlocks++
}
if numBlocks != 2 {
t.Errorf("Incorrect number of blocks in clock state, have %v, want %v", numBlocks, 2)
return
}
}
// func TestMerkleClockProcessNode(t *testing.T) {
// t.Error("Test not implemented")
// }
// func TestMerkleClockProcessNodeWithHeads(t *testing.T) {
// t.Error("Test not implemented")
// }