From cd26798582d7ea98599a64c261a67a72c75350a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 10 Jul 2023 23:15:49 +0200 Subject: [PATCH] assert: fix flaky TestNeverTrue Fix flaky TestNeverTrue: use a channel to make a synchronized list of return values to avoid a race condition. --- assert/assertions_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 162c71801..101bd418a 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2794,14 +2794,20 @@ func TestNeverFalse(t *testing.T) { True(t, Never(t, condition, 100*time.Millisecond, 20*time.Millisecond)) } +// TestNeverTrue checks Never with a condition that returns true on second call. func TestNeverTrue(t *testing.T) { mockT := new(testing.T) - state := 0 + + // A list of values returned by condition. + // Channel protects against concurrent access. + returns := make(chan bool, 2) + returns <- false + returns <- true + defer close(returns) + + // Will return true on second call. condition := func() bool { - defer func() { - state = state + 1 - }() - return state == 2 + return <-returns } False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))