From 856c869300f2e410210e5838991b60b1899ef7a0 Mon Sep 17 00:00:00 2001 From: Pavel Kalinnikov Date: Sat, 3 Feb 2024 14:42:29 +0000 Subject: [PATCH] tests: use entry slice helper in storage_test Signed-off-by: Pavel Kalinnikov --- storage_test.go | 60 ++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/storage_test.go b/storage_test.go index db39ccb7..44cf6023 100644 --- a/storage_test.go +++ b/storage_test.go @@ -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 @@ -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 @@ -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 { @@ -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() @@ -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 @@ -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") @@ -170,7 +170,7 @@ 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 @@ -178,42 +178,42 @@ func TestStorageAppend(t *testing.T) { 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), }, }