From 1798c0029e63a7ebb6daa3ef28d9398df3fee422 Mon Sep 17 00:00:00 2001 From: Lee Avital Date: Wed, 11 Oct 2023 10:44:15 -0400 Subject: [PATCH] [NPM-3010] avoid potential flakes in TestConntrackExists (#20039) We saw a spike in failures for TestConntrackExists in which conntrack.Exists() returned false for UDP connections. My guess is this is because the UDP connections didn't make it all the way through the kernel before checking if the connection exists. This PR aims to solve the problem by having PingUDP wait for a response from the test UDP server to ensure the conntrack entry is initialized. --- pkg/network/testutil/ping.go | 5 +++++ pkg/network/testutil/server.go | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/network/testutil/ping.go b/pkg/network/testutil/ping.go index 20d23c216e250b..830613b1dd616a 100644 --- a/pkg/network/testutil/ping.go +++ b/pkg/network/testutil/ping.go @@ -65,6 +65,11 @@ func PingUDP(tb require.TestingT, ip net.IP, port int) net.Conn { return nil } + bs := make([]byte, 10) + n, err := conn.Read(bs) + require.NoError(tb, err) + require.Equal(tb, []byte("pong"), bs[:n]) + return conn } diff --git a/pkg/network/testutil/server.go b/pkg/network/testutil/server.go index 8dc22615de6869..0fdc9a561113cc 100644 --- a/pkg/network/testutil/server.go +++ b/pkg/network/testutil/server.go @@ -110,14 +110,19 @@ func StartServerUDP(t *testing.T, ip net.IP, port int) io.Closer { Port: port, } - l, err := net.ListenUDP(network, addr) + udpConn, err := net.ListenUDP(network, addr) require.NoError(t, err) go func() { close(ch) for { bs := make([]byte, 10) - _, err := l.Read(bs) + _, addr, err := udpConn.ReadFrom(bs) + if err != nil { + return + } + + _, err = udpConn.WriteTo([]byte("pong"), addr) if err != nil { return } @@ -132,5 +137,5 @@ func StartServerUDP(t *testing.T, ip net.IP, port int) io.Closer { } }, 3*time.Second, 10*time.Millisecond, "timed out waiting for UDP server to come up") - return l + return udpConn }