From 3290e8de0a657a2e7446f009546c4e8c6b51655a Mon Sep 17 00:00:00 2001 From: Domino Valdano <2644901+reductionista@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:12:43 -0800 Subject: [PATCH] Add TestBlockHeap --- pkg/solana/logpoller/loader.go | 10 ++++--- pkg/solana/logpoller/loader_test.go | 42 ++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/pkg/solana/logpoller/loader.go b/pkg/solana/logpoller/loader.go index e717002b9..12940367c 100644 --- a/pkg/solana/logpoller/loader.go +++ b/pkg/solana/logpoller/loader.go @@ -323,7 +323,7 @@ type orderedParser struct { func newOrderedParser(parser ProgramEventProcessor, lggr logger.Logger) *orderedParser { op := &orderedParser{ parser: parser, - blocks: &blockHeap{}, + blocks: NewBlockHeap(), ready: make([]uint64, 0), expect: make(map[uint64]int), actual: make(map[uint64][]ProgramEvent), @@ -335,8 +335,6 @@ func newOrderedParser(parser ProgramEventProcessor, lggr logger.Logger) *ordered Close: op.close, }.NewServiceEngine(lggr) - heap.Init(op.blocks) - return op } @@ -487,6 +485,12 @@ var ( errExpectationsNotSet = errors.New("expectations not set") ) +func NewBlockHeap() *blockHeap { + h := &blockHeap{} + heap.Init(h) + return h +} + type blockHeap []uint64 func (h blockHeap) Len() int { return len(h) } diff --git a/pkg/solana/logpoller/loader_test.go b/pkg/solana/logpoller/loader_test.go index b4a0b6215..1b7fd7933 100644 --- a/pkg/solana/logpoller/loader_test.go +++ b/pkg/solana/logpoller/loader_test.go @@ -1,9 +1,11 @@ package logpoller_test import ( + "container/heap" "context" "crypto/rand" "reflect" + "slices" "sync" "sync/atomic" "testing" @@ -19,7 +21,7 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/utils/tests" "github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller" - mocks "github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller/mocks" + "github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller/mocks" ) var ( @@ -110,6 +112,44 @@ func TestEncodedLogCollector_ParseSingleEvent(t *testing.T) { }) } +func TestBlockHeap(t *testing.T) { + testBlocks := []uint64{53, 19, 105, 21, 6, 9} + expected := make([]uint64, len(testBlocks)) + copy(expected, testBlocks) + slices.Sort(expected) + + blocks := logpoller.NewBlockHeap() + for _, b := range testBlocks { + heap.Push(blocks, b) + } + + // Current iteration pattern: fails + for i, b := range *blocks { + assert.Equal(t, expected[i], b) + } + + // Suggested iteration pattern + for _, e := range expected { + block := (*blocks)[0] + assert.Equal(t, e, block) + // + // If waiting on information for block + // break + // + // If expectations 0 + // heap.Pop(blocks) + // continue + // + // If expectations met + // ExpectBlock(block) + // heap.Pop(blocks) + // + assert.Equal(t, e, heap.Pop(blocks)) + + // Else: return + } +} + func TestEncodedLogCollector_MultipleEventOrdered(t *testing.T) { t.Parallel()