-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_vector_test.go
More file actions
343 lines (316 loc) · 10.1 KB
/
Copy pathversion_vector_test.go
File metadata and controls
343 lines (316 loc) · 10.1 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package yin
import (
"fmt"
"math/rand"
"reflect"
"testing"
"testing/quick"
)
func TestVersionVectorGetIncrementAndCopySemantics(t *testing.T) {
source := map[ReplicaID]uint64{
ReplicaID("replica-a"): 1,
ReplicaID("replica-b"): 0,
}
vector := NewVersionVector(source)
source[ReplicaID("replica-a")] = 99
source[ReplicaID("replica-c")] = 7
if got := vector.Get(ReplicaID("replica-a")); got != 1 {
t.Fatalf("NewVersionVector did not copy input: Get(replica-a) = %d, want 1", got)
}
if got := vector.Get(ReplicaID("replica-b")); got != 0 {
t.Fatalf("zero counters should read as absent: Get(replica-b) = %d, want 0", got)
}
if got := vector.Get(ReplicaID("replica-c")); got != 0 {
t.Fatalf("input mutation leaked into vector: Get(replica-c) = %d, want 0", got)
}
counters := vector.Counters()
counters[ReplicaID("replica-a")] = 42
if got := vector.Get(ReplicaID("replica-a")); got != 1 {
t.Fatalf("Counters did not return a copy: Get(replica-a) = %d, want 1", got)
}
incremented := vector.Increment(ReplicaID("replica-a"))
if got := vector.Get(ReplicaID("replica-a")); got != 1 {
t.Fatalf("Increment mutated receiver: original Get(replica-a) = %d, want 1", got)
}
if got := incremented.Get(ReplicaID("replica-a")); got != 2 {
t.Fatalf("Increment(replica-a).Get(replica-a) = %d, want 2", got)
}
if got := incremented.Increment(ReplicaID("replica-c")).Get(ReplicaID("replica-c")); got != 1 {
t.Fatalf("Increment on missing replica = %d, want 1", got)
}
}
func TestVersionVectorJoin(t *testing.T) {
left := NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 1,
})
right := NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 1,
ReplicaID("replica-c"): 5,
})
want := NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 1,
ReplicaID("replica-c"): 5,
})
joined := left.Join(right)
if !joined.Equal(want) {
t.Fatalf("Join = %#v, want %#v", joined.Counters(), want.Counters())
}
if got := left.Get(ReplicaID("replica-c")); got != 0 {
t.Fatalf("Join mutated receiver: left.Get(replica-c) = %d, want 0", got)
}
if got := right.Get(ReplicaID("replica-b")); got != 0 {
t.Fatalf("Join mutated argument: right.Get(replica-b) = %d, want 0", got)
}
}
func TestVersionVectorCompare(t *testing.T) {
tests := []struct {
name string
left VersionVector
right VersionVector
want VersionVectorComparison
}{
{
name: "empty equal",
left: VersionVector{},
right: VersionVector{},
want: VersionVectorEqual,
},
{
name: "same counters equal",
left: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 5,
}),
right: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 5,
}),
want: VersionVectorEqual,
},
{
name: "left dominates",
left: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 3,
ReplicaID("replica-b"): 5,
}),
right: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 5,
}),
want: VersionVectorDominates,
},
{
name: "left dominated by right",
left: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
}),
right: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 1,
}),
want: VersionVectorDominatedBy,
},
{
name: "concurrent",
left: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 3,
ReplicaID("replica-b"): 1,
}),
right: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 4,
}),
want: VersionVectorConcurrent,
},
{
name: "present counter dominates absent counter",
left: NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 1,
}),
right: VersionVector{},
want: VersionVectorDominates,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := tc.left.Compare(tc.right); got != tc.want {
t.Fatalf("Compare = %s, want %s", got, tc.want)
}
})
}
}
func TestVersionVectorSince(t *testing.T) {
current := NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-b"): 5,
ReplicaID("replica-c"): 7,
})
base := NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 1,
ReplicaID("replica-b"): 5,
ReplicaID("replica-d"): 9,
})
want := NewVersionVector(map[ReplicaID]uint64{
ReplicaID("replica-a"): 2,
ReplicaID("replica-c"): 7,
})
progress := current.Since(base)
if !progress.Equal(want) {
t.Fatalf("Since = %#v, want %#v", progress.Counters(), want.Counters())
}
if got := current.Since(current); !got.Equal(VersionVector{}) {
t.Fatalf("Since self = %#v, want empty", got.Counters())
}
if got := base.Since(current); !got.Equal(NewVersionVector(map[ReplicaID]uint64{ReplicaID("replica-d"): 9})) {
t.Fatalf("Since keeps only counters greater than base: got %#v", got.Counters())
}
}
func TestVersionVectorJoinLaws(t *testing.T) {
t.Run("commutative", func(t *testing.T) {
checkVersionVectorProperty(t, "join commutative", 0x7656_6a01, func(a, b quickVersionVector) bool {
return a.V.Join(b.V).Equal(b.V.Join(a.V))
})
})
t.Run("associative", func(t *testing.T) {
checkVersionVectorProperty(t, "join associative", 0x7656_6a02, func(a, b, c quickVersionVector) bool {
left := a.V.Join(b.V).Join(c.V)
right := a.V.Join(b.V.Join(c.V))
return left.Equal(right)
})
})
t.Run("idempotent", func(t *testing.T) {
checkVersionVectorProperty(t, "join idempotent", 0x7656_6a03, func(a quickVersionVector) bool {
return a.V.Join(a.V).Equal(a.V)
})
})
}
func TestVersionVectorCompareConsistentWithJoin(t *testing.T) {
var reason string
checkVersionVectorProperty(t, "compare consistent with join", 0x7656_6a04, func(a, b quickVersionVector) bool {
left := a.V
right := b.V
joined := left.Join(right)
cmp := left.Compare(right)
reverse := right.Compare(left)
if !comparisonIsReverse(cmp, reverse) {
reason = fmt.Sprintf("reverse compare mismatch: left=%#v right=%#v cmp=%s reverse=%s", left.Counters(), right.Counters(), cmp, reverse)
return false
}
switch cmp {
case VersionVectorEqual:
if joined.Equal(left) && joined.Equal(right) {
return true
}
reason = fmt.Sprintf("equal vectors should join to both: left=%#v right=%#v joined=%#v", left.Counters(), right.Counters(), joined.Counters())
return false
case VersionVectorDominates:
if joined.Equal(left) && !left.Equal(right) {
return true
}
reason = fmt.Sprintf("dominating left should equal join: left=%#v right=%#v joined=%#v", left.Counters(), right.Counters(), joined.Counters())
return false
case VersionVectorDominatedBy:
if joined.Equal(right) && !left.Equal(right) {
return true
}
reason = fmt.Sprintf("dominated left should join to right: left=%#v right=%#v joined=%#v", left.Counters(), right.Counters(), joined.Counters())
return false
case VersionVectorConcurrent:
if !joined.Equal(left) && !joined.Equal(right) {
return true
}
reason = fmt.Sprintf("concurrent vectors should join to a strict combined vector: left=%#v right=%#v joined=%#v", left.Counters(), right.Counters(), joined.Counters())
return false
default:
reason = fmt.Sprintf("unknown comparison result %d", cmp)
return false
}
}, &reason)
}
func TestVersionVectorIncrementMonotonic(t *testing.T) {
var reason string
checkVersionVectorProperty(t, "increment monotonic", 0x7656_6a05, func(vector quickVersionVector, replica quickReplicaID) bool {
before := vector.V
after := before.Increment(replica.ID)
if got, want := after.Get(replica.ID), before.Get(replica.ID)+1; got != want {
reason = fmt.Sprintf("incremented counter mismatch: before=%#v replica=%q got=%d want=%d", before.Counters(), replica.ID, got, want)
return false
}
if cmp := after.Compare(before); cmp != VersionVectorDominates {
reason = fmt.Sprintf("increment should strictly dominate: before=%#v after=%#v cmp=%s", before.Counters(), after.Counters(), cmp)
return false
}
return true
}, &reason)
}
func checkVersionVectorProperty(t *testing.T, name string, seed int64, property any, reasons ...*string) {
t.Helper()
cfg := &quick.Config{
MaxCount: 512,
Rand: rand.New(rand.NewSource(seed)),
}
if err := quick.Check(property, cfg); err != nil {
details := ""
if len(reasons) > 0 && reasons[0] != nil && *reasons[0] != "" {
details = "\nreason: " + *reasons[0]
}
t.Fatalf("%s failed with seed %#x: %v%s", name, seed, err, details)
}
}
func comparisonIsReverse(left, right VersionVectorComparison) bool {
switch left {
case VersionVectorEqual:
return right == VersionVectorEqual
case VersionVectorDominates:
return right == VersionVectorDominatedBy
case VersionVectorDominatedBy:
return right == VersionVectorDominates
case VersionVectorConcurrent:
return right == VersionVectorConcurrent
default:
return false
}
}
type quickVersionVector struct {
V VersionVector
}
func (quickVersionVector) Generate(r *rand.Rand, size int) reflect.Value {
replicas := []ReplicaID{
ReplicaID("replica-a"),
ReplicaID("replica-b"),
ReplicaID("replica-c"),
ReplicaID("replica-d"),
ReplicaID("replica-e"),
}
entries := r.Intn(len(replicas) + 1)
order := r.Perm(len(replicas))
counters := make(map[ReplicaID]uint64, entries)
for i := 0; i < entries; i++ {
// Keep counters far below uint64 max so Increment's monotonic property is
// not obscured by overflow behavior.
counters[replicas[order[i]]] = uint64(1 + r.Intn(128))
}
return reflect.ValueOf(quickVersionVector{V: NewVersionVector(counters)})
}
func (v quickVersionVector) String() string {
return fmt.Sprintf("%#v", v.V.Counters())
}
type quickReplicaID struct {
ID ReplicaID
}
func (quickReplicaID) Generate(r *rand.Rand, size int) reflect.Value {
replicas := []ReplicaID{
ReplicaID("replica-a"),
ReplicaID("replica-b"),
ReplicaID("replica-c"),
ReplicaID("replica-d"),
ReplicaID("replica-e"),
ReplicaID("replica-new"),
}
return reflect.ValueOf(quickReplicaID{ID: replicas[r.Intn(len(replicas))]})
}
func (id quickReplicaID) String() string {
return string(id.ID)
}