|
|
@@ -23,6 +23,73 @@ import (
|
|
|
pb "github.com/coreos/etcd/raft/raftpb"
|
|
|
)
|
|
|
|
|
|
+func TestFindConflict(t *testing.T) {
|
|
|
+ previousEnts := []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}
|
|
|
+ tests := []struct {
|
|
|
+ from uint64
|
|
|
+ ents []pb.Entry
|
|
|
+ wconflict uint64
|
|
|
+ }{
|
|
|
+ // no conflict, empty ent
|
|
|
+ {1, []pb.Entry{}, 0},
|
|
|
+ {3, []pb.Entry{}, 0},
|
|
|
+ // no conflict
|
|
|
+ {1, []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}, 0},
|
|
|
+ {2, []pb.Entry{{Term: 2}, {Term: 3}}, 0},
|
|
|
+ {3, []pb.Entry{{Term: 3}}, 0},
|
|
|
+ // no conflict, but has new entries
|
|
|
+ {1, []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}, {Term: 4}, {Term: 4}}, 4},
|
|
|
+ {2, []pb.Entry{{Term: 2}, {Term: 3}, {Term: 4}, {Term: 4}}, 4},
|
|
|
+ {3, []pb.Entry{{Term: 3}, {Term: 4}, {Term: 4}}, 4},
|
|
|
+ {4, []pb.Entry{{Term: 4}, {Term: 4}}, 4},
|
|
|
+ // conflicts with existing entries
|
|
|
+ {1, []pb.Entry{{Term: 4}, {Term: 4}}, 1},
|
|
|
+ {2, []pb.Entry{{Term: 1}, {Term: 4}, {Term: 4}}, 2},
|
|
|
+ {3, []pb.Entry{{Term: 1}, {Term: 2}, {Term: 4}, {Term: 4}}, 3},
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, tt := range tests {
|
|
|
+ raftLog := newLog()
|
|
|
+ raftLog.append(raftLog.lastIndex(), previousEnts...)
|
|
|
+
|
|
|
+ gconflict := raftLog.findConflict(tt.from, tt.ents)
|
|
|
+ if gconflict != tt.wconflict {
|
|
|
+ t.Errorf("#%d: conflict = %d, want %d", i, gconflict, tt.wconflict)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestIsUpToDate(t *testing.T) {
|
|
|
+ previousEnts := []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}
|
|
|
+ raftLog := newLog()
|
|
|
+ raftLog.append(raftLog.lastIndex(), previousEnts...)
|
|
|
+ tests := []struct {
|
|
|
+ lastIndex uint64
|
|
|
+ term uint64
|
|
|
+ wUpToDate bool
|
|
|
+ }{
|
|
|
+ // greater term, ignore lastIndex
|
|
|
+ {raftLog.lastIndex() - 1, 4, true},
|
|
|
+ {raftLog.lastIndex(), 4, true},
|
|
|
+ {raftLog.lastIndex() + 1, 4, true},
|
|
|
+ // smaller term, ignore lastIndex
|
|
|
+ {raftLog.lastIndex() - 1, 2, false},
|
|
|
+ {raftLog.lastIndex(), 2, false},
|
|
|
+ {raftLog.lastIndex() + 1, 2, false},
|
|
|
+ // equal term, lager lastIndex wins
|
|
|
+ {raftLog.lastIndex() - 1, 3, false},
|
|
|
+ {raftLog.lastIndex(), 3, true},
|
|
|
+ {raftLog.lastIndex() + 1, 3, true},
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, tt := range tests {
|
|
|
+ gUpToDate := raftLog.isUpToDate(tt.lastIndex, tt.term)
|
|
|
+ if gUpToDate != tt.wUpToDate {
|
|
|
+ t.Errorf("#%d: uptodate = %v, want %v", i, gUpToDate, tt.wUpToDate)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// TestAppend ensures:
|
|
|
// 1. If an existing entry conflicts with a new one (same index
|
|
|
// but different terms), delete the existing entry and all that
|
|
|
@@ -72,7 +139,7 @@ func TestAppend(t *testing.T) {
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
raftLog := newLog()
|
|
|
- raftLog.ents = append(raftLog.ents, previousEnts...)
|
|
|
+ raftLog.append(raftLog.lastIndex(), previousEnts...)
|
|
|
raftLog.unstable = previousUnstable
|
|
|
index := raftLog.append(tt.after, tt.ents...)
|
|
|
if index != tt.windex {
|
|
|
@@ -152,7 +219,7 @@ func TestUnstableEnts(t *testing.T) {
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
raftLog := newLog()
|
|
|
- raftLog.ents = append(raftLog.ents, previousEnts...)
|
|
|
+ raftLog.append(0, previousEnts...)
|
|
|
raftLog.unstable = tt.unstable
|
|
|
ents := raftLog.unstableEnts()
|
|
|
raftLog.resetUnstable()
|