Skip to content

Commit

Permalink
Add TestBlockHeap
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Dec 12, 2024
1 parent ddf1048 commit 3290e8d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
10 changes: 7 additions & 3 deletions pkg/solana/logpoller/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -335,8 +335,6 @@ func newOrderedParser(parser ProgramEventProcessor, lggr logger.Logger) *ordered
Close: op.close,
}.NewServiceEngine(lggr)

heap.Init(op.blocks)

return op
}

Expand Down Expand Up @@ -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) }
Expand Down
42 changes: 41 additions & 1 deletion pkg/solana/logpoller/loader_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package logpoller_test

import (
"container/heap"
"context"
"crypto/rand"
"reflect"
"slices"
"sync"
"sync/atomic"
"testing"
Expand All @@ -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 (
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 3290e8d

Please sign in to comment.