From 7d839a4966ac7fba5da7c2b03ceb6f8956362dfe Mon Sep 17 00:00:00 2001 From: cszczepaniak Date: Sun, 30 Jul 2023 12:15:23 -0500 Subject: [PATCH 1/2] report errors properly in EventuallyWithT --- assert/assertions.go | 11 ++++++++++- assert/assertions_test.go | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index 6ab0ec347..0164bd5d4 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1921,13 +1921,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) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 162c71801..436e97d02 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -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)) From a54266dd32bfca7cdb700e80267e8fe6c42c49b5 Mon Sep 17 00:00:00 2001 From: cszczepaniak Date: Sun, 30 Jul 2023 12:18:30 -0500 Subject: [PATCH 2/2] deprecate CollectT.Copy --- assert/assertions.go | 1 + 1 file changed, 1 insertion(+) diff --git a/assert/assertions.go b/assert/assertions.go index 0164bd5d4..81d9a2826 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1879,6 +1879,7 @@ func (c *CollectT) Reset() { c.errors = nil } +// Deprecated: Copy should not be used. // Copy copies the collected errors to the supplied t. func (c *CollectT) Copy(t TestingT) { if tt, ok := t.(tHelper); ok {