-
Notifications
You must be signed in to change notification settings - Fork 57
/
paxos_test.go
59 lines (47 loc) · 1.27 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
package paxos
import (
"testing"
"time"
)
func TestPaxosWithSingleProposer(t *testing.T) {
// 1, 2, 3 are acceptors
// 1001 is a proposer
pn := newPaxosNetwork(1, 2, 3, 1001, 2001)
as := make([]*acceptor, 0)
for i := 1; i <= 3; i++ {
as = append(as, newAcceptor(i, pn.agentNetwork(i), 2001))
}
for _, a := range as {
go a.run()
}
p := newProposer(1001, "hello world", pn.agentNetwork(1001), 1, 2, 3)
go p.run()
l := newLearner(2001, pn.agentNetwork(2001), 1, 2, 3)
value := l.learn()
if value != "hello world" {
t.Errorf("value = %s, want %s", value, "hello world")
}
}
func TestPaxosWithTwoProposers(t *testing.T) {
// 1, 2, 3 are acceptors
// 1001,1002 is a proposer
pn := newPaxosNetwork(1, 2, 3, 1001, 1002, 2001)
as := make([]*acceptor, 0)
for i := 1; i <= 3; i++ {
as = append(as, newAcceptor(i, pn.agentNetwork(i), 2001))
}
for _, a := range as {
go a.run()
}
p1 := newProposer(1001, "hello world", pn.agentNetwork(1001), 1, 2, 3)
go p1.run()
time.Sleep(time.Millisecond)
p2 := newProposer(1002, "bad day", pn.agentNetwork(1002), 1, 2, 3)
go p2.run()
l := newLearner(2001, pn.agentNetwork(2001), 1, 2, 3)
value := l.learn()
if value != "hello world" {
t.Errorf("value = %s, want %s", value, "hello world")
}
time.Sleep(time.Millisecond)
}