diff --git a/assert/assertions.go b/assert/assertions.go index 0e77a00..bc8703c 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1503,24 +1503,26 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t h.Helper() } + ch := make(chan bool, 1) + timer := time.NewTimer(waitFor) - ticker := time.NewTicker(tick) - checkPassed := make(chan bool) defer timer.Stop() + + ticker := time.NewTicker(tick) defer ticker.Stop() - defer close(checkPassed) - for { + + for tick := ticker.C; ; { select { case <-timer.C: - return Fail(t, "Condition never satisfied", msgAndArgs...) - case result := <-checkPassed: - if result { + return false + case <-tick: + tick = nil + go func() { ch <- condition() }() + case v := <-ch: + if v { return true } - case <-ticker.C: - go func() { - checkPassed <- condition() - }() + tick = ticker.C } } } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 3023a14..93b78c4 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -1985,3 +1985,12 @@ func TestEventuallyTrue(t *testing.T) { True(t, Eventually(t, condition, 100*time.Millisecond, 20*time.Millisecond)) } + +func TestEventuallyIssue805(t *testing.T) { + mockT := new(testing.T) + + NotPanics(t, func() { + condition := func() bool { <-time.After(time.Millisecond); return true } + False(t, Eventually(mockT, condition, time.Millisecond, time.Microsecond)) + }) +}