raft: add TestUnstableStableTo

This commit is contained in:
Xiang Li 2014-12-03 13:25:18 -08:00
parent 7703d4942c
commit 37ab463e86

View File

@ -208,3 +208,87 @@ func TestUnstableRestore(t *testing.T) {
t.Errorf("snap = %v, want %v", u.snapshot, &s)
}
}
func TestUnstableStableTo(t *testing.T) {
tests := []struct {
entries []pb.Entry
offset uint64
snap *pb.Snapshot
index, term uint64
woffset uint64
wlen int
}{
{
[]pb.Entry{}, 0, nil,
5, 1,
0, 0,
},
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, nil,
5, 1, // stable to the first entry
6, 0,
},
{
[]pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, nil,
5, 1, // stable to the first entry
6, 1,
},
{
[]pb.Entry{{Index: 6, Term: 2}}, 5, nil,
6, 1, // stable to the first entry and term mismatch
5, 1,
},
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, nil,
4, 1, // stable to old entry
5, 1,
},
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, nil,
4, 2, // stable to old entry
5, 1,
},
// with snapshot
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
5, 1, // stable to the first entry
6, 0,
},
{
[]pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
5, 1, // stable to the first entry
6, 1,
},
{
[]pb.Entry{{Index: 6, Term: 2}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 5, Term: 1}},
6, 1, // stable to the first entry and term mismatch
5, 1,
},
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
4, 1, // stable to snapshot
5, 1,
},
{
[]pb.Entry{{Index: 5, Term: 2}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 2}},
4, 1, // stable to old entry
5, 1,
},
}
for i, tt := range tests {
u := unstable{
entries: tt.entries,
offset: tt.offset,
snapshot: tt.snap,
}
u.stableTo(tt.index, tt.term)
if u.offset != tt.woffset {
t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
}
if len(u.entries) != tt.wlen {
t.Errorf("#%d: len = %d, want %d", i, len(u.entries), tt.wlen)
}
}
}