multi: move WaitPredicate, WaitNoError, WaitInvariant to lntest/wait

This commit is contained in:
Conner Fromknecht committed 2019-09-19 12:46:29 -07:00
1 parent 003441d7e9
commit 1d41d4d666
12 files changed
+193 -180

No files matched your search

+7 -78
View File
@@ -22,6 +22,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwire"
)
@@ -493,7 +494,7 @@ func (n *NetworkHarness) EnsureConnected(ctx context.Context, a, b *HarnessNode)
return false
}
err := WaitPredicate(func() bool {
err := wait.Predicate(func() bool {
return findSelfInPeerList(a, b) && findSelfInPeerList(b, a)
}, time.Second*15)
if err != nil {
@@ -526,7 +527,7 @@ func (n *NetworkHarness) ConnectNodes(ctx context.Context, a, b *HarnessNode) er
return err
}
err = WaitPredicate(func() bool {
err = wait.Predicate(func() bool {
// If node B is seen in the ListPeers response from node A,
// then we can exit early as the connection has been fully
// established.
@@ -1068,12 +1069,12 @@ func (n *NetworkHarness) CloseChannel(ctx context.Context,
// Before proceeding, we'll ensure that the channel is active
// for both nodes.
err = WaitPredicate(activeChanPredicate(lnNode), timeout)
err = wait.Predicate(activeChanPredicate(lnNode), timeout)
if err != nil {
return nil, nil, fmt.Errorf("channel of closing " +
"node not active in time")
}
err = WaitPredicate(activeChanPredicate(receivingNode), timeout)
err = wait.Predicate(activeChanPredicate(receivingNode), timeout)
if err != nil {
return nil, nil, fmt.Errorf("channel of receiving " +
"node not active in time")
@@ -1194,85 +1195,13 @@ func (n *NetworkHarness) AssertChannelExists(ctx context.Context,
return false
}
if err := WaitPredicate(pred, time.Second*15); err != nil {
if err := wait.Predicate(pred, time.Second*15); err != nil {
return fmt.Errorf("channel not found: %v", predErr)
}
return nil
}
// WaitPredicate is a helper test function that will wait for a timeout period
// of time until the passed predicate returns true. This function is helpful as
// timing doesn't always line up well when running integration tests with
// several running lnd nodes. This function gives callers a way to assert that
// some property is upheld within a particular time frame.
func WaitPredicate(pred func() bool, timeout time.Duration) error {
const pollInterval = 20 * time.Millisecond
exitTimer := time.After(timeout)
for {
<-time.After(pollInterval)
select {
case <-exitTimer:
return fmt.Errorf("predicate not satisfied after time out")
default:
}
if pred() {
return nil
}
}
}
// WaitNoError is a wrapper around WaitPredicate that waits for the passed
// method f to execute without error, and returns the last error encountered if
// this doesn't happen within the timeout.
func WaitNoError(f func() error, timeout time.Duration) error {
var predErr error
pred := func() bool {
if err := f(); err != nil {
predErr = err
return false
}
return true
}
// If f() doesn't succeed within the timeout, return the last
// encountered error.
if err := WaitPredicate(pred, timeout); err != nil {
return predErr
}
return nil
}
// WaitInvariant is a helper test function that will wait for a timeout period
// of time, verifying that a statement remains true for the entire duration.
// This function is helpful as timing doesn't always line up well when running
// integration tests with several running lnd nodes. This function gives callers
// a way to assert that some property is maintained over a particular time
// frame.
func WaitInvariant(statement func() bool, timeout time.Duration) error {
const pollInterval = 20 * time.Millisecond
exitTimer := time.After(timeout)
for {
<-time.After(pollInterval)
// Fail if the invariant is broken while polling.
if !statement() {
return fmt.Errorf("invariant broken before time out")
}
select {
case <-exitTimer:
return nil
default:
}
}
}
// DumpLogs reads the current logs generated by the passed node, and returns
// the logs as a single string. This function is useful for examining the logs
// of a particular node in the case of a test failure.
@@ -1373,7 +1302,7 @@ func (n *NetworkHarness) sendCoins(ctx context.Context, amt btcutil.Amount,
// Now, wait for ListUnspent to show the unconfirmed transaction
// containing the correct pkscript.
err = WaitNoError(func() error {
err = wait.NoError(func() error {
// Since neutrino doesn't support unconfirmed outputs, skip
// this check.
if target.cfg.BackendCfg.Name() == "neutrino" {