-
Notifications
You must be signed in to change notification settings - Fork 266
/
acceptor_test.go
91 lines (70 loc) · 2.02 KB
/
acceptor_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
package consensus
import (
"github.com/bmizerany/assert"
_ "github.com/ha/doozerd/quiet"
"testing"
)
func TestIgnoreOldMessages(t *testing.T) {
tests := [][]*msg{
{newInviteSeqn1(11), newNominateSeqn1(1, "v")},
{newNominateSeqn1(11, "v"), newInviteSeqn1(1)},
{newInviteSeqn1(11), newInviteSeqn1(1)},
{newNominateSeqn1(11, "v"), newNominateSeqn1(1, "v")},
}
for _, test := range tests {
ac := acceptor{}
ac.update(test[0])
got := ac.update(test[1])
assert.Equal(t, (*msg)(nil), got)
}
}
func TestAcceptsInvite(t *testing.T) {
ac := acceptor{}
got := ac.update(newInviteSeqn1(1))
assert.Equal(t, newRsvp(1, 0, ""), got)
}
func TestItVotes(t *testing.T) {
totest := [][]*msg{
{newNominateSeqn1(1, "foo"), newVote(1, "foo")},
{newNominateSeqn1(1, "bar"), newVote(1, "bar")},
}
for _, test := range totest {
ac := acceptor{}
got := ac.update(test[0])
assert.Equal(t, test[1], got, test)
}
}
func TestItVotesWithAnotherRound(t *testing.T) {
ac := acceptor{}
val := "bar"
// According to paxos, we can omit Phase 1 in the first round
got := ac.update(newNominateSeqn1(2, val))
assert.Equal(t, newVote(2, val), got)
}
func TestItVotesWithAnotherSelf(t *testing.T) {
ac := acceptor{}
val := "bar"
// According to paxos, we can omit Phase 1 in the first round
got := ac.update(newNominateSeqn1(2, val))
assert.Equal(t, newVote(2, val), got)
}
func TestVotedRoundsAndValuesAreTracked(t *testing.T) {
ac := acceptor{}
ac.update(newNominateSeqn1(1, "v"))
got := ac.update(newInviteSeqn1(2))
assert.Equal(t, newRsvp(2, 1, "v"), got)
}
func TestVotesOnlyOncePerRound(t *testing.T) {
ac := acceptor{}
got := ac.update(newNominateSeqn1(1, "v"))
assert.Equal(t, newVote(1, "v"), got)
got = ac.update(newNominateSeqn1(1, "v"))
assert.Equal(t, (*msg)(nil), got)
}
func TestAcceptorIgnoresBadMessages(t *testing.T) {
ac := acceptor{}
got := ac.update(&msg{Cmd: invite}) // missing Crnd
assert.Equal(t, (*msg)(nil), got)
got = ac.update(&msg{Cmd: nominate}) // missing Crnd
assert.Equal(t, (*msg)(nil), got)
}