From 58d42c9854defe1e68c1577449698d0f749d070f Mon Sep 17 00:00:00 2001 From: Domino Valdano <2644901+reductionista@users.noreply.github.com> Date: Sat, 28 Sep 2024 19:19:56 -0700 Subject: [PATCH] Add test for ExecPagedQuery --- core/chains/evm/logpoller/orm_test.go | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/core/chains/evm/logpoller/orm_test.go b/core/chains/evm/logpoller/orm_test.go index e5a73c6f50e..cc3d79ae9e2 100644 --- a/core/chains/evm/logpoller/orm_test.go +++ b/core/chains/evm/logpoller/orm_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "database/sql" + "errors" "fmt" "math" "math/big" @@ -11,6 +12,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/mock" + "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/ethereum/go-ethereum/common" @@ -1155,6 +1158,69 @@ func TestORM_SelectLogsWithSigsByBlockRangeFilter(t *testing.T) { assertion(t, logs, err, startBlock, endBlock) } +type mockQueryExecutor struct { + mock.Mock +} + +func (m *mockQueryExecutor) Exec(limitBlock int64) (int64, error) { + res := m.Called(limitBlock) + return int64(res.Int(0)), res.Error(1) +} + +func TestORM_ExecPagedQuery(t *testing.T) { + t.Parallel() + ctx := testutils.Context(t) + lggr := logger.Test(t) + chainID := testutils.NewRandomEVMChainID() + db := pgtest.NewSqlxDB(t) + o := logpoller.NewORM(chainID, db, lggr) + + m := mockQueryExecutor{} + + queryError := errors.New("some error") + m.On("Exec", int64(0)).Return(0, queryError).Once() + + // Should handle errors gracefully + _, err := o.ExecPagedQuery(ctx, 0, 0, m.Exec) + assert.ErrorIs(t, err, queryError) + + m.On("Exec", int64(60)).Return(4, nil).Once() + + // Query should only get executed once with limitBlock=end if called with limit=0 + numResults, err := o.ExecPagedQuery(ctx, 0, 60, m.Exec) + require.NoError(t, err) + assert.Equal(t, int64(4), numResults) + + // Should report actual db errors + _, err = o.ExecPagedQuery(ctx, 300, 1000, m.Exec) + assert.Error(t, err) + + o.InsertBlock(ctx, common.HexToHash("0x1234"), 42, time.Now(), 0) + + m.On("Exec", mock.Anything).Return(3, nil) + + // Should get called with limitBlock = 342, 642, 942, 1000 + numResults, err = o.ExecPagedQuery(ctx, 300, 1000, m.Exec) + require.NoError(t, err) + assert.Equal(t, int64(12), numResults) // 3 results in each of 4 calls + m.AssertNumberOfCalls(t, "Exec", 6) // 4 new calls, plus the prior 2 + expectedLimitBlocks := []int64{341, 641, 941, 1000} + for _, expected := range expectedLimitBlocks { + m.AssertCalled(t, "Exec", expected) + } + + // Should not go all the way to 1000, but stop after ~ 13 results have + // been returned + numResults, err = o.ExecPagedQuery(ctx, 15, 1000, m.Exec) + require.NoError(t, err) + assert.Equal(t, int64(15), numResults) + m.AssertNumberOfCalls(t, "Exec", 11) + expectedLimitBlocks = []int64{56, 71, 86, 101, 116} // 42 + 15 * n - 1 for n = 1, 2, 3, 4, 5 + for _, expected := range expectedLimitBlocks { + m.AssertCalled(t, "Exec", expected) + } +} + func TestORM_DeleteBlocksBefore(t *testing.T) { th := SetupTH(t, lpOpts) o1 := th.ORM