From 9e3eec8f103d912a972fcbb9164cb17e7e9160e0 Mon Sep 17 00:00:00 2001 From: Yong Kang Date: Fri, 23 Jun 2023 14:59:21 -0700 Subject: [PATCH] fixed head listener test --- pkg/internal/testutils/testutils.go | 8 ++++ pkg/solana/headtracker/config.go | 48 +++++++++++--------- pkg/solana/headtracker/head_listener_test.go | 14 +++--- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/pkg/internal/testutils/testutils.go b/pkg/internal/testutils/testutils.go index a52b2dfa3..55948e679 100644 --- a/pkg/internal/testutils/testutils.go +++ b/pkg/internal/testutils/testutils.go @@ -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 @@ -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 +} diff --git a/pkg/solana/headtracker/config.go b/pkg/solana/headtracker/config.go index b80b00bc0..5dd90bf62 100644 --- a/pkg/solana/headtracker/config.go +++ b/pkg/solana/headtracker/config.go @@ -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 { @@ -24,7 +26,7 @@ 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, @@ -32,32 +34,34 @@ var defaultConfigSet = configSet{ 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 } diff --git a/pkg/solana/headtracker/head_listener_test.go b/pkg/solana/headtracker/head_listener_test.go index 4daac9834..d50268868 100644 --- a/pkg/solana/headtracker/head_listener_test.go +++ b/pkg/solana/headtracker/head_listener_test.go @@ -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 } @@ -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)