Skip to content

Commit

Permalink
raft: add TestUnstableStableTo
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Dec 3, 2014
1 parent 9bf3fa6 commit a4746c6
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions log_unstable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit a4746c6

Please sign in to comment.