-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp_test.go
More file actions
156 lines (147 loc) · 4.2 KB
/
Copy pathtimestamp_test.go
File metadata and controls
156 lines (147 loc) · 4.2 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
package yin
import "testing"
func TestTimestampCompare(t *testing.T) {
tests := []struct {
name string
left Timestamp
right Timestamp
want int
}{
{
name: "equal",
left: NewTimestamp(3, ReplicaID("replica-a")),
right: NewTimestamp(3, ReplicaID("replica-a")),
want: 0,
},
{
name: "less by counter",
left: NewTimestamp(2, ReplicaID("replica-z")),
right: NewTimestamp(3, ReplicaID("replica-a")),
want: -1,
},
{
name: "greater by counter",
left: NewTimestamp(4, ReplicaID("replica-a")),
right: NewTimestamp(3, ReplicaID("replica-z")),
want: 1,
},
{
name: "less by replica tiebreaker",
left: NewTimestamp(5, ReplicaID("replica-a")),
right: NewTimestamp(5, ReplicaID("replica-b")),
want: -1,
},
{
name: "greater by replica tiebreaker",
left: NewTimestamp(5, ReplicaID("replica-b")),
right: NewTimestamp(5, ReplicaID("replica-a")),
want: 1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := normalizeComparison(tc.left.Compare(tc.right)); got != tc.want {
t.Fatalf("Timestamp.Compare() = %d, want %d", got, tc.want)
}
})
}
}
func TestTimestampAdvance(t *testing.T) {
maxCounter := ^uint64(0)
tests := []struct {
name string
local Timestamp
seen Timestamp
replica ReplicaID
wantCounter uint64
wantReplicaID ReplicaID
}{
{
name: "starts from zero",
local: NewTimestamp(0, ReplicaID("replica-a")),
seen: NewTimestamp(0, ReplicaID("replica-a")),
replica: ReplicaID("replica-a"),
wantCounter: 1,
wantReplicaID: ReplicaID("replica-a"),
},
{
name: "local counter wins",
local: NewTimestamp(7, ReplicaID("replica-a")),
seen: NewTimestamp(3, ReplicaID("replica-b")),
replica: ReplicaID("replica-a"),
wantCounter: 8,
wantReplicaID: ReplicaID("replica-a"),
},
{
name: "seen counter wins",
local: NewTimestamp(7, ReplicaID("replica-a")),
seen: NewTimestamp(12, ReplicaID("replica-b")),
replica: ReplicaID("replica-a"),
wantCounter: 13,
wantReplicaID: ReplicaID("replica-a"),
},
{
name: "equal counters still tick",
local: NewTimestamp(12, ReplicaID("replica-a")),
seen: NewTimestamp(12, ReplicaID("replica-z")),
replica: ReplicaID("replica-a"),
wantCounter: 13,
wantReplicaID: ReplicaID("replica-a"),
},
{
name: "advances to maximum counter",
local: NewTimestamp(maxCounter-1, ReplicaID("replica-a")),
seen: NewTimestamp(1, ReplicaID("replica-b")),
replica: ReplicaID("replica-a"),
wantCounter: maxCounter,
wantReplicaID: ReplicaID("replica-a"),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := tc.local.Advance(tc.seen, tc.replica)
if got.Counter() != tc.wantCounter {
t.Fatalf("Advance().Counter() = %d, want %d", got.Counter(), tc.wantCounter)
}
if got.ReplicaID() != tc.wantReplicaID {
t.Fatalf("Advance().ReplicaID() = %q, want %q", got.ReplicaID(), tc.wantReplicaID)
}
if got.Compare(tc.local) <= 0 || got.Compare(tc.seen) <= 0 {
t.Fatalf("Advance() = %#v; want greater than both local %#v and seen %#v", got, tc.local, tc.seen)
}
})
}
}
func TestTimestampAdvancePanicsOnOverflow(t *testing.T) {
maxCounter := ^uint64(0)
tests := []struct {
name string
local Timestamp
seen Timestamp
}{
{
name: "local counter already maximum",
local: NewTimestamp(maxCounter, ReplicaID("replica-a")),
seen: NewTimestamp(1, ReplicaID("replica-b")),
},
{
name: "seen counter already maximum",
local: NewTimestamp(1, ReplicaID("replica-a")),
seen: NewTimestamp(maxCounter, ReplicaID("replica-b")),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
defer func() {
recovered := recover()
if recovered == nil {
t.Fatal("Advance did not panic on counter overflow")
}
if recovered != "yin: timestamp counter overflow" {
t.Fatalf("Advance panic = %v, want %q", recovered, "yin: timestamp counter overflow")
}
}()
_ = tc.local.Advance(tc.seen, ReplicaID("replica-next"))
})
}
}