From 72147fe1e5a6c4666f8bd3bff04ee62e8ab0cfcd Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 21 Dec 2017 11:33:28 +0100 Subject: [PATCH] lntest: add new WaitPredicate helper function --- lntest/harness.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lntest/harness.go b/lntest/harness.go index c5186955..ab2537e7 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -773,11 +773,24 @@ func (n *NetworkHarness) AssertChannelExists(ctx context.Context, for _, channel := range resp.Channels { if channel.ChannelPoint == chanPoint.String() { +// 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 { + exitTimer := time.After(timeout) + for { + select { + case <-exitTimer: + return fmt.Errorf("predicate not satisfied after time out") + default: + } + + if pred() { return nil } } - - return fmt.Errorf("channel not found") } // DumpLogs reads the current logs generated by the passed node, and returns