lntest/harness: adds WaitInvariant helper method

This commits adds a complement to the existing WaitPredicate
helper function, ensuring that a boolean statement remains
true for the duration of the provided timeout. This expands
our ability to do simple sanity checks where the wait-until
semantics of WaitPredicate may not be as-well suited.
This commit is contained in:
Conner Fromknecht 2018-03-13 00:09:49 -07:00
parent 72dcf44246
commit 82c1e9af92
No known key found for this signature in database
GPG Key ID: 39DE78FBE6ACB0EF
1 changed files with 26 additions and 0 deletions

View File

@ -846,6 +846,32 @@ func WaitPredicate(pred func() bool, timeout time.Duration) error {
}
}
// 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.