Skip to content

fix: add Maybe BalanceAt expectations to tests missing them#22042

Merged
jmank88 merged 2 commits intodevelopfrom
fix/add-balanceat-maybe-expectations
Apr 16, 2026
Merged

fix: add Maybe BalanceAt expectations to tests missing them#22042
jmank88 merged 2 commits intodevelopfrom
fix/add-balanceat-maybe-expectations

Conversation

@Fletch153
Copy link
Copy Markdown
Collaborator

Summary

Adds .Maybe() BalanceAt mock expectations to 8 tests that use mock eth clients without their own BalanceAt expectation. This prevents a data race in go_core_race_tests.

Problem

Tests that create chains with mock clients but complete quickly may never need a BalanceAt expectation. However, the balance monitor's background worker can fire during shutdown, calling BalanceAt on the mock. With no matching expectation, testify hits its "unexpected method call" error path, which calls callString()fmt.Sprintf("%#v", ctx) — an unsynchronized reflect read that races with concurrent context cancellation.

testify's callString() reflecting into arguments is fundamentally broken (unsynchronized), but an upstream fix was rejected. The workaround: make the call expected so testify takes the happy path (match → return, no reflect).

Fix

Added to each vulnerable test:

ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(big.NewInt(0), nil)

.Maybe() means "this call might happen, I don't care either way." If the balance monitor calls BalanceAt, testify matches it and returns — no reflect, no race. If it doesn't call it, AssertExpectations doesn't complain.

Tests Modified

File Test
core/internal/features/features_test.go TestIntegration_ExternalInitiatorV2
core/services/job/runner_integration_test.go TestRunner_Success_Callback_AsyncJob
core/services/job/runner_integration_test.go TestRunner_Error_Callback_AsyncJob
core/services/pipeline/task.eth_call_test.go TestETHCallTask
core/services/relay/evm/write_target_test.go TestEvmWrite
core/services/vrf/v2/integration_v2_test.go TestStartingCountsV1
core/web/pipeline_runs_controller_test.go TestPipelineRunsController_CreateWithBody_HappyPath
core/web/pipeline_runs_controller_test.go TestPipelineRunsController_CreateNoBody_HappyPath

Related

…flect

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.
@github-actions
Copy link
Copy Markdown
Contributor

👋 Fletch153, thanks for creating this pull request!

To help reviewers, please consider creating future PRs as drafts first. This allows you to self-review and make any final changes before notifying the team.

Once you're ready, you can mark it as "Ready for review" to request feedback. Thanks!

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 16, 2026

✅ No conflicts with other open PRs targeting develop

jmank88
jmank88 previously approved these changes Apr 16, 2026
@trunk-io
Copy link
Copy Markdown

trunk-io Bot commented Apr 16, 2026

Static BadgeStatic BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@jmank88 jmank88 requested a review from pavel-raykov April 16, 2026 15:13
@cl-sonarqube-production
Copy link
Copy Markdown

@jmank88 jmank88 added this pull request to the merge queue Apr 16, 2026
Merged via the queue into develop with commit dffced0 Apr 16, 2026
114 of 115 checks passed
@jmank88 jmank88 deleted the fix/add-balanceat-maybe-expectations branch April 16, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants