test(wallets): wait for async ongoing balance refresh#357
Merged
Conversation
## 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).
There was a problem hiding this comment.
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_untilpolling loop inassert_wallet_attributes_with_updated_balanceto re-fetch the wallet untilongoing_balance_centsreflects the refreshed value before asserting.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
## 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.
vincent-pochet
approved these changes
Jun 11, 2026
mariohd
approved these changes
Jun 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::IncreaseServicenow setsbalance_cents,credits_balanceandlast_balance_sync_atin-line when the wallet transaction settles, then enqueuesRefreshWalletJobwhich setsongoing_balance_centsandcredits_ongoing_balance. So oncebalance_centsreaches 2000, the ongoing balance is only eventually 2000.assert_wallet_attributes_with_updated_balanceasserted the ongoing fields directly, so it could read a stale 0 before the async job ran.Description
assert_wallet_attributes_with_updated_balancenow re-fetches the wallet untilongoing_balance_centsreaches 2000 before asserting. The fix sits in the assertion rather thanwait_for_balance_update(which keeps waiting on the synchronousbalance_cents). It's backward compatible with the synchronous behaviour: when the ongoing balance is already synced, the poll returns on the first fetch.