From 82c1e9af922853a555156da4a7ddde95e00b2b78 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 13 Mar 2018 00:09:49 -0700 Subject: [PATCH] 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. --- lntest/harness.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lntest/harness.go b/lntest/harness.go index 03ea96ee..858a4cd6 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -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.