Skip to content

Commit 2f881ed

Browse files
committed
fix(assert): fail Never when condition does not return before timeout
Track whether the condition completed at least once. If waitFor elapses before the first evaluation returns, report a failure instead of passing without checking. Fixes #1654
1 parent 12f8b56 commit 2f881ed

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

assert/assertions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,18 +2166,23 @@ func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.D
21662166
defer ticker.Stop()
21672167

21682168
var tickC <-chan time.Time
2169+
checked := false
21692170

21702171
// Check the condition once first on the initial call.
21712172
go checkCond()
21722173

21732174
for {
21742175
select {
21752176
case <-timer.C:
2177+
if !checked {
2178+
return Fail(t, "Condition never completed before timeout", msgAndArgs...)
2179+
}
21762180
return true
21772181
case <-tickC:
21782182
tickC = nil
21792183
go checkCond()
21802184
case v := <-ch:
2185+
checked = true
21812186
if v {
21822187
return Fail(t, "Condition satisfied", msgAndArgs...)
21832188
}

assert/assertions_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,6 +3634,18 @@ func TestNeverFailQuickly(t *testing.T) {
36343634
False(t, Never(mockT, condition, 100*time.Millisecond, time.Second))
36353635
}
36363636

3637+
func TestNeverTimeoutBeforeConditionReturns(t *testing.T) {
3638+
t.Parallel()
3639+
3640+
mockT := new(testing.T)
3641+
3642+
condition := func() bool {
3643+
time.Sleep(2 * time.Second)
3644+
return true
3645+
}
3646+
False(t, Never(mockT, condition, time.Millisecond, time.Second))
3647+
}
3648+
36373649
func Test_validateEqualArgs(t *testing.T) {
36383650
t.Parallel()
36393651

0 commit comments

Comments
 (0)