Skip to content

test(wallets): wait for async ongoing balance refresh#357

Merged
mariohd merged 2 commits into
mainfrom
test/fix-wallet-ongoing-balance-async
Jun 12, 2026
Merged

test(wallets): wait for async ongoing balance refresh#357
mariohd merged 2 commits into
mainfrom
test/fix-wallet-ongoing-balance-async

Conversation

@groyoh

@groyoh groyoh commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Context

The wallet integration tests started racing against lago-api once the ongoing balance refresh moved to an async job (getlago/lago-api#5631).

Wallets::Balance::IncreaseService now sets balance_cents, credits_balance and last_balance_sync_at in-line when the wallet transaction settles, then enqueues RefreshWalletJob which sets ongoing_balance_cents and credits_ongoing_balance. So once balance_cents reaches 2000, the ongoing balance is only eventually 2000. assert_wallet_attributes_with_updated_balance asserted the ongoing fields directly, so it could read a stale 0 before the async job ran.

Description

assert_wallet_attributes_with_updated_balance now re-fetches the wallet until ongoing_balance_cents reaches 2000 before asserting. The fix sits in the assertion rather than wait_for_balance_update (which keeps waiting on the synchronous balance_cents). It's backward compatible with the synchronous behaviour: when the ongoing balance is already synced, the poll returns on the first fetch.

## Context

lago-api now refreshes the wallet ongoing balance asynchronously after a
balance increase (getlago/lago-api#5631). `Wallets::Balance::IncreaseService`
sets `balance_cents`, `credits_balance` and `last_balance_sync_at` in-line, then
enqueues `RefreshWalletJob` which sets `ongoing_balance_cents` and
`credits_ongoing_balance`. Once `balance_cents` reaches 2000 the ongoing balance
is only eventually 2000, so asserting it directly races the async job.

## Description

`assert_wallet_attributes_with_updated_balance` now re-fetches the wallet until
`ongoing_balance_cents` reaches 2000 before asserting. This is backward
compatible with the synchronous behaviour (the poll returns on the first fetch
when the ongoing balance is already synced).
Copilot AI review requested due to automatic review settings June 11, 2026 12:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a race condition in the wallet integration tests caused by ongoing_balance_cents being refreshed asynchronously (via RefreshWalletJob) after a wallet transaction settles, which can make assertions read stale ongoing-balance fields.

Changes:

  • Add a wait_until polling loop in assert_wallet_attributes_with_updated_balance to re-fetch the wallet until ongoing_balance_cents reflects the refreshed value before asserting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread spec/integration/customers/wallets_spec.rb
Comment thread spec/integration/customers/wallets_spec.rb
## Context

PR review flagged two issues with the async ongoing balance wait:
the polling condition hard-coded 2000 while the helper accepts an
`ongoing_balance_cents` override, and the legacy `client.wallets` spec
(`spec/integration/wallets_spec.rb`) had the same race without the fix.

## Description

The wait now derives the expected ongoing balance from the passed
attributes (defaulting to 2000) so it stays consistent with the
assertions. The same re-fetch wait was applied to the legacy
`assert_wallet_attributes_with_updated_balance` helper.
@mariohd mariohd merged commit fa36b3c into main Jun 12, 2026
6 checks passed
@mariohd mariohd deleted the test/fix-wallet-ongoing-balance-async branch June 12, 2026 12:04
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.

4 participants