From 0714ffa0d7d2c34c092cc9b9c335759670762a10 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 22 Jan 2019 13:38:05 +0100 Subject: [PATCH] lntest/harness: add WaitNoError --- lntest/harness.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lntest/harness.go b/lntest/harness.go index 70ab825c..bec7eb4c 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -1155,6 +1155,28 @@ func WaitPredicate(pred func() bool, timeout time.Duration) error { } } +// 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