-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaxos_test.go
103 lines (93 loc) · 2.49 KB
/
paxos_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
package paxos
import "testing"
func TestBroadcast(t *testing.T) {
acceptors := []chan message{make(chan message, 100), make(chan message, 100)}
msg := message{}
broadcast(acceptors, msg)
for _, acceptor := range acceptors {
if <-acceptor != msg {
t.Errorf("Received message was different to sent message!")
}
}
}
func TestSingleProposer(t *testing.T) {
n := NewNetwork(1, 1, 2, []int{1})
go n.acceptors[0].Run()
go n.proposers[0].Run()
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}
func TestTwoProposersSameValue(t *testing.T) {
n := NewNetwork(2, 1, 2, []int{2, 2})
go n.acceptors[0].Run()
go n.proposers[0].Run()
go n.proposers[1].Run()
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}
func TestTwoProposersDifferentValue(t *testing.T) {
n := NewNetwork(2, 1, 2, []int{1, 2})
go n.acceptors[0].Run()
go n.proposers[0].Run()
go n.proposers[1].Run()
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}
func TestManyProposersDifferentValues(t *testing.T) {
n := NewNetwork(5, 1, 2, []int{1, 2, 3, 4, 5})
go n.acceptors[0].Run()
for _, p := range n.proposers {
go p.Run()
}
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}
func TestTwoAcceptors(t *testing.T) {
n := NewNetwork(1, 2, 2, []int{3})
go n.acceptors[0].Run()
go n.acceptors[1].Run()
go n.proposers[0].Run()
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}
func TestManyProposersManyAcceptorsSameValue(t *testing.T) {
n := NewNetwork(5, 5, 2, []int{1, 1, 1, 1, 1})
for _, a := range n.acceptors {
go a.Run()
}
for _, p := range n.proposers {
go p.Run()
}
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}
func TestManyProposersManyAcceptorsDifferentValues(t *testing.T) {
n := NewNetwork(5, 5, 2, []int{1, 2, 3, 4, 5})
for _, a := range n.acceptors {
go a.Run()
}
for _, p := range n.proposers {
go p.Run()
}
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}
func TestManyProposersManyAcceptorsSemiSameValues(t *testing.T) {
n := NewNetwork(5, 5, 2, []int{1, 2, 1, 2, 1})
for _, a := range n.acceptors {
go a.Run()
}
for _, p := range n.proposers {
go p.Run()
}
if n.learners[0].Run() != n.learners[1].Run() {
t.Errorf("Did not receive the same value!")
}
}