From c9c2c86516396675b6e37fb012b525f4d021911e Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 12 Apr 2025 10:39:47 +0800 Subject: [PATCH] fix flaky TestDial test (#5029) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * give the kernel some time to free the socket * remove problematic assertion The listen function is started in a Goroutine, hence we can’t be sure it’s already started. --- client_test.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/client_test.go b/client_test.go index 8d5d9deb3..dbff79525 100644 --- a/client_test.go +++ b/client_test.go @@ -67,7 +67,6 @@ func testDial(t *testing.T, _, addr, err := server.ReadFrom(make([]byte, 1500)) require.NoError(t, err) - require.True(t, areTransportsRunning()) cancel() select { case err := <-errChan: @@ -76,19 +75,29 @@ func testDial(t *testing.T, t.Fatal("timeout") } - // The socket that the client used for dialing should be closed now. - // Binding to the same address would error if the address was still in use. - conn, err := net.ListenUDP("udp", addr.(*net.UDPAddr)) if shouldCloseConn { - require.NoError(t, err) - defer conn.Close() + // The socket that the client used for dialing should be closed now. + // Binding to the same address would error if the address was still in use. + require.Eventually(t, func() bool { + conn, err := net.ListenUDP("udp", addr.(*net.UDPAddr)) + if err != nil { + return false + } + conn.Close() + return true + }, scaleDuration(200*time.Millisecond), scaleDuration(10*time.Millisecond)) + require.False(t, areTransportsRunning()) + return + } + + // The socket that the client used for dialing should not be closed now. + // Binding to the same address will error if the address was still in use. + _, err = net.ListenUDP("udp", addr.(*net.UDPAddr)) + require.Error(t, err) + if runtime.GOOS == "windows" { + require.ErrorContains(t, err, "bind: Only one usage of each socket address") } else { - require.Error(t, err) - if runtime.GOOS == "windows" { - require.ErrorContains(t, err, "bind: Only one usage of each socket address") - } else { - require.ErrorContains(t, err, "address already in use") - } + require.ErrorContains(t, err, "address already in use") } require.False(t, areTransportsRunning())