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
|
package internal
import (
"testing"
"time"
)
func TestScore(t *testing.T) {
quorum := make(quorumMap)
config := config{Participants: []string{"foo:1234", "bar:4321", "baz:3444"}}
vote1 := newVote(config, "foo:334234", "foo bar\n")
vote1.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote1)
vote2 := newVote(config, "bar:334234", "bar baz\n")
vote2.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote2)
vote3_dup := newVote(config, "bar:33234", "bar baz\n")
vote3_dup.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote3_dup)
vote4 := newVote(config, "baz:334234", "foo bar baz\n")
vote4.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote4)
scores := quorum.score(config)
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
if scores[0].id != "bar" || scores[0].score != 3 {
t.Errorf("Expected score[0] to be {bar,3}: %v", scores[0])
}
}
func TestTieScore(t *testing.T) {
addVotes := func(config config, quorum quorumMap) {
vote1 := newVote(config, "foo:334234", "foo bar baz\n")
vote1.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote1)
vote2 := newVote(config, "bar:334234", "foo bar baz\n")
vote2.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote2)
vote3 := newVote(config, "baz:334234", "foo bar baz\n")
vote3.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote3)
}
t.Run("First tie score test", func(t *testing.T) {
quorum := make(quorumMap)
// If it is a tie, the first particpant (here: "foo") will win.
config := config{Participants: []string{"foo:1234", "bar:4321", "baz:3444"}}
addVotes(config, quorum)
scores := quorum.score(config)
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
if scores[0].id != "foo" || scores[0].score != 3 {
t.Errorf("Expected score[0] to be {foo,3}: %v", scores[0])
}
winner, _ := quorum.winner(config)
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) {
quorum := make(quorumMap)
// If it is a tie, the first particpant (here: "bar") will win.
config := config{Participants: []string{"bar:1234", "foo:4321", "baz:3444"}}
addVotes(config, quorum)
scores := quorum.score(config)
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
if scores[0].id != "bar" || scores[0].score != 3 {
t.Errorf("Expected score[0] to be {bar,3}: %v", scores[0])
}
winner, _ := quorum.winner(config)
if winner != "bar" {
t.Errorf("Expected the winner to be bar but is: %s", winner)
}
})
}
func TestCleanExpired(t *testing.T) {
quorum := make(quorumMap)
config := config{Participants: []string{"foo:1234", "bay:4321"}}
vote1 := newVote(config, "foo:334234", " foo bar baz bay\n")
vote1.expiresAt = time.Now().Add(1 * time.Hour)
quorum.vote(vote1)
vote2 := newVote(config, "bar:334234", " foo bar baz bay\n")
vote2.expiresAt = time.Now()
quorum.vote(vote2)
if len(quorum) != 2 {
t.Errorf("Expected to have two votes before expiry: %v", quorum)
}
quorum.cleanExpired()
if len(quorum) != 1 {
t.Errorf("Expected to have only one vote after expiry: %v", quorum)
}
}
|