From 0ce8989a785d03240028bf56d79c11f182842893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Fri, 14 Mar 2025 12:24:02 +0100 Subject: [PATCH] test/cli-plugins: Try to make TestConnectAndWait less flaky MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add runtime.Gosched() calls to encourage goroutine scheduling - Increase the timeout from 10ms to 500ms - Use poll.WaitOn with appropriate delays to ensure the goroutine has spawned before checking - Lock the test goroutines to its own thread Signed-off-by: Paweł Gronowski --- cli-plugins/socket/socket_test.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cli-plugins/socket/socket_test.go b/cli-plugins/socket/socket_test.go index 7d786abfc6..7938766b88 100644 --- a/cli-plugins/socket/socket_test.go +++ b/cli-plugins/socket/socket_test.go @@ -178,24 +178,39 @@ 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) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() srv, err := NewPluginServer(nil) assert.NilError(t, err, "failed to setup server") defer srv.Close() t.Setenv(EnvKey, srv.Addr().String()) + + runtime.Gosched() numGoroutines := runtime.NumGoroutine() ConnectAndWait(func() {}) - assert.Equal(t, runtime.NumGoroutine(), numGoroutines+1) + + runtime.Gosched() + poll.WaitOn(t, func(t poll.LogT) poll.Result { + // +1 goroutine for the poll.WaitOn + // +1 goroutine for the connect goroutine + if runtime.NumGoroutine() < numGoroutines+1+1 { + return poll.Continue("waiting for connect goroutine to spawn") + } + return poll.Success() + }, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(500*time.Millisecond)) srv.Close() + runtime.Gosched() poll.WaitOn(t, func(t poll.LogT) poll.Result { + // +1 goroutine for the poll.WaitOn 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)) + }, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(500*time.Millisecond)) }) }