Skip to content

Commit 0213fa4

Browse files
committed
do not hang on failed condition
1 parent feb1324 commit 0213fa4

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

assert/assertions.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,15 +2010,30 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
20102010
h.Helper()
20112011
}
20122012

2013-
ch := make(chan bool, 1)
2014-
checkCond := func() { ch <- condition() }
2013+
const failed = 0
2014+
const stop = 1
2015+
const noStop = 2
2016+
2017+
resultCh := make(chan int, 1)
2018+
checkCond := func() {
2019+
result := failed
2020+
defer func() {
2021+
resultCh <- result
2022+
}()
2023+
if condition() {
2024+
result = stop
2025+
} else {
2026+
result = noStop
2027+
}
2028+
}
20152029

20162030
timer := time.NewTimer(waitFor)
20172031
defer timer.Stop()
20182032

20192033
ticker := time.NewTicker(tick)
20202034
defer ticker.Stop()
20212035

2036+
// Use a nillable channel to control ticks.
20222037
var tickC <-chan time.Time
20232038

20242039
// Check the condition once first on the initial call.
@@ -2029,13 +2044,23 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
20292044
case <-timer.C:
20302045
return Fail(t, "Condition never satisfied", msgAndArgs...)
20312046
case <-tickC:
2032-
tickC = nil
2033-
go checkCond()
2034-
case v := <-ch:
2035-
if v {
2047+
tickC = nil // Do not check again until we get a result.
2048+
go checkCond() // Schedule the next check.
2049+
case v := <-resultCh:
2050+
switch v {
2051+
case failed:
2052+
// Conditon pannicked or test failed and finished.
2053+
// Cannot determine correct result.
2054+
// Cannot decide if we should continue or not.
2055+
// Stop here and now, and mark test as failed.
2056+
return Fail(t, "Condition aborted")
2057+
case stop:
20362058
return true
2059+
case noStop:
2060+
fallthrough
2061+
default:
2062+
tickC = ticker.C // Enable ticks to check again.
20372063
}
2038-
tickC = ticker.C
20392064
}
20402065
}
20412066
}

0 commit comments

Comments
 (0)