diff --git a/raft/raft_test.go b/raft/raft_test.go index e72a6371..2e5d0ca1 100644 --- a/raft/raft_test.go +++ b/raft/raft_test.go @@ -24,6 +24,14 @@ import ( pb "github.com/pingcap-incubator/tinykv/proto/pkg/eraftpb" ) +// returns a new MemoryStorage with only ents filled +func newMemoryStorageWithEnts(ents []pb.Entry) *MemoryStorage { + return &MemoryStorage{ + ents: ents, + snapshot: pb.Snapshot{Metadata: &pb.SnapshotMetadata{ConfState: &pb.ConfState{}}}, + } +} + // nextEnts returns the appliable entries and updates the applied index func nextEnts(r *Raft, s *MemoryStorage) (ents []pb.Entry) { // Transfer all unstable entries to "stable" storage. @@ -372,7 +380,7 @@ func TestDuelingCandidates2AB(t *testing.T) { // 3 will be follower again since both 1 and 2 rejects its vote request since 3 does not have a long enough log nt.send(pb.Message{From: 3, To: 3, MsgType: pb.MessageType_MsgHup}) - wlog := newLog(&MemoryStorage{ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}}}) + wlog := newLog(newMemoryStorageWithEnts([]pb.Entry{{}, {Data: nil, Term: 1, Index: 1}})) wlog.committed = 1 tests := []struct { sm *Raft @@ -429,7 +437,7 @@ func TestCandidateConcede2AB(t *testing.T) { if g := a.Term; g != 1 { t.Errorf("term = %d, want %d", g, 1) } - wlog := newLog(&MemoryStorage{ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}}}) + wlog := newLog(newMemoryStorageWithEnts([]pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}})) wlog.committed = 2 wantLog := ltoa(wlog) for i, p := range tt.peers { @@ -508,7 +516,7 @@ func TestProposal2AB(t *testing.T) { wantLog := newLog(NewMemoryStorage()) if tt.success { - wantLog = newLog(&MemoryStorage{ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}}}) + wantLog = newLog(newMemoryStorageWithEnts([]pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}})) wantLog.committed = 2 } base := ltoa(wantLog) @@ -629,7 +637,7 @@ func TestRecvMessageType_MsgRequestVote2AA(t *testing.T) { sm := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) sm.State = tt.state sm.Vote = tt.voteFor - sm.RaftLog = newLog(&MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 2}, {Index: 2, Term: 2}}}) + sm.RaftLog = newLog(newMemoryStorageWithEnts([]pb.Entry{{}, {Index: 1, Term: 2}, {Index: 2, Term: 2}})) // raft.Term is greater than or equal to raft.RaftLog.lastTerm. In this // test we're only testing MessageType_MsgRequestVote responses when the campaigning node @@ -928,7 +936,7 @@ func TestRecvMessageType_MsgBeat2AA(t *testing.T) { for i, tt := range tests { sm := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage()) - sm.RaftLog = newLog(&MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 0}, {Index: 2, Term: 1}}}) + sm.RaftLog = newLog(newMemoryStorageWithEnts([]pb.Entry{{}, {Index: 1, Term: 0}, {Index: 2, Term: 1}})) sm.Term = 1 sm.State = tt.state sm.Step(pb.Message{From: 1, To: 1, MsgType: pb.MessageType_MsgBeat})