pollForShadowCreditBalance polls until the shadow balance's CreditBalance reaches the expected value
(ctx context.Context, ds database.IDataSource, balanceID string, expectedCredit *big.Int, pollInterval, timeout time.Duration)
| 64 | |
| 65 | // pollForShadowCreditBalance polls until the shadow balance's CreditBalance reaches the expected value |
| 66 | func pollForShadowCreditBalance(ctx context.Context, ds database.IDataSource, balanceID string, expectedCredit *big.Int, pollInterval, timeout time.Duration) (*model.Balance, error) { |
| 67 | timeoutCtx, cancel := context.WithTimeout(ctx, timeout) |
| 68 | defer cancel() |
| 69 | |
| 70 | ticker := time.NewTicker(pollInterval) |
| 71 | defer ticker.Stop() |
| 72 | |
| 73 | var lastBalance *model.Balance |
| 74 | for { |
| 75 | select { |
| 76 | case <-timeoutCtx.Done(): |
| 77 | if lastBalance != nil { |
| 78 | return nil, fmt.Errorf("timed out waiting for shadow balance %s CreditBalance to reach %s (got %s): %w", |
| 79 | balanceID, expectedCredit.String(), lastBalance.CreditBalance.String(), timeoutCtx.Err()) |
| 80 | } |
| 81 | return nil, fmt.Errorf("timed out waiting for shadow balance %s CreditBalance to reach %s: %w", |
| 82 | balanceID, expectedCredit.String(), timeoutCtx.Err()) |
| 83 | case <-ticker.C: |
| 84 | balance, err := ds.GetBalanceByIDLite(balanceID) |
| 85 | if err != nil { |
| 86 | continue |
| 87 | } |
| 88 | lastBalance = balance |
| 89 | if balance.CreditBalance.Cmp(expectedCredit) == 0 { |
| 90 | return balance, nil |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // pollForAggregateDebitBalance polls until the aggregate balance's DebitBalance reaches the expected value |
| 97 | func pollForAggregateDebitBalance(ctx context.Context, ds database.IDataSource, balanceID string, expectedDebit *big.Int, pollInterval, timeout time.Duration) (*model.Balance, error) { |
no test coverage detected