From ce9a64c8f4524e6f339f48b1892374c0a5e1580e Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Wed, 12 Mar 2025 14:34:13 +0100 Subject: [PATCH] test: fix flaky TestConnectAndWait/connect_goroutine_exits_after_EOF Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> --- cli-plugins/socket/socket_test.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/cli-plugins/socket/socket_test.go b/cli-plugins/socket/socket_test.go index 7d786abfc6f9..edbfe22832bd 100644 --- a/cli-plugins/socket/socket_test.go +++ b/cli-plugins/socket/socket_test.go @@ -6,7 +6,6 @@ import ( "io/fs" "net" "os" - "runtime" "strings" "sync/atomic" "testing" @@ -175,8 +174,6 @@ func TestConnectAndWait(t *testing.T) { } }) - // TODO: this test cannot be executed with `t.Parallel()`, due to - // relying on goroutine numbers to ensure correct behaviour t.Run("connect goroutine exits after EOF", func(t *testing.T) { srv, err := NewPluginServer(nil) assert.NilError(t, err, "failed to setup server") @@ -184,18 +181,18 @@ func TestConnectAndWait(t *testing.T) { defer srv.Close() t.Setenv(EnvKey, srv.Addr().String()) - numGoroutines := runtime.NumGoroutine() - ConnectAndWait(func() {}) - assert.Equal(t, runtime.NumGoroutine(), numGoroutines+1) + ch := make(chan struct{}) + ConnectAndWait(func() { + close(ch) + }) srv.Close() - poll.WaitOn(t, func(t poll.LogT) poll.Result { - if runtime.NumGoroutine() > numGoroutines+1 { - return poll.Continue("waiting for connect goroutine to exit") - } - return poll.Success() - }, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(10*time.Millisecond)) + select { + case <-ch: + case <-time.After(1 * time.Second): + t.Fatal("callback not called after 1s") + } }) }