|
@@ -132,11 +132,15 @@ func TestAppend(t *testing.T) {
|
|
|
if index != tt.windex {
|
|
if index != tt.windex {
|
|
|
t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
|
|
t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
|
|
|
}
|
|
}
|
|
|
- if g := raftLog.entries(1, noLimit); !reflect.DeepEqual(g, tt.wents) {
|
|
|
|
|
|
|
+ g, err := raftLog.entries(1, noLimit)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("#%d: unexpected error %v", i, err)
|
|
|
|
|
+ }
|
|
|
|
|
+ if !reflect.DeepEqual(g, tt.wents) {
|
|
|
t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
|
|
t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
|
|
|
}
|
|
}
|
|
|
- if g := raftLog.unstable.offset; g != tt.wunstable {
|
|
|
|
|
- t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
|
|
|
|
|
|
|
+ if goff := raftLog.unstable.offset; goff != tt.wunstable {
|
|
|
|
|
+ t.Errorf("#%d: unstable = %d, want %d", i, goff, tt.wunstable)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -257,7 +261,10 @@ func TestLogMaybeAppend(t *testing.T) {
|
|
|
t.Errorf("#%d: committed = %d, want %d", i, gcommit, tt.wcommit)
|
|
t.Errorf("#%d: committed = %d, want %d", i, gcommit, tt.wcommit)
|
|
|
}
|
|
}
|
|
|
if gappend && len(tt.ents) != 0 {
|
|
if gappend && len(tt.ents) != 0 {
|
|
|
- gents := raftLog.slice(raftLog.lastIndex()-uint64(len(tt.ents))+1, raftLog.lastIndex()+1, noLimit)
|
|
|
|
|
|
|
+ gents, err := raftLog.slice(raftLog.lastIndex()-uint64(len(tt.ents))+1, raftLog.lastIndex()+1, noLimit)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("unexpected error %v", err)
|
|
|
|
|
+ }
|
|
|
if !reflect.DeepEqual(tt.ents, gents) {
|
|
if !reflect.DeepEqual(tt.ents, gents) {
|
|
|
t.Errorf("%d: appended entries = %v, want %v", i, gents, tt.ents)
|
|
t.Errorf("%d: appended entries = %v, want %v", i, gents, tt.ents)
|
|
|
}
|
|
}
|
|
@@ -297,8 +304,8 @@ func TestCompactionSideEffects(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for j := offset; j <= raftLog.lastIndex(); j++ {
|
|
for j := offset; j <= raftLog.lastIndex(); j++ {
|
|
|
- if raftLog.term(j) != j {
|
|
|
|
|
- t.Errorf("term(%d) = %d, want %d", j, raftLog.term(j), j)
|
|
|
|
|
|
|
+ if mustTerm(raftLog.term(j)) != j {
|
|
|
|
|
+ t.Errorf("term(%d) = %d, want %d", j, mustTerm(raftLog.term(j)), j)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -322,7 +329,10 @@ func TestCompactionSideEffects(t *testing.T) {
|
|
|
t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
|
|
t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- ents := raftLog.entries(raftLog.lastIndex(), noLimit)
|
|
|
|
|
|
|
+ ents, err := raftLog.entries(raftLog.lastIndex(), noLimit)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("unexpected error %v", err)
|
|
|
|
|
+ }
|
|
|
if len(ents) != 1 {
|
|
if len(ents) != 1 {
|
|
|
t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
|
|
t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
|
|
|
}
|
|
}
|
|
@@ -555,8 +565,8 @@ func TestLogRestore(t *testing.T) {
|
|
|
if raftLog.unstable.offset != index+1 {
|
|
if raftLog.unstable.offset != index+1 {
|
|
|
t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
|
|
t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
|
|
|
}
|
|
}
|
|
|
- if raftLog.term(index) != term {
|
|
|
|
|
- t.Errorf("term = %d, want %d", raftLog.term(index), term)
|
|
|
|
|
|
|
+ if mustTerm(raftLog.term(index)) != term {
|
|
|
|
|
+ t.Errorf("term = %d, want %d", mustTerm(raftLog.term(index)), term)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -572,40 +582,49 @@ func TestIsOutOfBounds(t *testing.T) {
|
|
|
|
|
|
|
|
first := offset + 1
|
|
first := offset + 1
|
|
|
tests := []struct {
|
|
tests := []struct {
|
|
|
- lo, hi uint64
|
|
|
|
|
- wpainc bool
|
|
|
|
|
|
|
+ lo, hi uint64
|
|
|
|
|
+ wpanic bool
|
|
|
|
|
+ wErrCompacted bool
|
|
|
}{
|
|
}{
|
|
|
{
|
|
{
|
|
|
first - 2, first + 1,
|
|
first - 2, first + 1,
|
|
|
|
|
+ false,
|
|
|
true,
|
|
true,
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
first - 1, first + 1,
|
|
first - 1, first + 1,
|
|
|
|
|
+ false,
|
|
|
true,
|
|
true,
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
first, first,
|
|
first, first,
|
|
|
false,
|
|
false,
|
|
|
|
|
+ false,
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
first + num/2, first + num/2,
|
|
first + num/2, first + num/2,
|
|
|
false,
|
|
false,
|
|
|
|
|
+ false,
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
first + num - 1, first + num - 1,
|
|
first + num - 1, first + num - 1,
|
|
|
false,
|
|
false,
|
|
|
|
|
+ false,
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
first + num, first + num,
|
|
first + num, first + num,
|
|
|
false,
|
|
false,
|
|
|
|
|
+ false,
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
first + num, first + num + 1,
|
|
first + num, first + num + 1,
|
|
|
true,
|
|
true,
|
|
|
|
|
+ false,
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
first + num + 1, first + num + 1,
|
|
first + num + 1, first + num + 1,
|
|
|
true,
|
|
true,
|
|
|
|
|
+ false,
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -613,15 +632,21 @@ func TestIsOutOfBounds(t *testing.T) {
|
|
|
func() {
|
|
func() {
|
|
|
defer func() {
|
|
defer func() {
|
|
|
if r := recover(); r != nil {
|
|
if r := recover(); r != nil {
|
|
|
- if !tt.wpainc {
|
|
|
|
|
|
|
+ if !tt.wpanic {
|
|
|
t.Errorf("%d: panic = %v, want %v: %v", i, true, false, r)
|
|
t.Errorf("%d: panic = %v, want %v: %v", i, true, false, r)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}()
|
|
}()
|
|
|
- l.mustCheckOutOfBounds(tt.lo, tt.hi)
|
|
|
|
|
- if tt.wpainc {
|
|
|
|
|
|
|
+ err := l.mustCheckOutOfBounds(tt.lo, tt.hi)
|
|
|
|
|
+ if tt.wpanic {
|
|
|
t.Errorf("%d: panic = %v, want %v", i, false, true)
|
|
t.Errorf("%d: panic = %v, want %v", i, false, true)
|
|
|
}
|
|
}
|
|
|
|
|
+ if tt.wErrCompacted && err != ErrCompacted {
|
|
|
|
|
+ t.Errorf("%d: err = %v, want %v", i, err, ErrCompacted)
|
|
|
|
|
+ }
|
|
|
|
|
+ if !tt.wErrCompacted && err != nil {
|
|
|
|
|
+ t.Errorf("%d: unexpected err %v", i, err)
|
|
|
|
|
+ }
|
|
|
}()
|
|
}()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -650,7 +675,7 @@ func TestTerm(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for j, tt := range tests {
|
|
for j, tt := range tests {
|
|
|
- term := l.term(tt.index)
|
|
|
|
|
|
|
+ term := mustTerm(l.term(tt.index))
|
|
|
if !reflect.DeepEqual(term, tt.w) {
|
|
if !reflect.DeepEqual(term, tt.w) {
|
|
|
t.Errorf("#%d: at = %d, want %d", j, term, tt.w)
|
|
t.Errorf("#%d: at = %d, want %d", j, term, tt.w)
|
|
|
}
|
|
}
|
|
@@ -680,7 +705,7 @@ func TestTermWithUnstableSnapshot(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
for i, tt := range tests {
|
|
|
- term := l.term(tt.index)
|
|
|
|
|
|
|
+ term := mustTerm(l.term(tt.index))
|
|
|
if !reflect.DeepEqual(term, tt.w) {
|
|
if !reflect.DeepEqual(term, tt.w) {
|
|
|
t.Errorf("#%d: at = %d, want %d", i, term, tt.w)
|
|
t.Errorf("#%d: at = %d, want %d", i, term, tt.w)
|
|
|
}
|
|
}
|
|
@@ -714,8 +739,8 @@ func TestSlice(t *testing.T) {
|
|
|
wpanic bool
|
|
wpanic bool
|
|
|
}{
|
|
}{
|
|
|
// test no limit
|
|
// test no limit
|
|
|
- {offset - 1, offset + 1, noLimit, nil, true},
|
|
|
|
|
- {offset, offset + 1, noLimit, nil, true},
|
|
|
|
|
|
|
+ {offset - 1, offset + 1, noLimit, nil, false},
|
|
|
|
|
+ {offset, offset + 1, noLimit, nil, false},
|
|
|
{half - 1, half + 1, noLimit, []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
|
|
{half - 1, half + 1, noLimit, []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
|
|
|
{half, half + 1, noLimit, []pb.Entry{{Index: half, Term: half}}, false},
|
|
{half, half + 1, noLimit, []pb.Entry{{Index: half, Term: half}}, false},
|
|
|
{last - 1, last, noLimit, []pb.Entry{{Index: last - 1, Term: last - 1}}, false},
|
|
{last - 1, last, noLimit, []pb.Entry{{Index: last - 1, Term: last - 1}}, false},
|
|
@@ -739,10 +764,23 @@ func TestSlice(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}()
|
|
}()
|
|
|
- g := l.slice(tt.from, tt.to, tt.limit)
|
|
|
|
|
|
|
+ g, err := l.slice(tt.from, tt.to, tt.limit)
|
|
|
|
|
+ if tt.from <= offset && err != ErrCompacted {
|
|
|
|
|
+ t.Fatalf("#%d: err = %v, want %v", j, err, ErrCompacted)
|
|
|
|
|
+ }
|
|
|
|
|
+ if tt.from > offset && err != nil {
|
|
|
|
|
+ t.Fatalf("#%d: unexpected error %v", j, err)
|
|
|
|
|
+ }
|
|
|
if !reflect.DeepEqual(g, tt.w) {
|
|
if !reflect.DeepEqual(g, tt.w) {
|
|
|
t.Errorf("#%d: from %d to %d = %v, want %v", j, tt.from, tt.to, g, tt.w)
|
|
t.Errorf("#%d: from %d to %d = %v, want %v", j, tt.from, tt.to, g, tt.w)
|
|
|
}
|
|
}
|
|
|
}()
|
|
}()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func mustTerm(term uint64, err error) uint64 {
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ panic(err)
|
|
|
|
|
+ }
|
|
|
|
|
+ return term
|
|
|
|
|
+}
|