raft: ignore old messages

This commit is contained in:
Blake Mizerany 2014-05-16 10:53:56 -07:00 committed by Yicheng Qin
parent 895d80d0e1
commit b06c0cc3ec

View File

@ -51,11 +51,23 @@ func TestDualingCandidates(t *testing.T) {
t.Log("healing")
tt.heal()
tt.step(Message{To: 2, Type: msgHup})
if c.state != stateLeader {
t.Errorf("state = %s, want %s", c.state, stateLeader)
tests := []struct {
sm *stateMachine
state stateType
term int
}{
{a, stateFollower, 2},
{c, stateLeader, 2},
}
if g := c.term; g != 2 {
t.Errorf("term = %d, want %d", g, 2)
for i, tt := range tests {
if g := tt.sm.state; g != tt.state {
t.Errorf("#%d: state = %s, want %s", i, g, tt.state)
}
if g := tt.sm.term; g != tt.term {
t.Errorf("#%d: term = %d, want %d", i, g, tt.term)
}
}
if g := diffLogs(tt.logs(defaultLog)); g != nil {
for _, diff := range g {
@ -64,6 +76,21 @@ func TestDualingCandidates(t *testing.T) {
}
}
func TestOldMessages(t *testing.T) {
tt := newNetwork(nil, nil, nil)
// make 0 leader @ term 3
tt.step(Message{To: 0, Type: msgHup})
tt.step(Message{To: 0, Type: msgHup})
tt.step(Message{To: 0, Type: msgHup})
// pretend we're an old leader trying to make progress
tt.step(Message{To: 0, Type: msgApp, Term: 1, Entries: []Entry{{Term: 1}}})
if g := diffLogs(tt.logs(defaultLog)); g != nil {
for _, diff := range g {
t.Errorf("bag log:\n%s", diff)
}
}
}
func TestProposal(t *testing.T) {
data := []byte("somedata")
successLog := []Entry{{}, {Term: 1, Data: data}}