Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report errors properly in EventuallyWithT #1438

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 11 additions & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,7 @@ func (c *CollectT) Reset() {
c.errors = nil
}

// Deprecated: Copy should not be used.
dolmen marked this conversation as resolved.
Show resolved Hide resolved
// Copy copies the collected errors to the supplied t.
func (c *CollectT) Copy(t TestingT) {
if tt, ok := t.(tHelper); ok {
Expand Down Expand Up @@ -1921,13 +1922,22 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
ticker := time.NewTicker(tick)
defer ticker.Stop()

var lastErrors []error

copyErrors := func(t TestingT) {
for _, err := range lastErrors {
t.Errorf("%v", err)
}
}

for tick := ticker.C; ; {
select {
case <-timer.C:
collect.Copy(t)
copyErrors(t)
return Fail(t, "Condition never satisfied", msgAndArgs...)
case <-tick:
tick = nil
lastErrors = collect.errors
collect.Reset()
go func() {
condition(collect)
Expand Down
5 changes: 5 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2763,8 +2763,13 @@ func TestEventuallyTrue(t *testing.T) {
func TestEventuallyWithTFalse(t *testing.T) {
mockT := new(CollectT)

var calledOnce bool
condition := func(collect *CollectT) {
if calledOnce {
time.Sleep(time.Second)
}
True(collect, false)
calledOnce = true
}

False(t, EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
Expand Down
Loading