-
Notifications
You must be signed in to change notification settings - Fork 7
/
crdt_test.go
309 lines (266 loc) · 6.93 KB
/
crdt_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package riaken_core
import (
"testing"
)
func TestCrdtCounter(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
// Create a new Counter
bucket := session.GetBucket("crdt_counter").Type("test_counters")
counter := bucket.Crdt("foo").NewCounter()
counter.Increment(1)
if _, err := counter.Commit(); err != nil {
t.Fatal(err.Error())
}
// Fetch without considering type
crdt := bucket.Crdt("foo")
if _, err := crdt.Fetch(); err != nil {
t.Fatal(err.Error())
}
if crdt.Counter == nil {
t.Fatal("exepected Counter to exist")
}
if crdt.Counter.Value != 1 {
t.Errorf("expected: %d, got: %d", 1, crdt.Counter.Value)
}
// Increment
crdt.Counter.Increment(5)
if _, err := crdt.Counter.Commit(); err != nil {
t.Fatal(err.Error())
}
// Refetch with new object
crdt = bucket.Crdt("foo")
if _, err := crdt.Fetch(); err != nil {
t.Fatal(err.Error())
}
if crdt.Counter == nil {
t.Fatal("exepected Counter to exist")
}
if crdt.Counter.Value != 6 {
t.Errorf("expected: %d, got: %d", 6, crdt.Counter.Value)
}
// Decrement
crdt.Counter.Decrement(3)
if _, err := crdt.Counter.Commit(); err != nil {
t.Fatal(err.Error())
}
if crdt.Counter.Value != 3 {
t.Errorf("expected: %d, got: %d", 3, crdt.Counter.Value)
}
// Delete with standard object delete
object := bucket.Object("foo")
if _, err := object.Delete(); err != nil {
t.Fatal(err.Error())
}
}
func TestCrdtSet(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
// Create a new Set
bucket := session.GetBucket("crdt_set").Type("test_sets")
set := bucket.Crdt("foo").NewSet()
set.Add("bar")
set.Add("baz")
if _, err := set.Commit(); err != nil {
t.Fatal(err.Error())
}
// Fetch without considering type
crdt := bucket.Crdt("foo")
if _, err := crdt.Fetch(); err != nil {
t.Fatal(err.Error())
}
if crdt.Set == nil {
t.Fatal("exepected Set to exist")
}
if len(crdt.Set.Values) != 2 {
t.Fatal("exepected 2 values")
}
crdt.Set.Add("aaa")
crdt.Set.Add("bbb")
crdt.Set.Remove("baz")
if _, err := crdt.Set.Commit(); err != nil {
t.Fatal(err.Error())
}
crdt = bucket.Crdt("foo")
if _, err := crdt.Fetch(); err != nil {
t.Fatal(err.Error())
}
if len(crdt.Set.Values) != 3 {
t.Fatal("exepected 3 values")
}
// Delete with standard object delete
object := bucket.Object("foo")
if _, err := object.Delete(); err != nil {
t.Fatal(err.Error())
}
}
func TestCrdtMap(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
// Create a new Map
bucket := session.GetBucket("crdt_map").Type("test_maps")
crdt := bucket.Crdt("foo")
mp := crdt.NewMap()
// Add Flags to the Map
mp.Flags["f1"] = true
mp.Flags["f2"] = false
// Add Registers to the Map
mp.Registers["r1"] = "1r"
mp.Registers["r2"] = "2r"
// Add Counters to the Map
mp.Counters["c1"] = crdt.NewCounter()
mp.Counters["c1"].Increment(10)
// Add Sets to the Map
mp.Sets["s1"] = crdt.NewSet()
mp.Sets["s1"].Add("1")
mp.Sets["s1"].Add("2")
mp.Sets["s1"].Add("3")
mp.Sets["s2"] = crdt.NewSet()
mp.Sets["s2"].Add("a")
mp.Sets["s2"].Add("b")
// Add Maps to the Map
mp.Maps["m1"] = crdt.NewMap()
mp.Maps["m1"].Flags["ff1"] = true
mp.Maps["m1"].Registers["rr1"] = "1rr"
mp.Maps["m1"].Counters["cc1"] = crdt.NewCounter()
mp.Maps["m1"].Counters["cc1"].Increment(20)
mp.Maps["m1"].Sets["ss1"] = crdt.NewSet()
mp.Maps["m1"].Sets["ss1"].Add("111")
mp.Maps["m1"].Sets["ss1"].Add("222")
mp.Maps["m1"].Maps["mm1"] = crdt.NewMap()
mp.Maps["m1"].Maps["mm1"].Flags["fff1"] = false
mp.Maps["m1"].Maps["mm2"] = crdt.NewMap()
mp.Maps["m1"].Maps["mm2"].Registers["123"] = "abc"
if _, err := mp.Commit(); err != nil {
t.Fatal(err.Error())
}
// Fetch without considering type
crdt = bucket.Crdt("foo")
if _, err := crdt.Fetch(); err != nil {
t.Fatal(err.Error())
}
if crdt.Map == nil {
t.Fatal("exepected Map to exist")
}
if v, ok := crdt.Map.Flags["f1"]; !ok {
t.Error("expected map->flag->f1 to exist")
} else {
if !v {
t.Error("flag should be true")
}
}
if v, ok := crdt.Map.Registers["r1"]; !ok {
t.Error("expected map->registers->r1 to exist")
} else {
if v != "1r" {
t.Errorf("expected: %s, got: %s", "1r", v)
}
}
if v, ok := crdt.Map.Counters["c1"]; !ok {
t.Error("expected map->counters->c1 to exist")
} else {
if v.Value != 10 {
t.Errorf("expected: %d, got: %d", 10, v.Value)
}
}
if v, ok := crdt.Map.Sets["s1"]; !ok {
t.Error("expected map->sets->s1 to exist")
} else {
if len(v.Values) != 3 {
t.Errorf("expected: %d, got: %d", 3, len(v.Values))
}
}
if v, ok := crdt.Map.Maps["m1"]; !ok {
t.Error("expected map->maps->m1 to exist")
} else {
if b, ok := v.Flags["ff1"]; !ok {
t.Error("expected map->maps->m1->flags->ff1 to exist")
} else if !b {
t.Error("flag should be true")
}
}
if m1, ok := crdt.Map.Maps["m1"]; !ok {
t.Error("expected map->maps->m1 to exist")
} else {
if m2, ok := m1.Maps["mm2"]; !ok {
t.Error("expected map->maps->mm2 to exist")
} else {
if r, ok := m2.Registers["123"]; !ok {
t.Error("expected map->maps->mm2->registers->123 to exist")
} else {
if r != "abc" {
t.Error("register should be abc")
}
}
}
}
// Change Values
crdt.Map.Flags["f1"] = false
crdt.Map.Remove(CRDT_MAP_REGISTER, "r2")
crdt.Map.Counters["c1"].Decrement(6)
crdt.Map.Sets["s1"].Remove("1")
crdt.Map.Maps["m1"].Registers["rr1"] = "rr1rr"
crdt.Map.Maps["m1"].Remove(CRDT_MAP_MAP, "mm2")
if _, err := crdt.Map.Commit(); err != nil {
t.Fatal(err.Error())
}
// Fetch without considering type
crdt = bucket.Crdt("foo")
if _, err := crdt.Fetch(); err != nil {
t.Fatal(err.Error())
}
if crdt.Map == nil {
t.Fatal("exepected Map to exist")
}
if v, ok := crdt.Map.Flags["f1"]; !ok {
t.Error("expected map->flag->f1 to exist")
} else {
if v {
t.Error("flag should be false")
}
}
if _, ok := crdt.Map.Registers["r2"]; ok {
t.Error("expected map->registers->r2 to not exist")
}
if v, ok := crdt.Map.Counters["c1"]; !ok {
t.Error("expected map->counters->c1 to exist")
} else {
if v.Value != 4 {
t.Errorf("expected: %d, got: %d", 4, v.Value)
}
}
if v, ok := crdt.Map.Sets["s1"]; !ok {
t.Error("expected map->sets->s1 to exist")
} else {
if len(v.Values) != 2 {
t.Errorf("expected: %d, got: %d", 2, len(v.Values))
}
}
if v, ok := crdt.Map.Maps["m1"]; !ok {
t.Error("expected map->maps->m1 to exist")
} else {
if r, ok := v.Registers["rr1"]; !ok {
t.Error("expected map->maps->m1->registers->rr1 to exist")
} else if r != "rr1rr" {
t.Errorf("expected: %s, got: %s", "rr1rr", r)
}
}
if m1, ok := crdt.Map.Maps["m1"]; !ok {
t.Error("expected map->maps->m1 to exist")
} else {
if _, ok := m1.Maps["mm2"]; ok {
t.Error("map->maps->m1->maps->mm2 should not exist")
}
}
// Delete with standard object delete
object := bucket.Object("foo")
if _, err := object.Delete(); err != nil {
t.Fatal(err.Error())
}
}