Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions assert/assertions.go
Comment thread
dolmen marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -1931,25 +1931,31 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
}

ch := make(chan bool, 1)
checkCond := func() { ch <- condition() }

timer := time.NewTimer(waitFor)
defer timer.Stop()

ticker := time.NewTicker(tick)
defer ticker.Stop()

for tick := ticker.C; ; {
var tickC <-chan time.Time

// Check the condition once first on the initial call.
go checkCond()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brackendawson I just got impacted in some of our test cases, because the behaviour changes a little here: the checkCond function was only evaluated after the first tick interval, but that is no longer the case. not sure if that is worth opening an issue though, it also makes sense to check immediately.

(in our case your change pinpointed a race condition in some kubernetes ingress / contour / envoy reconfiguration that we were not aware of 😅 )

Copy link
Copy Markdown

@OGoodness OGoodness Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is causing issues with our eventually check as well. The full time isn't being respected. We have a tick of 100ms and a time of 1 second, but only 100 ms is being used.
The same test works on the v1.10.0 major version but fails on the v1.11.0 update, supposedly due to this PR.

Alternatively, this could be an issue with the assert function, so this could be a red herring.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the behaviour changes a little here: the checkCond function was only evaluated after the first tick interval, but that is no longer the case

That's precisely what the PR implements, so it's very much on purpose, it helps speed up tests in the happy path
I was pushing for this change while expecting it to break some of my tests (which it did), but it only revealed some logic errors or races, so it's beneficial anyway

The full time isn't being respected. We have a tick of 100ms and a time of 1 second, but only 100 ms is being used.

Do you mean the check function only runs once and not every 100ms ? Is it blocking maybe ?


for {
select {
case <-timer.C:
return Fail(t, "Condition never satisfied", msgAndArgs...)
case <-tick:
tick = nil
go func() { ch <- condition() }()
case <-tickC:
tickC = nil
go checkCond()
case v := <-ch:
if v {
return true
}
tick = ticker.C
tickC = ticker.C
}
}
}
Expand Down Expand Up @@ -2019,35 +2025,42 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
var lastFinishedTickErrs []error
ch := make(chan *CollectT, 1)

checkCond := func() {
collect := new(CollectT)
defer func() {
ch <- collect
}()
condition(collect)
}

timer := time.NewTimer(waitFor)
defer timer.Stop()

ticker := time.NewTicker(tick)
defer ticker.Stop()

for tick := ticker.C; ; {
var tickC <-chan time.Time

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

for {
select {
case <-timer.C:
for _, err := range lastFinishedTickErrs {
t.Errorf("%v", err)
}
return Fail(t, "Condition never satisfied", msgAndArgs...)
case <-tick:
tick = nil
go func() {
collect := new(CollectT)
defer func() {
ch <- collect
}()
condition(collect)
}()
case <-tickC:
tickC = nil
go checkCond()
case collect := <-ch:
if !collect.failed() {
return true
}
// Keep the errors from the last ended condition, so that they can be copied to t if timeout is reached.
lastFinishedTickErrs = collect.errors
tick = ticker.C
tickC = ticker.C
}
}
}
Expand All @@ -2062,25 +2075,31 @@ func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.D
}

ch := make(chan bool, 1)
checkCond := func() { ch <- condition() }

timer := time.NewTimer(waitFor)
defer timer.Stop()

ticker := time.NewTicker(tick)
defer ticker.Stop()

for tick := ticker.C; ; {
var tickC <-chan time.Time

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

for {
select {
case <-timer.C:
return true
case <-tick:
tick = nil
go func() { ch <- condition() }()
case <-tickC:
tickC = nil
go checkCond()
case v := <-ch:
if v {
return Fail(t, "Condition satisfied", msgAndArgs...)
}
tick = ticker.C
tickC = ticker.C
}
}
}
Expand Down
67 changes: 48 additions & 19 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2980,6 +2980,49 @@ func TestEventuallyWithTFailNow(t *testing.T) {
Len(t, mockT.errors, 1)
}

// Check that a long running condition doesn't block Eventually.
// See issue 805 (and its long tail of following issues)
func TestEventuallyTimeout(t *testing.T) {
Comment on lines +2983 to +2985
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't new; I just moved it so that all of the Eventually* tests are grouped together rather than having the Never* tests in between them

mockT := new(testing.T)

NotPanics(t, func() {
done, done2 := make(chan struct{}), make(chan struct{})

// A condition function that returns after the Eventually timeout
condition := func() bool {
// Wait until Eventually times out and terminates
<-done
close(done2)
return true
}

False(t, Eventually(mockT, condition, time.Millisecond, time.Microsecond))

close(done)
<-done2
})
}

func TestEventuallySucceedQuickly(t *testing.T) {
mockT := new(testing.T)

condition := func() bool { return true }

// By making the tick longer than the total duration, we expect that this test would fail if
// we didn't check the condition before the first tick elapses.
True(t, Eventually(mockT, condition, 100*time.Millisecond, time.Second))
}

func TestEventuallyWithTSucceedQuickly(t *testing.T) {
mockT := new(testing.T)

condition := func(t *CollectT) {}

// By making the tick longer than the total duration, we expect that this test would fail if
// we didn't check the condition before the first tick elapses.
True(t, EventuallyWithT(mockT, condition, 100*time.Millisecond, time.Second))
}

func TestNeverFalse(t *testing.T) {
condition := func() bool {
return false
Expand Down Expand Up @@ -3007,27 +3050,13 @@ func TestNeverTrue(t *testing.T) {
False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
}

// Check that a long running condition doesn't block Eventually.
// See issue 805 (and its long tail of following issues)
func TestEventuallyTimeout(t *testing.T) {
func TestNeverFailQuickly(t *testing.T) {
mockT := new(testing.T)

NotPanics(t, func() {
done, done2 := make(chan struct{}), make(chan struct{})

// A condition function that returns after the Eventually timeout
condition := func() bool {
// Wait until Eventually times out and terminates
<-done
close(done2)
return true
}

False(t, Eventually(mockT, condition, time.Millisecond, time.Microsecond))

close(done)
<-done2
})
// By making the tick longer than the total duration, we expect that this test would fail if
// we didn't check the condition before the first tick elapses.
condition := func() bool { return true }
False(t, Never(mockT, condition, 100*time.Millisecond, time.Second))
}

func Test_validateEqualArgs(t *testing.T) {
Expand Down