Skip to content

Commit

Permalink
fixed head listener test
Browse files Browse the repository at this point in the history
  • Loading branch information
yongkangc committed Jun 23, 2023
1 parent 3571cc7 commit 9e3eec8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
8 changes: 8 additions & 0 deletions pkg/internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"
"time"

"github.com/smartcontractkit/chainlink-solana/pkg/solana/headtracker"
)

// TODO: These can prob refactor to chainlink internal testutils
Expand Down Expand Up @@ -42,3 +44,9 @@ func WaitTimeout(t *testing.T) time.Duration {
// TestInterval is just a sensible poll interval that gives fast tests without
// risk of spamming
const TestInterval = 100 * time.Millisecond

// NewHeadtrackerConfig returns a new Solana Headtracker Config with overrides.
func NewHeadtrackerConfig(config *headtracker.Config, overrideFn func(*headtracker.Config)) *headtracker.Config {
overrideFn(config)
return config
}
48 changes: 26 additions & 22 deletions pkg/solana/headtracker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package headtracker

import (
"time"

htrktypes "github.com/smartcontractkit/chainlink-relay/pkg/headtracker/types"
)

// This config serves as a POC for headtracker.
// It should be replaced with a more robust config
// such as the one in pkg/solana/config
// This Config serves as a POC for headtracker.
// It should be replaced with a more robust Config
// such as the one in pkg/solana/Config

// TODO: replace this config with a more robust config
type config struct {
defaults configSet
// TODO: replace this Config with a more robust Config. Requires research
type Config struct {
Defaults configSet
}

type configSet struct {
Expand All @@ -24,40 +26,42 @@ type configSet struct {

var defaultConfigSet = configSet{
// headtracker
BlockEmissionIdleWarningThreshold: 30 * time.Second,
BlockEmissionIdleWarningThreshold: 30 * time.Second, // TODO: Check this Config value again
FinalityDepth: 50,
HeadTrackerHistoryDepth: 100,
HeadTrackerMaxBufferSize: 3,
HeadTrackerSamplingInterval: 1 * time.Second,
PollingInterval: 2 * time.Second,
}

func NewConfig() *config {
return &config{
defaults: defaultConfigSet,
func NewConfig() *Config {
return &Config{
Defaults: defaultConfigSet,
}
}

func (c *config) BlockEmissionIdleWarningThreshold() time.Duration {
return c.defaults.BlockEmissionIdleWarningThreshold
var _ htrktypes.Config = &Config{}

func (c *Config) BlockEmissionIdleWarningThreshold() time.Duration {
return c.Defaults.BlockEmissionIdleWarningThreshold
}

func (c *config) FinalityDepth() uint32 {
return c.defaults.FinalityDepth
func (c *Config) FinalityDepth() uint32 {
return c.Defaults.FinalityDepth
}

func (c *config) HeadTrackerHistoryDepth() uint32 {
return c.defaults.HeadTrackerHistoryDepth
func (c *Config) HeadTrackerHistoryDepth() uint32 {
return c.Defaults.HeadTrackerHistoryDepth
}

func (c *config) HeadTrackerMaxBufferSize() uint32 {
return c.defaults.HeadTrackerMaxBufferSize
func (c *Config) HeadTrackerMaxBufferSize() uint32 {
return c.Defaults.HeadTrackerMaxBufferSize
}

func (c *config) HeadTrackerSamplingInterval() time.Duration {
return c.defaults.HeadTrackerSamplingInterval
func (c *Config) HeadTrackerSamplingInterval() time.Duration {
return c.Defaults.HeadTrackerSamplingInterval
}

func (c *config) PollingInterval() time.Duration {
return c.defaults.PollingInterval
func (c *Config) PollingInterval() time.Duration {
return c.Defaults.PollingInterval
}
14 changes: 6 additions & 8 deletions pkg/solana/headtracker/head_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,15 @@ func Test_HeadListener_NotReceivingHeads(t *testing.T) {
lggr, _ := logger.New()
client := cltest.NewClientMockWithDefaultChain(t)
cfg := headtracker.NewConfig()
overridenConfig := testutils.NewHeadtrackerConfig(cfg, func(c *headtracker.Config) {
c.Defaults.BlockEmissionIdleWarningThreshold = 1 * time.Second
})
chStop := make(chan struct{})
hl := headtracker.NewListener(lggr, client, cfg, chStop)
hl := headtracker.NewListener(lggr, client, overridenConfig, chStop)

firstHeadAwaiter := cltest.NewAwaiter()
// handler := func(context.Context, *types.Head) error {
// firstHeadAwaiter.ItHappened()
// return nil
// }

var headCount atomic.Int32
handler := func(context.Context, *types.Head) error {
headCount.Add(1)
firstHeadAwaiter.ItHappened()
return nil
}

Expand Down Expand Up @@ -130,6 +127,7 @@ func Test_HeadListener_NotReceivingHeads(t *testing.T) {
go hl.ListenForNewHeads(handler, done)

subscribeAwaiter.AwaitOrFail(t, testutils.WaitTimeout(t))
require.Eventually(t, hl.Connected, testutils.WaitTimeout(t), testutils.TestInterval)

chHeads <- cltest.Head(0)
firstHeadAwaiter.AwaitOrFail(t)
Expand Down

0 comments on commit 9e3eec8

Please sign in to comment.