-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
180 lines (160 loc) · 3.87 KB
/
main_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
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
package paxos_simple
import (
"fmt"
"testing"
"time"
)
// 初始化集群
func makeCluster(proposerNum, acceptorNum, learnNum int) (p []*Proposer, a []*Acceptor, l []*Learner) {
acceptorPort := 4100
learnerPort := 4200
learners := make([]string, learnNum)
for i := 0; i < learnNum; i++ {
learnerPort++
learner := &Learner{localAddr: formatAddr(learnerPort)}
l = append(l, learner)
learners[i] = learner.localAddr
l[i].startRpc()
}
acceptors := make([]string, acceptorNum)
for i := 0; i < acceptorNum; i++ {
acceptorPort++
acceptor := &Acceptor{
localAddr: formatAddr(acceptorPort),
learners: learners,
}
a = append(a, acceptor)
acceptors[i] = acceptor.localAddr
a[i].startRpc()
}
for i := 0; i < proposerNum; i++ {
proposer := &Proposer{
acceptors: acceptors,
id: i + 1,
}
p = append(p, proposer)
}
return
}
func formatAddr(port int) string {
return fmt.Sprintf("127.0.0.1:%d", port)
}
// 清除数据
func clean(p []*Proposer, a []*Acceptor, l []*Learner) {
for _, i := range p {
i.clean()
}
for _, i := range a {
i.clean()
}
for _, i := range l {
i.clean()
}
}
// 关闭端口
func close(p []*Proposer, a []*Acceptor, l []*Learner) {
for _, i := range p {
i.close()
}
for _, i := range a {
i.close()
}
for _, i := range l {
i.close()
}
}
func checkOne(t *testing.T, p []*Proposer, value interface{}) {
for _, i := range p {
if i.consensusValue != value {
t.Fatalf("错误的共识值,期望值:%v,收到值:%v", value, i.consensusValue)
}
}
}
func checkMulti(t *testing.T, p []*Proposer) {
var value interface{}
for _, i := range p {
if i.consensusValue == nil {
t.Fatalf("错误的共识值, 共识值为: nil")
}
if value != nil && value != i.consensusValue {
t.Fatalf("错误的共识值, 前共识值:%v 当前共识值:%v", value, i.consensusValue)
}
if value == nil {
value = i.consensusValue
}
}
if value == nil {
t.Fatalf("无共识值存在")
}
}
func TestBasicPaxos(t *testing.T) {
pNum := 1
aNum := 3
lNum := 2
logPrint("单Proposer测试 proposer :%d, acceptor :%d, learner :%d 开始", pNum, aNum, lNum)
p, a, l := makeCluster(pNum, aNum, lNum)
defer close(p, a, l)
clean(p, a, l)
p[0].propose(100)
time.Sleep(1 * time.Second)
checkOne(t, p, 100)
clean(p, a, l)
logPrint("单Proposer测试 proposer :%d, acceptor :%d, learner :%d 结束", pNum, aNum, lNum)
}
func TestMultiProposer(t *testing.T) {
pNum := 3
aNum := 3
lNum := 2
logPrint("多Proposer测试 proposer :%d, acceptor :%d, learner :%d 开始", pNum, aNum, lNum)
p, a, l := makeCluster(pNum, aNum, lNum)
defer close(p, a, l)
for i, proposer := range p {
proposer.propose(100 + i)
}
time.Sleep(1 * time.Second)
checkMulti(t, p)
clean(p, a, l)
for i, proposer := range p {
proposer.propose(200 + i)
}
time.Sleep(1 * time.Second)
checkMulti(t, p)
clean(p, a, l)
for i, proposer := range p {
proposer.propose(300 + i)
}
time.Sleep(1 * time.Second)
checkMulti(t, p)
clean(p, a, l)
logPrint("多Proposer测试 proposer :%d, acceptor :%d, learner :%d 结束", pNum, aNum, lNum)
}
func TestMultiProposerUnreliable(t *testing.T) {
pNum := 3
aNum := 3
lNum := 2
logPrint("多Proposer+acceptor模拟不可靠测试 proposer :%d, acceptor :%d, learner :%d 开始", pNum, aNum, lNum)
p, a, l := makeCluster(pNum, aNum, lNum)
for _, acceptor := range a {
acceptor.isUnreliable = true
}
defer close(p, a, l)
for i, proposer := range p {
proposer.propose(100 + i)
}
time.Sleep(1 * time.Second)
checkMulti(t, p)
clean(p, a, l)
for i, proposer := range p {
proposer.propose(200 + i)
}
time.Sleep(1 * time.Second)
checkMulti(t, p)
clean(p, a, l)
for i, proposer := range p {
proposer.propose(300 + i)
}
time.Sleep(1 * time.Second)
checkMulti(t, p)
clean(p, a, l)
logPrint("多Proposer+acceptor模拟不可靠测试 proposer :%d, acceptor :%d, learner :%d 结束", pNum, aNum, lNum)
}