Skip to content

Commit

Permalink
raft: add TestUnstableTruncateAndAppend
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Dec 4, 2014
1 parent ae75482 commit 6472984
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions log_unstable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,59 @@ func TestUnstableStableTo(t *testing.T) {
}
}
}

func TestUnstableTruncateAndAppend(t *testing.T) {
tests := []struct {
entries []pb.Entry
offset uint64
snap *pb.Snapshot
toappend []pb.Entry

woffset uint64
wentries []pb.Entry
}{
// append to the end
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, nil,
[]pb.Entry{{Index: 6, Term: 1}, {Index: 7, Term: 1}},
5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}},
},
// replace the unstable entries
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, nil,
[]pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
5, []pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
},
{
[]pb.Entry{{Index: 5, Term: 1}}, 5, nil,
[]pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
4, []pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
},
// truncate the existing entries and append
{
[]pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
[]pb.Entry{{Index: 6, Term: 2}},
5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 2}},
},
{
[]pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
[]pb.Entry{{Index: 7, Term: 2}, {Index: 8, Term: 2}},
5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 2}, {Index: 8, Term: 2}},
},
}

for i, tt := range tests {
u := unstable{
entries: tt.entries,
offset: tt.offset,
snapshot: tt.snap,
}
u.truncateAndAppend(tt.toappend)
if u.offset != tt.woffset {
t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
}
if !reflect.DeepEqual(u.entries, tt.wentries) {
t.Errorf("#%d: entries = %v, want %v", i, u.entries, tt.wentries)
}
}
}

0 comments on commit 6472984

Please sign in to comment.