Skip to content

Commit

Permalink
tests: use entry slice helper in storage_test
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kalinnikov <pavel@cockroachlabs.com>
  • Loading branch information
pav-kv committed Feb 3, 2024
1 parent 0bbd6b8 commit 856c869
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestStorageTerm(t *testing.T) {
ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
ents := index(3).terms(3, 4, 5)
tests := []struct {
i uint64

Expand Down Expand Up @@ -56,7 +56,7 @@ func TestStorageTerm(t *testing.T) {
}

func TestStorageEntries(t *testing.T) {
ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 6}}
ents := index(3).terms(3, 4, 5, 6)
tests := []struct {
lo, hi, maxsize uint64

Expand All @@ -65,18 +65,18 @@ func TestStorageEntries(t *testing.T) {
}{
{2, 6, math.MaxUint64, ErrCompacted, nil},
{3, 4, math.MaxUint64, ErrCompacted, nil},
{4, 5, math.MaxUint64, nil, []pb.Entry{{Index: 4, Term: 4}}},
{4, 6, math.MaxUint64, nil, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
{4, 7, math.MaxUint64, nil, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 6}}},
{4, 5, math.MaxUint64, nil, index(4).terms(4)},
{4, 6, math.MaxUint64, nil, index(4).terms(4, 5)},
{4, 7, math.MaxUint64, nil, index(4).terms(4, 5, 6)},
// even if maxsize is zero, the first entry should be returned
{4, 7, 0, nil, []pb.Entry{{Index: 4, Term: 4}}},
{4, 7, 0, nil, index(4).terms(4)},
// limit to 2
{4, 7, uint64(ents[1].Size() + ents[2].Size()), nil, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
{4, 7, uint64(ents[1].Size() + ents[2].Size()), nil, index(4).terms(4, 5)},
// limit to 2
{4, 7, uint64(ents[1].Size() + ents[2].Size() + ents[3].Size()/2), nil, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
{4, 7, uint64(ents[1].Size() + ents[2].Size() + ents[3].Size() - 1), nil, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
{4, 7, uint64(ents[1].Size() + ents[2].Size() + ents[3].Size()/2), nil, index(4).terms(4, 5)},
{4, 7, uint64(ents[1].Size() + ents[2].Size() + ents[3].Size() - 1), nil, index(4).terms(4, 5)},
// all
{4, 7, uint64(ents[1].Size() + ents[2].Size() + ents[3].Size()), nil, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 6}}},
{4, 7, uint64(ents[1].Size() + ents[2].Size() + ents[3].Size()), nil, index(4).terms(4, 5, 6)},
}

for _, tt := range tests {
Expand All @@ -90,21 +90,21 @@ func TestStorageEntries(t *testing.T) {
}

func TestStorageLastIndex(t *testing.T) {
ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
ents := index(3).terms(3, 4, 5)
s := &MemoryStorage{ents: ents}

last, err := s.LastIndex()
require.NoError(t, err)
require.Equal(t, uint64(5), last)

require.NoError(t, s.Append([]pb.Entry{{Index: 6, Term: 5}}))
require.NoError(t, s.Append(index(6).terms(5)))
last, err = s.LastIndex()
require.NoError(t, err)
require.Equal(t, uint64(6), last)
}

func TestStorageFirstIndex(t *testing.T) {
ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
ents := index(3).terms(3, 4, 5)
s := &MemoryStorage{ents: ents}

first, err := s.FirstIndex()
Expand All @@ -118,7 +118,7 @@ func TestStorageFirstIndex(t *testing.T) {
}

func TestStorageCompact(t *testing.T) {
ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
ents := index(3).terms(3, 4, 5)
tests := []struct {
i uint64

Expand All @@ -145,7 +145,7 @@ func TestStorageCompact(t *testing.T) {
}

func TestStorageCreateSnapshot(t *testing.T) {
ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
ents := index(3).terms(3, 4, 5)
cs := &pb.ConfState{Voters: []uint64{1, 2, 3}}
data := []byte("data")

Expand All @@ -170,50 +170,50 @@ func TestStorageCreateSnapshot(t *testing.T) {
}

func TestStorageAppend(t *testing.T) {
ents := []pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}}
ents := index(3).terms(3, 4, 5)
tests := []struct {
entries []pb.Entry

werr error
wentries []pb.Entry
}{
{
[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}},
index(1).terms(1, 2),
nil,
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}},
index(3).terms(3, 4, 5),
},
{
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}},
index(3).terms(3, 4, 5),
nil,
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}},
index(3).terms(3, 4, 5),
},
{
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 6}, {Index: 5, Term: 6}},
index(3).terms(3, 6, 6),
nil,
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 6}, {Index: 5, Term: 6}},
index(3).terms(3, 6, 6),
},
{
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 5}},
index(3).terms(3, 4, 5, 5),
nil,
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 5}},
index(3).terms(3, 4, 5, 5),
},
// Truncate incoming entries, truncate the existing entries and append.
{
[]pb.Entry{{Index: 2, Term: 3}, {Index: 3, Term: 3}, {Index: 4, Term: 5}},
index(2).terms(3, 3, 5),
nil,
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 5}},
index(3).terms(3, 5),
},
// Truncate the existing entries and append.
{
[]pb.Entry{{Index: 4, Term: 5}},
index(4).terms(5),
nil,
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 5}},
index(3).terms(3, 5),
},
// Direct append.
{
[]pb.Entry{{Index: 6, Term: 5}},
index(6).terms(5),
nil,
[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 5}},
index(3).terms(3, 4, 5, 5),
},
}

Expand Down

0 comments on commit 856c869

Please sign in to comment.