From e8837d5396cf9194344f16c051216546a991166c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Wed, 5 Jul 2023 22:56:35 +0200 Subject: [PATCH] assert: fix TestEventuallyTimeout Fix TestEventuallyIssue805 which was flaky because of the use of a timer that has exactly the same duration as the Eventually timout. But time is not a synchronization primitive. Now we use channels to ensure that the condition is still running and Eventually times out before checking its return value. The test is also renamed to TestEventuallyTimeout to more clearly show its purpose. --- assert/assertions_test.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 9a44f95..6ce6d72 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2870,12 +2870,31 @@ func TestNeverTrue(t *testing.T) { False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)) } -func TestEventuallyIssue805(t *testing.T) { +// 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) { mockT := new(testing.T) NotPanics(t, func() { - condition := func() bool { <-time.After(time.Millisecond); return true } + t.Log("start") + done, done2 := make(chan struct{}), make(chan struct{}) + + // A condition function that returns after the Eventually timeout + condition := func() bool { + t.Log("ok1") + // Wait until Eventually times out and terminates + <-done + close(done2) + t.Log("ok2") + return true + } + False(t, Eventually(mockT, condition, time.Millisecond, time.Microsecond)) + + t.Log("Eventually done") + close(done) + <-done2 + t.Log("Test done") }) }