Skip to content

Commit

Permalink
polygon/heimdall: unit test entityFetcher.FetchAllEntities (#10379)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored May 20, 2024
1 parent d514b5c commit 9ec941d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
15 changes: 11 additions & 4 deletions polygon/heimdall/entity_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import (
type entityFetcher[TEntity Entity] interface {
FetchLastEntityId(ctx context.Context) (uint64, error)
FetchEntitiesRange(ctx context.Context, idRange ClosedRange) ([]TEntity, error)
FetchAllEntities(ctx context.Context) ([]TEntity, error)
}

type entityFetcherImpl[TEntity Entity] struct {
name string

fetchLastEntityId func(ctx context.Context) (int64, error)
fetchEntity func(ctx context.Context, id int64) (TEntity, error)
fetchEntitiesPage func(ctx context.Context, page uint64, limit uint64) ([]TEntity, error)

fetchEntitiesPage func(ctx context.Context, page uint64, limit uint64) ([]TEntity, error)
fetchEntitiesPageLimit uint64

logger log.Logger
}
Expand All @@ -30,14 +33,18 @@ func newEntityFetcher[TEntity Entity](
fetchLastEntityId func(ctx context.Context) (int64, error),
fetchEntity func(ctx context.Context, id int64) (TEntity, error),
fetchEntitiesPage func(ctx context.Context, page uint64, limit uint64) ([]TEntity, error),
fetchEntitiesPageLimit uint64,
logger log.Logger,
) entityFetcher[TEntity] {
return &entityFetcherImpl[TEntity]{
name: name,
fetchLastEntityId: fetchLastEntityId,
fetchEntity: fetchEntity,
fetchEntitiesPage: fetchEntitiesPage,
logger: logger,

fetchEntitiesPage: fetchEntitiesPage,
fetchEntitiesPageLimit: fetchEntitiesPageLimit,

logger: logger,
}
}

Expand Down Expand Up @@ -82,7 +89,7 @@ func (f *entityFetcherImpl[TEntity]) FetchAllEntities(ctx context.Context) ([]TE
defer progressLogTicker.Stop()

for page := uint64(1); ; page++ {
entitiesPage, err := f.fetchEntitiesPage(ctx, page, 10_000)
entitiesPage, err := f.fetchEntitiesPage(ctx, page, f.fetchEntitiesPageLimit)
if err != nil {
return nil, err
}
Expand Down
52 changes: 52 additions & 0 deletions polygon/heimdall/entity_fetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package heimdall

import (
"context"
"testing"

"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

libcommon "github.com/ledgerwatch/erigon-lib/common"
)

func TestEntityFetcher_FetchAllEntities(t *testing.T) {
for count := uint64(0); count < 20; count++ {
testEntityFetcher_FetchAllEntities(t, count, 5)
}
}

func testEntityFetcher_FetchAllEntities(t *testing.T, count uint64, fetchEntitiesPageLimit uint64) {
ctx := context.Background()
logger := log.New()

var expectedEntities []*Checkpoint
for i := uint64(0); i < count; i++ {
c := makeCheckpoint(i*256, 256)
c.Id = CheckpointId(i + 1)
expectedEntities = append(expectedEntities, c)
}

servedEntities := make([]*Checkpoint, len(expectedEntities))
copy(servedEntities, expectedEntities)
libcommon.SliceShuffle(servedEntities)
fetchEntitiesPage := func(ctx context.Context, page uint64, limit uint64) ([]*Checkpoint, error) {
offset := (page - 1) * limit
totalLen := uint64(len(servedEntities))
return servedEntities[min(offset, totalLen):min(offset+limit, totalLen)], nil
}

fetcher := newEntityFetcher[*Checkpoint](
"fetcher",
nil,
nil,
fetchEntitiesPage,
fetchEntitiesPageLimit,
logger,
)

actualEntities, err := fetcher.FetchAllEntities(ctx)
require.NoError(t, err)
assert.Equal(t, expectedEntities, actualEntities)
}
3 changes: 3 additions & 0 deletions polygon/heimdall/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func newCheckpointFetcher(client HeimdallClient, logger log.Logger) entityFetche
client.FetchCheckpointCount,
client.FetchCheckpoint,
client.FetchCheckpoints,
10_000, // fetchEntitiesPageLimit
logger,
)
}
Expand All @@ -131,6 +132,7 @@ func newMilestoneFetcher(client HeimdallClient, logger log.Logger) entityFetcher
client.FetchMilestoneCount,
client.FetchMilestone,
nil,
0,
logger,
)
}
Expand All @@ -153,6 +155,7 @@ func newSpanFetcher(client HeimdallClient, logger log.Logger) entityFetcher[*Spa
fetchLastEntityId,
fetchEntity,
nil,
0,
logger,
)
}
Expand Down

0 comments on commit 9ec941d

Please sign in to comment.