From d3750cd13750be66e6cad7ca852552303b5384c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicent=20Mart=C3=AD?= <42793+vmg@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:13:52 +0100 Subject: [PATCH] connpool: fix racy test (#14731) Signed-off-by: Vicent Marti --- go/pools/smartconnpool/pool_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/go/pools/smartconnpool/pool_test.go b/go/pools/smartconnpool/pool_test.go index c9c2235d90f..9a9fb9500b6 100644 --- a/go/pools/smartconnpool/pool_test.go +++ b/go/pools/smartconnpool/pool_test.go @@ -149,7 +149,7 @@ func TestOpen(t *testing.T) { } // Test that Get waits - ch := make(chan bool) + done := make(chan struct{}) go func() { for i := 0; i < 5; i++ { if i%2 == 0 { @@ -163,14 +163,16 @@ func TestOpen(t *testing.T) { for i := 0; i < 5; i++ { p.put(resources[i]) } - ch <- true + close(done) }() for i := 0; i < 5; i++ { - // Sleep to ensure the goroutine waits - time.Sleep(10 * time.Millisecond) + // block until we have a client wait for a connection, then offer it + for p.wait.waiting() == 0 { + time.Sleep(time.Millisecond) + } p.put(resources[i]) } - <-ch + <-done assert.EqualValues(t, 5, p.Metrics.WaitCount()) assert.Equal(t, 5, len(state.waits)) // verify start times are monotonic increasing