Skip to content

Commit

Permalink
test(store): table test for GetRange
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Oct 10, 2023
1 parent 181510e commit 210cb9a
Showing 1 changed file with 55 additions and 12 deletions.
67 changes: 55 additions & 12 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ func TestStore_GetRangeByHeight_ExpectedRange(t *testing.T) {
assert.Equal(t, lastHeaderInRangeHeight, out[len(out)-1].Height())
}

// TestStore_GetRange_ExpectedRange
func TestStore_GetRange_ExpectedRange(t *testing.T) {
// TestStore_GetRange tests possible combinations of requests and ensures that
// the store can handle them adequately (even malformed requests)
func TestStore_GetRange(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
t.Cleanup(cancel)

Expand All @@ -158,18 +159,60 @@ func TestStore_GetRange_ExpectedRange(t *testing.T) {
err = store.Append(ctx, in...)
require.NoError(t, err)

// request the range [4:98]
firstHeaderInRangeHeight := uint64(4)
lastHeaderInRangeHeight := uint64(98)
to := lastHeaderInRangeHeight + 1
expectedLenHeaders := to - firstHeaderInRangeHeight // expected amount
var tests = []struct {
name string
from uint64
to uint64
expectedError bool
}{
{
name: "valid request for headers all contained in store",
from: 4,
to: 99,
expectedError: false,
},
{
name: "malformed request for headers contained in store",
from: 99,
to: 4,
expectedError: true,
},
{
name: "valid request for headers not contained in store",
from: 100,
to: 500,
expectedError: true,
},
{
name: "valid request for headers partially contained in store (`to` is greater than chain head)",
from: 77,
to: 200,
expectedError: true,
},
}

out, err := store.GetRange(ctx, firstHeaderInRangeHeight, to)
require.NoError(t, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
firstHeaderInRangeHeight := tt.from
lastHeaderInRangeHeight := tt.to - 1
to := lastHeaderInRangeHeight + 1
expectedLenHeaders := to - firstHeaderInRangeHeight // expected amount

// request the range [tt.to:tt.from)
out, err := store.GetRange(ctx, firstHeaderInRangeHeight, to)
if tt.expectedError {
assert.Error(t, err)
assert.Nil(t, out)
return
}
require.NoError(t, err)

assert.Len(t, out, int(expectedLenHeaders))
assert.Equal(t, firstHeaderInRangeHeight, out[0].Height())
assert.Equal(t, lastHeaderInRangeHeight, out[len(out)-1].Height())
})
}

assert.Len(t, out, int(expectedLenHeaders))
assert.Equal(t, firstHeaderInRangeHeight, out[0].Height())
assert.Equal(t, lastHeaderInRangeHeight, out[len(out)-1].Height())
}

func TestStorePendingCacheMiss(t *testing.T) {
Expand Down

0 comments on commit 210cb9a

Please sign in to comment.