Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ _testmain.go

.DS_Store

.idea

# Output of "go test -c"
/assert/assert.test
/require/require.test
Expand Down
8 changes: 6 additions & 2 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -1989,9 +1990,9 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
if h, ok := t.(tHelper); ok {
h.Helper()
}

ch := make(chan bool, 1)
checkCond := func() { ch <- condition() }
wg := &sync.WaitGroup{}
checkCond := func() { defer wg.Done(); ch <- condition() }

timer := time.NewTimer(waitFor)
defer timer.Stop()
Expand All @@ -2002,14 +2003,17 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
var tickC <-chan time.Time

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

for {
select {
case <-timer.C:
wg.Wait()
return Fail(t, "Condition never satisfied", msgAndArgs...)
case <-tickC:
tickC = nil
wg.Add(1)
go checkCond()
case v := <-ch:
if v {
Expand Down
19 changes: 19 additions & 0 deletions require/requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,22 @@ func TestEventuallyWithTTrue(t *testing.T) {
False(t, mockT.Failed, "Check should pass")
Equal(t, 2, counter, "Condition is expected to be called 2 times")
}

func TestEventuallyTimeout(t *testing.T) {
t.Parallel()

mockT := new(MockT)

counter := 0
condition := func() bool {
counter += 1
// this is here to check for data race conditions.
time.Sleep(20 * time.Millisecond)
return false
}
//Done to check for data race
counter += 1
Eventually(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)
True(t, mockT.Failed, "check should fail")
Greater(t, counter, 2, "we should have more than one iteration")
}
Loading