lntest: extract WaitForBalance into a method

This commit is contained in:
Wilmer Paulino 2018-08-09 21:15:40 -07:00
parent aa7c2e6d47
commit 0a335bbea2
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
2 changed files with 29 additions and 20 deletions

@ -1171,24 +1171,6 @@ func (n *NetworkHarness) sendCoins(ctx context.Context, amt btcutil.Amount,
return err
}
// Pause until the nodes current wallet balances reflects the amount
// sent to it above.
// TODO(roasbeef): factor out into helper func
balanceTicker := time.Tick(time.Millisecond * 50)
balanceTimeout := time.After(time.Second * 30)
for {
select {
case <-balanceTicker:
currentBal, err := target.WalletBalance(ctx, balReq)
if err != nil {
return err
}
if currentBal.ConfirmedBalance == initialBalance.ConfirmedBalance+int64(amt) {
return nil
}
case <-balanceTimeout:
return fmt.Errorf("balances not synced after deadline")
}
}
expectedBalance := initialBalance.ConfirmedBalance + int64(amt)
return target.WaitForBalance(expectedBalance, true)
}

@ -895,6 +895,33 @@ func (hn *HarnessNode) WaitForBlockchainSync(ctx context.Context) error {
}
}
// WaitForBalance waits until the node sees the expected confirmed/unconfirmed
// balance within their wallet.
func (hn *HarnessNode) WaitForBalance(expectedBalance int64, confirmed bool) error {
ctx := context.Background()
req := &lnrpc.WalletBalanceRequest{}
doesBalanceMatch := func() bool {
balance, err := hn.WalletBalance(ctx, req)
if err != nil {
return false
}
if confirmed {
return balance.ConfirmedBalance == expectedBalance
}
return balance.UnconfirmedBalance == expectedBalance
}
err := WaitPredicate(doesBalanceMatch, 30*time.Second)
if err != nil {
return errors.New("balances not synced after deadline")
}
return nil
}
// fileExists reports whether the named file or directory exists.
// This function is taken from https://github.com/btcsuite/btcd
func fileExists(name string) bool {