From fc506bf38c1c40ed5f624998cce2cbf839ac8bdf Mon Sep 17 00:00:00 2001 From: Aleksandr Bukata Date: Fri, 21 Jun 2024 17:14:45 +0100 Subject: [PATCH] tests --- .../headreporter/head_reporter_test.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/services/headreporter/head_reporter_test.go b/core/services/headreporter/head_reporter_test.go index da30ab574b..292ab8cf75 100644 --- a/core/services/headreporter/head_reporter_test.go +++ b/core/services/headreporter/head_reporter_test.go @@ -8,26 +8,30 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/headreporter" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + "sync/atomic" "testing" "time" ) func Test_HeadReporterService(t *testing.T) { - t.Run("c", func(t *testing.T) { + t.Run("report everything", func(t *testing.T) { db := pgtest.NewSqlxDB(t) headReporter := mocks.NewHeadReporter(t) - service := headreporter.NewHeadReporterServiceWithReporters(db, newLegacyChainContainer(t, db), logger.TestLogger(t), []headreporter.HeadReporter{headReporter}) + service := headreporter.NewHeadReporterServiceWithReporters(db, newLegacyChainContainer(t, db), logger.TestLogger(t), []headreporter.HeadReporter{headReporter}, time.Second) err := service.Start(testutils.Context(t)) require.NoError(t, err) + var reportCalls atomic.Int32 head := newHead() - headReporter.On("ReportNewHead", mock.Anything, &head).Return(nil) - headReporter.On("ReportPeriodic", mock.Anything).Return(nil) + headReporter.On("ReportNewHead", mock.Anything, &head).Run(func(args mock.Arguments) { + reportCalls.Add(1) + }).Return(nil) + headReporter.On("ReportPeriodic", mock.Anything).Run(func(args mock.Arguments) { + reportCalls.Add(1) + }).Return(nil) service.OnNewLongestChain(testutils.Context(t), &head) - require.Eventually(t, func() bool { - return headReporter.AssertCalled(t, "ReportNewHead", mock.Anything, &head) && headReporter.AssertCalled(t, "ReportPeriodic", mock.Anything) - }, time.Second, 10*time.Millisecond) + require.Eventually(t, func() bool { return reportCalls.Load() == 2 }, 5*time.Second, 100*time.Millisecond) }) }