mirror of
https://github.com/talent-plan/tinykv.git
synced 2024-12-26 12:50:11 +08:00
Add a helper function (#242)
Add newMemoryStorageWithEnts to provide valid MemoryStorage
This commit is contained in:
parent
951f0b3baf
commit
eb173f0df1
@ -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})
|
||||
|
Loading…
Reference in New Issue
Block a user