-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhlc_test.go
155 lines (124 loc) · 3.18 KB
/
hlc_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
package hlc
import (
"testing"
)
func TestFromTimestamp(t *testing.T) {
ts := int64(1605806872706744321)
hlc := FromTimestamp(ts)
l :=int64(1605806872706744320)
c :=int64(1)
if l != hlc.ts || c != hlc.counter {
t.Errorf("unexpected result. %d != %d and %d != %d", l, hlc.ts, c, hlc.counter)
}
}
func TestHlc_PaperExample(t *testing.T) {
// Implementation of example from paper (3.3 HLC Algorithm, Figure 6.)
// (see also a scheme testdata/example.png)
// Initialise four nodes j = [0, 1, 2, 3]
// 0 node with Initial state (10, 10, 0), others (0,0,0)
// Initialise a shared physical clock between 1,2,3 nodes.
pt := &FakeClock{0}
//
// TICK: 0
hlc0 := New(&FakeClock{10}) // node 0 (pt: 10, l: 10, c: 0)
hlc1 := New(pt) // node 1 (0, 0, 0)
hlc2 := New(pt) // node 2 (0, 0, 0)
hlc3 := New(pt) // node 3 (0, 0, 0)
//
// TICK: 1 (pt: 1)
pt.Tick()
hlc1.Update(hlc0)
hlc2.Now()
hlc3.Now()
// Check Invariants for TICK 1
// hlc1: (1,10,1)
// hlc2: (1,1,0)
// hlc3: (1,1,0)
compareHlc(t, hlc1, &Hlc{ts: 10, counter: 1})
compareHlc(t, hlc2, &Hlc{ts: 1, counter: 0})
compareHlc(t, hlc3, &Hlc{ts: 1, counter: 0})
//
// TICK: 2 (pt: 2)
pt.Tick()
hlc1.Now()
hlc2.Update(hlc1)
hlc3.Now()
// Check Invariants for TICK 2
// hlc1: (2,10,2)
// hlc2: (2,10,3)
// hlc3: (2,2,0)
compareHlc(t, hlc1, &Hlc{ts: 10, counter: 2})
compareHlc(t, hlc2, &Hlc{ts: 10, counter: 3})
compareHlc(t, hlc3, &Hlc{ts: 2, counter: 0})
//
// TICK: 3 (pt: 3)
pt.Tick()
hlc1.Now()
hlc2.Now()
hlc3.Update(hlc2)
// Check Invariants for TICK 3
// hlc1: (3, 10, 3) paper has an error (3, 13)
// hlc2: (3, 10, 4)
// hlc3: (3, 10, 5)
compareHlc(t, hlc1, &Hlc{ts: 10, counter: 3})
compareHlc(t, hlc2, &Hlc{ts: 10, counter: 4})
compareHlc(t, hlc3, &Hlc{ts: 10, counter: 5})
//
// TICK: 4 (pt: 4)
pt.Tick()
hlc3.Now()
hlc2.Now()
hlc1.Update(hlc3)
// Check Invariants for TICK 4
// hlc1: (4, 10, 7)
// hlc2: (4, 10, 5)
// hlc3: (4, 10, 6)
compareHlc(t, hlc1, &Hlc{ts: 10, counter: 7})
compareHlc(t, hlc2, &Hlc{ts: 10, counter: 5})
compareHlc(t, hlc3, &Hlc{ts: 10, counter: 6})
}
func TestHlc_LocalEvents(t *testing.T) {
pt := &FakeClock{10}
hlc0 := New(pt) // node 0 (pt: 10, l: 10, c: 0)
// TICK: 11
pt.Tick()
hlc0.Now()
compareHlc(t, hlc0, &Hlc{ts: 11, counter: 0})
// TICK: 12
pt.Tick()
hlc0.Now()
compareHlc(t, hlc0, &Hlc{ts: 12, counter: 0})
// TICK: 13
pt.Tick()
hlc0.Now()
compareHlc(t, hlc0, &Hlc{ts: 13, counter: 0})
}
func TestHlc_Timestamp(t *testing.T) {
lc := New(&FakeClock{ts: 1605806872706798000})
ts := lc.Now()
expected := int64(1605806872706744321)
if ts != expected {
t.Errorf("timestamp was wrong. Expected %d, but got %d", expected, ts)
}
}
func compareHlc(t *testing.T, expected, actual *Hlc) {
if actual.ts != expected.ts || actual.counter != expected.counter {
t.Fatalf("expected %v, but got %v", expected, actual)
}
}
func TestHlc_Max(t *testing.T) {
cases := []struct {
Expected int64
Result int64
} {
{0, max()},
{1, max(1)},
{2, max(2, 1)},
{4, max(2, 1, 3, 4, 1)},
}
for _, c := range cases{
if c.Result != c.Expected {
t.Errorf("expected %d, but got %d", c.Expected, c.Result)
}
}
}