fix flaky TestDial test (#5029)

* 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.
This commit is contained in:
Marten Seemann
2025-04-12 10:39:47 +08:00
committed by GitHub
parent ef2b87f5d5
commit c9c2c86516

View File

@@ -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())