Skip to content

Commit

Permalink
tests: init raftLog using the newLog function
Browse files Browse the repository at this point in the history
  • Loading branch information
pav-kv committed Feb 1, 2024
1 parent f6b5617 commit aea363d
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,10 @@ func TestCandidateConcede(t *testing.T) {
if g := a.Term; g != 1 {
t.Errorf("term = %d, want %d", g, 1)
}
wantLog := ltoa(&raftLog{
storage: &MemoryStorage{
ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}},
},
unstable: unstable{offset: 3},
committed: 2,
})

wantLog := ltoa(newLog(&MemoryStorage{
ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}},
}, nil))
for i, p := range tt.peers {
if sm, ok := p.(*raft); ok {
l := ltoa(sm.raftLog)
Expand Down Expand Up @@ -1052,17 +1049,13 @@ func TestOldMessages(t *testing.T) {
// commit a new entry
tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("somedata")}}})

ilog := &raftLog{
storage: &MemoryStorage{
ents: []pb.Entry{
{}, {Data: nil, Term: 1, Index: 1},
{Data: nil, Term: 2, Index: 2}, {Data: nil, Term: 3, Index: 3},
{Data: []byte("somedata"), Term: 3, Index: 4},
},
ilog := newLog(&MemoryStorage{
ents: []pb.Entry{
{}, {Data: nil, Term: 1, Index: 1},
{Data: nil, Term: 2, Index: 2}, {Data: nil, Term: 3, Index: 3},
{Data: []byte("somedata"), Term: 3, Index: 4},
},
unstable: unstable{offset: 5},
committed: 4,
}
}, nil)
base := ltoa(ilog)
for i, p := range tt.peers {
if sm, ok := p.(*raft); ok {
Expand Down Expand Up @@ -1113,12 +1106,9 @@ func TestProposal(t *testing.T) {

wantLog := newLog(NewMemoryStorage(), raftLogger)
if tt.success {
wantLog = &raftLog{
storage: &MemoryStorage{
ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}},
},
unstable: unstable{offset: 3},
}
wantLog = newLog(&MemoryStorage{
ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}},
}, nil)
}
base := ltoa(wantLog)
for i, p := range tt.peers {
Expand Down Expand Up @@ -1151,12 +1141,9 @@ func TestProposalByProxy(t *testing.T) {
// propose via follower
tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("somedata")}}})

wantLog := &raftLog{
storage: &MemoryStorage{
ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Data: data, Index: 2}},
},
unstable: unstable{offset: 3},
committed: 2}
wantLog := newLog(&MemoryStorage{
ents: []pb.Entry{{}, {Data: nil, Term: 1, Index: 1}, {Term: 1, Data: data, Index: 2}},
}, nil)
base := ltoa(wantLog)
for i, p := range tt.peers {
if sm, ok := p.(*raft); ok {
Expand Down Expand Up @@ -1577,10 +1564,9 @@ func testRecvMsgVote(t *testing.T, msgType pb.MessageType) {
sm.step = stepLeader
}
sm.Vote = tt.voteFor
sm.raftLog = &raftLog{
storage: &MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 2}, {Index: 2, Term: 2}}},
unstable: unstable{offset: 3},
}
sm.raftLog = newLog(&MemoryStorage{
ents: []pb.Entry{{}, {Index: 1, Term: 2}, {Index: 2, Term: 2}},
}, nil)

// raft.Term is greater than or equal to raft.raftLog.lastTerm. In this
// test we're only testing MsgVote responses when the campaigning node
Expand Down Expand Up @@ -2617,10 +2603,9 @@ func TestLeaderAppResp(t *testing.T) {
// sm term is 1 after it becomes the leader.
// thus the last log term must be 1 to be committed.
sm := newTestRaft(1, 10, 1, newTestMemoryStorage(withPeers(1, 2, 3)))
sm.raftLog = &raftLog{
storage: &MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 1}, {Index: 2, Term: 1}}},
unstable: unstable{offset: 3},
}
sm.raftLog = newLog(&MemoryStorage{
ents: []pb.Entry{{}, {Index: 1, Term: 1}, {Index: 2, Term: 1}},
}, nil)
sm.becomeCandidate()
sm.becomeLeader()
sm.readMessages()
Expand Down Expand Up @@ -2732,7 +2717,9 @@ func TestRecvMsgBeat(t *testing.T) {

for i, tt := range tests {
sm := newTestRaft(1, 10, 1, newTestMemoryStorage(withPeers(1, 2, 3)))
sm.raftLog = &raftLog{storage: &MemoryStorage{ents: []pb.Entry{{}, {Index: 1, Term: 1}, {Index: 2, Term: 1}}}}
sm.raftLog = newLog(&MemoryStorage{
ents: []pb.Entry{{}, {Index: 1, Term: 1}, {Index: 2, Term: 1}},
}, nil)
sm.Term = 1
sm.state = tt.state
switch tt.state {
Expand Down

0 comments on commit aea363d

Please sign in to comment.