Skip to content

Commit dffced0

Browse files
authored
fix: add Maybe BalanceAt expectations to tests missing them (#22042)
* fix: add Maybe BalanceAt expectations to prevent race with testify reflect Tests using mock eth clients that don't set a BalanceAt expectation are vulnerable to a data race: the balance monitor's background worker may call BalanceAt during shutdown, hitting testify's unexpected-call error path which does fmt.Sprintf("%#v", ctx) — an unsynchronized reflect read that races with concurrent context cancellation. Adding .Maybe().Return(big.NewInt(0), nil) registers the expectation so testify takes the happy path (match and return) instead of the error path (reflect into arguments). The race cannot fire on the happy path. * fix: correct import ordering for goimports lint
1 parent 6eb6b84 commit dffced0

6 files changed

Lines changed: 9 additions & 0 deletions

File tree

core/internal/features/features_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func TestIntegration_ExternalInitiatorV2(t *testing.T) {
8686

8787
ctx := testutils.Context(t)
8888
ethClient := cltest.NewEthMocksWithStartupAssertions(t)
89+
ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
8990

9091
cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
9192
c.JobPipeline.ExternalInitiatorsEnabled = ptr(true)

core/services/job/runner_integration_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ func TestRunner_Success_Callback_AsyncJob(t *testing.T) {
784784
t.Parallel()
785785

786786
ethClient := cltest.NewEthMocksWithStartupAssertions(t)
787+
ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
787788

788789
cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
789790
t := true
@@ -963,6 +964,7 @@ func TestRunner_Error_Callback_AsyncJob(t *testing.T) {
963964
t.Parallel()
964965

965966
ethClient := cltest.NewEthMocksWithStartupAssertions(t)
967+
ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
966968

967969
cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
968970
t := true

core/services/pipeline/task.eth_call_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ func TestETHCallTask(t *testing.T) {
297297
}
298298

299299
ethClient := clienttest.NewClient(t)
300+
ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
300301
config := pipelinemocks.NewConfig(t)
301302
test.setupClientMocks(ethClient, config)
302303

core/services/relay/evm/write_target_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func TestEvmWrite(t *testing.T) {
125125
ht.On("LatestAndFinalizedBlock", mock.Anything).Return(&evmtypes.Head{Number: 99}, &evmtypes.Head{}, nil)
126126
chain.On("HeadTracker").Return(ht)
127127

128+
evmClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
128129
chain.On("Client").Return(evmClient)
129130

130131
evmCfg := configtest.NewChainScopedConfig(t, func(c *toml.EVMConfig) {

core/services/vrf/v2/integration_v2_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,7 @@ func TestStartingCountsV1(t *testing.T) {
21292129
ec.On("Dial", mock.Anything).Maybe().Return(nil)
21302130
ec.On("Close").Maybe().Return(nil)
21312131
ec.On("ConfiguredChainID").Return(testutils.SimulatedChainID)
2132+
ec.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
21322133
ec.On("LatestBlockHeight", mock.Anything).Return(big.NewInt(2), nil).Maybe()
21332134
txm := makeTestTxm(t, txStore, ks.Eth(), ec)
21342135
legacyChains := evmtest.NewLegacyChains(t, evmtest.TestChainOpts{

core/web/pipeline_runs_controller_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"math/big"
78
"net/http"
89
"net/url"
910
"strconv"
@@ -36,6 +37,7 @@ func TestPipelineRunsController_CreateWithBody_HappyPath(t *testing.T) {
3637

3738
ctx := testutils.Context(t)
3839
ethClient := cltest.NewEthMocksWithStartupAssertions(t)
40+
ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
3941
cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
4042
c.JobPipeline.HTTPRequest.DefaultTimeout = commonconfig.MustNewDuration(2 * time.Second)
4143
c.Database.Listener.FallbackPollInterval = commonconfig.MustNewDuration(10 * time.Millisecond)
@@ -92,6 +94,7 @@ func TestPipelineRunsController_CreateNoBody_HappyPath(t *testing.T) {
9294

9395
ctx := testutils.Context(t)
9496
ethClient := cltest.NewEthMocksWithStartupAssertions(t)
97+
ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)
9598
cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
9699
c.JobPipeline.HTTPRequest.DefaultTimeout = commonconfig.MustNewDuration(2 * time.Second)
97100
c.Database.Listener.FallbackPollInterval = commonconfig.MustNewDuration(10 * time.Millisecond)

0 commit comments

Comments
 (0)