Skip to content

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

Closed
mariohd wants to merge 1 commit into
mainfrom
fix/wallet-balance-race
Closed

test(wallets): wait for async ongoing balance refresh#359
mariohd wants to merge 1 commit into
mainfrom
fix/wallet-balance-race

Conversation

@mariohd

@mariohd mariohd commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Context

Six wallet integration tests (#update, #get, #get_all on both spec/integration/wallets_spec.rb and spec/integration/customers/wallets_spec.rb) fail in CI with expected: "10.0", got: "0.0" on credits_ongoing_balance.

balance_cents is updated synchronously when the wallet transaction settles, but ongoing_balance_cents / credits_ongoing_balance are refreshed by an async RefreshWalletJob. The existing wait_for_balance_update only polls until balance_cents == 2000, so the assertions can run before the ongoing refresh has landed and the wallet still reports 0.0 for the ongoing fields.

Fix

Adds a second wait inside assert_wallet_attributes_with_updated_balance that re-fetches the wallet until ongoing_balance_cents matches the expected value before asserting. The expected value is read from the optional ongoing_balance_cents override (default 2000), so tests that pass a custom override continue to drive the wait correctly.

expected_ongoing_balance_cents = attributes.fetch(:ongoing_balance_cents, 2000)
wait_until do
  wallet = client.wallets.get(wallet.lago_id)   # client.customers.wallets.get(...) in the nested file
  wallet.ongoing_balance_cents == expected_ongoing_balance_cents
end

Same shape applied to both the top-level (wallets_spec.rb) and customer-scoped (customers/wallets_spec.rb) helpers.

Tests

docker compose exec client bundle exec rspec \
  spec/integration/wallets_spec.rb \
  spec/integration/customers/wallets_spec.rb

All ten previously-failing examples pass; no regressions in the surrounding suites. The #destroy flake that occasionally surfaces when the two files run back-to-back is unrelated (HTTP 409 from leftover Lago state), passes in isolation, and was already green before this branch.

## Context

Six wallet integration tests (`#update`, `#get`, `#get_all` on both
`spec/integration/wallets_spec.rb` and `spec/integration/customers/
wallets_spec.rb`) intermittently fail in CI with
`expected: "10.0", got: "0.0"` on `credits_ongoing_balance`.

`balance_cents` is synced synchronously when the wallet transaction
settles, but `ongoing_balance_cents` / `credits_ongoing_balance` are
refreshed by an async job (RefreshWalletJob). The existing
`wait_for_balance_update` only polls until `balance_cents == 2000`,
so the assertion can run before the ongoing refresh has landed and
the wallet still reports `0.0`.

## Description

Re-fetches the wallet inside `assert_wallet_attributes_with_updated_
balance` and waits until `ongoing_balance_cents` matches the
expected value before asserting. The expected value is read from
the optional `ongoing_balance_cents` override (default 2000), so
tests that override it continue to drive the wait correctly.
@mariohd mariohd closed this Jun 11, 2026
@mariohd

mariohd commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Closed in favor of #357

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.

1 participant