raft: revise test case and fix typo

This commit is contained in:
Dylan.Wen 2017-02-21 15:23:42 +08:00
parent 2533c2a50c
commit bc6bebe7b0

View File

@ -1859,66 +1859,70 @@ func TestReadOnlyOptionLeaseWithoutCheckQuorum(t *testing.T) {
// TestReadOnlyForNewLeader ensures that a leader only accepts MsgReadIndex message // TestReadOnlyForNewLeader ensures that a leader only accepts MsgReadIndex message
// when it commits at least one log entry at it term. // when it commits at least one log entry at it term.
func TestReadOnlyForNewLeader(t *testing.T) { func TestReadOnlyForNewLeader(t *testing.T) {
cfg := newTestConfig(1, []uint64{1, 2, 3}, 10, 1, nodeConfigs := []struct {
&MemoryStorage{ id uint64
ents: []pb.Entry{{}, {Index: 1, Term: 1}, {Index: 2, Term: 1}}, committed uint64
hardState: pb.HardState{Commit: 1, Term: 1}, applied uint64
}) compact_index uint64
cfg.Applied = 1 }{
a := newRaft(cfg) {1, 1, 1, 0},
cfg = newTestConfig(2, []uint64{1, 2, 3}, 10, 1, {2, 2, 2, 2},
&MemoryStorage{ {3, 2, 2, 2},
ents: []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 1}}, }
hardState: pb.HardState{Commit: 2, Term: 1}, peers := make([]stateMachine, 0)
}) for _, c := range nodeConfigs {
cfg.Applied = 2 storage := NewMemoryStorage()
b := newRaft(cfg) storage.Append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 1}})
cfg = newTestConfig(2, []uint64{1, 2, 3}, 10, 1, storage.SetHardState(pb.HardState{Term: 1, Commit: c.committed})
&MemoryStorage{ if c.compact_index != 0 {
ents: []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 1}}, storage.Compact(c.compact_index)
hardState: pb.HardState{Commit: 2, Term: 1}, }
}) cfg := newTestConfig(c.id, []uint64{1, 2, 3}, 10, 1, storage)
cfg.Applied = 2 cfg.Applied = c.applied
c := newRaft(cfg) raft := newRaft(cfg)
nt := newNetwork(a, b, c) peers = append(peers, raft)
}
nt := newNetwork(peers...)
// Drop MsgApp to forbid peer a to commit any log entry at its term after it becomes leader. // Drop MsgApp to forbid peer a to commit any log entry at its term after it becomes leader.
nt.ignore(pb.MsgApp) nt.ignore(pb.MsgApp)
// Force peer a to become leader. // Force peer a to become leader.
nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup}) nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
if a.state != StateLeader {
t.Fatalf("state = %s, want %s", a.state, StateLeader) sm := nt.peers[1].(*raft)
if sm.state != StateLeader {
t.Fatalf("state = %s, want %s", sm.state, StateLeader)
} }
// Ensure peer a drops read only request. // Ensure peer a drops read only request.
var windex uint64 = 4 var windex uint64 = 4
wctx := []byte("ctx") wctx := []byte("ctx")
nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: wctx}}}) nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: wctx}}})
if len(a.readStates) != 0 { if len(sm.readStates) != 0 {
t.Fatalf("len(readStates) = %d, want zero", len(a.readStates)) t.Fatalf("len(readStates) = %d, want zero", len(sm.readStates))
} }
nt.recover() nt.recover()
// Force peer a to commit a log entry at its term // Force peer a to commit a log entry at its term
for i := 0; i < a.heartbeatTimeout; i++ { for i := 0; i < sm.heartbeatTimeout; i++ {
a.tick() sm.tick()
} }
nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{}}}) nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{}}})
if a.raftLog.committed != 4 { if sm.raftLog.committed != 4 {
t.Fatalf("committed = %d, want 4", a.raftLog.committed) t.Fatalf("committed = %d, want 4", sm.raftLog.committed)
} }
lastLogTerm := a.raftLog.zeroTermOnErrCompacted(a.raftLog.term(a.raftLog.committed)) lastLogTerm := sm.raftLog.zeroTermOnErrCompacted(sm.raftLog.term(sm.raftLog.committed))
if lastLogTerm != a.Term { if lastLogTerm != sm.Term {
t.Fatalf("last log term = %d, want %d", lastLogTerm, a.Term) t.Fatalf("last log term = %d, want %d", lastLogTerm, sm.Term)
} }
// Ensure peer a accepts read only request after it commits a entry at its term. // Ensure peer a accepts read only request after it commits a entry at its term.
nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: wctx}}}) nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: wctx}}})
if len(a.readStates) != 1 { if len(sm.readStates) != 1 {
t.Fatalf("len(readStates) = %d, want 1", len(a.readStates)) t.Fatalf("len(readStates) = %d, want 1", len(sm.readStates))
} }
rs := a.readStates[0] rs := sm.readStates[0]
if rs.Index != windex { if rs.Index != windex {
t.Fatalf("readIndex = %d, want %d", rs.Index, windex) t.Fatalf("readIndex = %d, want %d", rs.Index, windex)
} }