From 9d79e7687689a5fd1632acec3717bd404872053c Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 30 Nov 2021 18:50:25 +0800 Subject: [PATCH] itest: timeout while waiting for predicate result This commit sends the returned value of pred() inside a goroutine such that when a predicate function never returns during the first run, the wait package can timeout properly. --- lntest/wait/wait.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lntest/wait/wait.go b/lntest/wait/wait.go index 4bd8c7a24..16a5f2ab2 100644 --- a/lntest/wait/wait.go +++ b/lntest/wait/wait.go @@ -15,17 +15,26 @@ const PollInterval = 200 * time.Millisecond // some property is upheld within a particular time frame. func Predicate(pred func() bool, timeout time.Duration) error { exitTimer := time.After(timeout) + result := make(chan bool, 1) + for { <-time.After(PollInterval) + go func() { + result <- pred() + }() + + // Each time we call the pred(), we expect a result to be + // returned otherwise it will timeout. select { case <-exitTimer: - return fmt.Errorf("predicate not satisfied after time out") - default: - } + return fmt.Errorf("predicate not satisfied after " + + "time out") - if pred() { - return nil + case succeed := <-result: + if succeed { + return nil + } } } }