Skip to content

Commit 37cc033

Browse files
committed
raft: generate MsgApp in TestHandleMsgApp
Epic: none Release note: none
1 parent a5d7c25 commit 37cc033

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

pkg/raft/log_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -999,27 +999,30 @@ func (i index) terms(terms ...uint64) []pb.Entry {
999999
return entries
10001000
}
10011001

1002-
// append generates a valid LeadSlice of entries appended after the given entry
1003-
// ID, at indices [id.index+1, id.index+len(terms)], with the given terms of
1004-
// each entry. Terms must be >= id.term, and non-decreasing.
1005-
func (id entryID) append(terms ...uint64) LeadSlice {
1006-
term := id.term
1007-
if ln := len(terms); ln != 0 {
1008-
term = terms[ln-1]
1009-
}
1010-
ls := LeadSlice{
1011-
term: term,
1012-
LogSlice: LogSlice{
1013-
prev: id,
1014-
entries: index(id.index + 1).terms(terms...),
1015-
},
1002+
// terms generates a LogSlice of entries with the given terms appended after the
1003+
// given entry ID.
1004+
func (id entryID) terms(terms ...uint64) LogSlice {
1005+
ls := LogSlice{
1006+
prev: id,
1007+
entries: index(id.index + 1).terms(terms...),
10161008
}
10171009
if err := ls.valid(); err != nil {
10181010
panic(err)
10191011
}
10201012
return ls
10211013
}
10221014

1015+
// append generates a valid LeadSlice of entries appended after the given entry
1016+
// ID, at indices [id.index+1, id.index+len(terms)], with the given terms of
1017+
// each entry. Terms must be >= id.term, and non-decreasing.
1018+
func (id entryID) append(terms ...uint64) LeadSlice {
1019+
ls := id.terms(terms...)
1020+
return LeadSlice{
1021+
term: ls.lastEntryID().term,
1022+
LogSlice: ls,
1023+
}
1024+
}
1025+
10231026
// intRange returns a slice containing integers in [from, to) interval.
10241027
func intRange[T constraints.Integer](from, to T) []T {
10251028
slice := make([]T, to-from)

pkg/raft/raft_test.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,42 +1375,52 @@ func TestStepIgnoreOldTermMsg(t *testing.T) {
13751375
// delete the existing entry and all that follow it; append any new entries not already in the log.
13761376
// 3. If leaderCommit > commitIndex, set commitIndex = min(leaderCommit, index of last new entry).
13771377
func TestHandleMsgApp(t *testing.T) {
1378-
tests := []struct {
1378+
const term = 2
1379+
init := index(1).terms(1, 2) // the initial log
1380+
1381+
msgApp := func(term uint64, ls LogSlice, commit uint64) pb.Message {
1382+
return pb.Message{
1383+
From: 2, To: 1, Type: pb.MsgApp, Term: term,
1384+
Index: ls.prev.index, LogTerm: ls.prev.term, Entries: ls.Entries(),
1385+
Commit: commit,
1386+
}
1387+
}
1388+
for _, tt := range []struct {
13791389
m pb.Message
13801390
wIndex uint64
13811391
wCommit uint64
13821392
wReject bool
13831393
}{
13841394
// Ensure 1
1385-
{pb.Message{Type: pb.MsgApp, Term: 3, LogTerm: 3, Index: 2, Commit: 3}, 2, 0, true}, // previous log mismatch
1386-
{pb.Message{Type: pb.MsgApp, Term: 3, LogTerm: 3, Index: 3, Commit: 3}, 2, 0, true}, // previous log non-exist
1395+
{m: msgApp(3, entryID{index: 2, term: 3}.terms(), 3), wIndex: 2, wReject: true}, // previous log mismatch
1396+
{m: msgApp(3, entryID{index: 3, term: 3}.terms(), 3), wIndex: 2, wReject: true}, // previous log non-exist
13871397

13881398
// Ensure 2
1389-
{pb.Message{Type: pb.MsgApp, Term: 2, LogTerm: 1, Index: 1, Commit: 1}, 2, 1, false},
1390-
{pb.Message{Type: pb.MsgApp, Term: 3, LogTerm: 0, Index: 0, Commit: 1, Entries: []pb.Entry{{Index: 1, Term: 3}}}, 1, 1, false},
1391-
{pb.Message{Type: pb.MsgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 3, Entries: []pb.Entry{{Index: 3, Term: 2}, {Index: 4, Term: 2}}}, 4, 3, false},
1392-
{pb.Message{Type: pb.MsgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 4, Entries: []pb.Entry{{Index: 3, Term: 2}}}, 3, 3, false},
1393-
{pb.Message{Type: pb.MsgApp, Term: 2, LogTerm: 1, Index: 1, Commit: 4, Entries: []pb.Entry{{Index: 2, Term: 2}}}, 2, 2, false},
1399+
{m: msgApp(2, entryID{index: 1, term: 1}.terms(), 1), wIndex: 2, wCommit: 1},
1400+
{m: msgApp(3, entryID{}.terms(3), 1), wIndex: 1, wCommit: 1},
1401+
{m: msgApp(2, entryID{index: 2, term: 2}.terms(2, 2), 3), wIndex: 4, wCommit: 3},
1402+
{m: msgApp(2, entryID{index: 2, term: 2}.terms(2), 3), wIndex: 3, wCommit: 3},
1403+
{m: msgApp(2, entryID{index: 1, term: 1}.terms(2), 4), wIndex: 2, wCommit: 2},
13941404

13951405
// Ensure 3
1396-
{pb.Message{Type: pb.MsgApp, Term: 1, LogTerm: 1, Index: 1, Commit: 3}, 2, 1, false}, // match entry 1, commit up to last new entry 1
1397-
{pb.Message{Type: pb.MsgApp, Term: 2, LogTerm: 1, Index: 1, Commit: 3, Entries: []pb.Entry{{Index: 2, Term: 2}}}, 2, 2, false}, // match entry 1, commit up to last new entry 2
1398-
{pb.Message{Type: pb.MsgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 3}, 2, 2, false}, // match entry 2, commit up to last new entry 2
1399-
{pb.Message{Type: pb.MsgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 4}, 2, 2, false}, // commit up to log.last()
1400-
}
1401-
1402-
for i, tt := range tests {
1403-
storage := newTestMemoryStorage(withPeers(1))
1404-
require.NoError(t, storage.Append(index(1).terms(1, 2)))
1405-
sm := newTestRaft(1, 10, 1, storage)
1406-
sm.becomeFollower(2, None)
1407-
1408-
sm.handleAppendEntries(tt.m)
1409-
assert.Equal(t, tt.wIndex, sm.raftLog.lastIndex(), "#%d", i)
1410-
assert.Equal(t, tt.wCommit, sm.raftLog.committed, "#%d", i)
1411-
m := sm.readMessages()
1412-
require.Len(t, m, 1, "#%d", i)
1413-
assert.Equal(t, tt.wReject, m[0].Reject, "#%d", i)
1406+
{m: msgApp(1, entryID{index: 1, term: 1}.terms(), 3), wIndex: 2, wCommit: 1}, // match entry 1, commit up to last new entry 1
1407+
{m: msgApp(2, entryID{index: 1, term: 1}.terms(2), 3), wIndex: 2, wCommit: 2}, // match entry 1, commit up to last new entry 2
1408+
{m: msgApp(2, entryID{index: 2, term: 2}.terms(), 3), wIndex: 2, wCommit: 2}, // match entry 2, commit up to last new entry 2
1409+
{m: msgApp(2, entryID{index: 2, term: 2}.terms(), 4), wIndex: 2, wCommit: 2}, // commit up to log.last()
1410+
} {
1411+
t.Run("", func(t *testing.T) {
1412+
storage := newTestMemoryStorage(withPeers(1, 2))
1413+
require.NoError(t, storage.Append(init))
1414+
sm := newTestRaft(1, 10, 1, storage)
1415+
sm.becomeFollower(term, None)
1416+
1417+
sm.handleAppendEntries(tt.m)
1418+
assert.Equal(t, tt.wIndex, sm.raftLog.lastIndex())
1419+
assert.Equal(t, tt.wCommit, sm.raftLog.committed)
1420+
m := sm.readMessages()
1421+
require.Len(t, m, 1)
1422+
assert.Equal(t, tt.wReject, m[0].Reject)
1423+
})
14141424
}
14151425
}
14161426

0 commit comments

Comments
 (0)