Eventually asserts that given condition will be met in waitFor time, periodically checking target function each tick. assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond)
(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{})
| 1986 | // |
| 1987 | // assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond) |
| 1988 | func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { |
| 1989 | if h, ok := t.(tHelper); ok { |
| 1990 | h.Helper() |
| 1991 | } |
| 1992 | |
| 1993 | ch := make(chan bool, 1) |
| 1994 | checkCond := func() { ch <- condition() } |
| 1995 | |
| 1996 | timer := time.NewTimer(waitFor) |
| 1997 | defer timer.Stop() |
| 1998 | |
| 1999 | ticker := time.NewTicker(tick) |
| 2000 | defer ticker.Stop() |
| 2001 | |
| 2002 | var tickC <-chan time.Time |
| 2003 | |
| 2004 | // Check the condition once first on the initial call. |
| 2005 | go checkCond() |
| 2006 | |
| 2007 | for { |
| 2008 | select { |
| 2009 | case <-timer.C: |
| 2010 | return Fail(t, "Condition never satisfied", msgAndArgs...) |
| 2011 | case <-tickC: |
| 2012 | tickC = nil |
| 2013 | go checkCond() |
| 2014 | case v := <-ch: |
| 2015 | if v { |
| 2016 | return true |
| 2017 | } |
| 2018 | tickC = ticker.C |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | // CollectT implements the TestingT interface and collects all errors. |
| 2024 | type CollectT struct { |
searching dependent graphs…