Fix panic for Eventually functions

Fixes #805, Fixes #835
pull/629/merge
Jacek Szwec 2019-08-29 19:36:06 -04:00 committed by George Lesica
parent a88bf7aab8
commit f1bd0923b8
2 changed files with 22 additions and 11 deletions

View File

@ -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
}
}
}

View File

@ -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))
})
}