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
|
package quorum
import (
"testing"
"time"
"codeberg.org/snonux/gorum/internal/config"
"codeberg.org/snonux/gorum/internal/vote"
)
var inOneHour = time.Now().Add(1 * time.Hour)
func TestScore(t *testing.T) {
t.Parallel()
conf, _ := config.New(config.WithNodes(
config.Node{Hostname: "foo", Port: 1234, Priority: 3},
config.Node{Hostname: "bar", Port: 4321, Priority: 2},
config.Node{Hostname: "baz", Port: 3444, Priority: 1}))
quo := New(conf)
vote1, _ := vote.New(conf, "foo", "bar")
vote1.FromID = "foo"
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
vote2, _ := vote.New(conf, "bar", "baz")
vote2.FromID = "bar"
vote2.ExpiresAt = inOneHour
quo.vote(vote2)
vote3_dup, _ := vote.New(conf, "bar", "baz")
vote3_dup.FromID = "bar"
vote3_dup.ExpiresAt = inOneHour
quo.vote(vote3_dup)
vote4, _ := vote.New(conf, "foo", "bar", "baz")
vote4.FromID = "baz"
vote4.ExpiresAt = inOneHour
quo.vote(vote4)
scores := quo.scores()
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
t.Log(scores)
if scores[0].ID != "bar" || scores[0].Value != 222 {
t.Errorf("Expected score[0] to be {bar,222}: %v", scores[0])
}
winner, _ := scores.Winner()
if winner.ID != "bar" {
t.Errorf("expectted bar to be the winner, but the winner is %s", winner.ID)
}
}
func TestTieScore(t *testing.T) {
addVotes := func(conf config.Config, quo Quorum) {
vote1, _ := vote.New(conf, "foo", "bar", "baz")
vote1.FromID = "foo"
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
vote2, _ := vote.New(conf, "foo", "bar", "baz")
vote2.FromID = "bar"
vote2.ExpiresAt = inOneHour
quo.vote(vote2)
vote3, _ := vote.New(conf, "foo", "bar", "baz")
vote3.FromID = "baz"
vote3.ExpiresAt = inOneHour
quo.vote(vote3)
}
t.Run("First tie score test", func(t *testing.T) {
// If it is a tie, the particpant with the highest prio (here: "foo") will win.
conf, _ := config.New(config.WithNodes(
config.Node{Hostname: "foo", Port: 1234, Priority: 3},
config.Node{Hostname: "bar", Port: 4321, Priority: 2},
config.Node{Hostname: "baz", Port: 3444, Priority: 1}))
quo := New(conf)
addVotes(conf, quo)
scores := quo.scores()
t.Log("scores", scores)
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
if scores[0].ID != "foo" || scores[0].Value != 333 {
t.Errorf("Expected score[0] to be {foo,333}: %v", scores[0])
}
winner := scores[0].ID
if winner != "foo" {
t.Errorf("Expected the winner to be foo but is: %s", winner)
}
})
t.Run("Second tie score test", func(t *testing.T) {
// If it is a tie, the particpant with the highest prio (here: "bar") will win.
conf, _ := config.New(config.WithNodes(
config.Node{Hostname: "bar", Port: 1234, Priority: 3},
config.Node{Hostname: "foo", Port: 4321, Priority: 2},
config.Node{Hostname: "baz", Port: 3444, Priority: 1}))
quo := New(conf)
addVotes(conf, quo)
scores := quo.scores()
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
if scores[0].ID != "bar" || scores[0].Value != 333 {
t.Errorf("Expected score[0] to be {bar,333}: %v", scores[0])
}
winner := scores[0].ID
if winner != "bar" {
t.Errorf("Expected the winner to be bar but is: %s", winner)
}
})
}
func TestExpire(t *testing.T) {
conf, _ := config.New(config.WithNodes(
config.Node{Hostname: "foo", Port: 1234, Priority: 3},
config.Node{Hostname: "bar", Port: 4321, Priority: 2},
config.Node{Hostname: "bay", Port: 2212, Priority: 1}))
quo := New(conf)
vote1, _ := vote.New(conf, "bar", "baz", "bay")
vote1.FromID = "foo"
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
vote2, _ := vote.New(conf, "foo", "bar", "baz")
vote2.FromID = "bar"
vote2.ExpiresAt = time.Now()
quo.vote(vote2)
if len(quo.votes) != 2 {
t.Errorf("Expected to have two votes before expiry: %v", quo)
}
newVote, _ := quo.expireOldVotes()
if len(quo.votes) != 1 {
t.Errorf("Expected to have one vote after expiry: %v", quo)
}
if len(newVote.IDs) != 1 {
t.Errorf("Expected to have one live node after expiry: %v", quo)
}
if newVote.IDs[0] != "foo" {
t.Errorf("Expected 'foo' to be the live node, but got : %v", newVote.IDs[0])
}
}
func TestLiveNodes(t *testing.T) {
conf, _ := config.New(config.WithNodes(
config.Node{Hostname: "foo", Port: 1234, Priority: 3},
config.Node{Hostname: "bay", Port: 4321, Priority: 1}))
quo := New(conf)
vote1, _ := vote.New(conf, "bar", "baz", "bay")
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
vote2, _ := vote.New(conf, "foo", "bar", "baz")
vote2.ExpiresAt = inOneHour
quo.vote(vote2)
if newVote, changed := quo.makeMyVote(); !changed {
t.Errorf("Expected live node list to be changed: %v", newVote)
}
if newVote, changed := quo.makeMyVote(); changed {
t.Errorf("Expected live node list not to be changed: %v", newVote)
}
}
|