tests: Deflake socket_inet_loopback_isolated_test

This test case creates one connection, then it closes one end of it. After that
it waits for a linger timeout and tries to create the second connection with
the same client source port. The test is flaky if the second half of the fist
connection isn't closed before creating the second connection.

PiperOrigin-RevId: 715924851
This commit is contained in:
Andrei Vagin
2025-01-15 13:38:00 -08:00
committed by gVisor bot
parent 1864d9d091
commit ca3c23db50
2 changed files with 12 additions and 1 deletions
+5 -1
View File
@@ -295,7 +295,11 @@ func (n *Network) CreateLinksAndRoutes(args *CreateLinksAndRoutesArgs, _ *struct
nicID := n.Stack.NextNICID()
nicids[link.Name] = nicID
linkEP := ethernet.New(loopback.New())
var linkEP stack.LinkEndpoint
linkEP = ethernet.New(loopback.New())
if args.LogPackets {
linkEP = sniffer.New(linkEP)
}
log.Infof("Enabling loopback interface %q with id %d on addresses %+v", link.Name, nicID, link.Addresses)
opts := stack.NICOptions{
@@ -210,6 +210,13 @@ TEST_P(SocketInetLoopbackIsolatedTest, TCPFinWait2Test) {
ASSERT_THAT(bind(conn_fd2.get(), AsSockAddr(&conn_bound_addr), conn_addrlen),
SyscallSucceeds());
// Close the `accepted` end otherwise connect can return ECONNREFUSED.
constexpr int kTCPLingerTimeout0 = 0;
EXPECT_THAT(setsockopt(accepted.get(), IPPROTO_TCP, TCP_LINGER2,
&kTCPLingerTimeout0, sizeof(kTCPLingerTimeout0)),
SyscallSucceedsWithValue(0));
shutdown(accepted.get(), SHUT_WR);
ASSERT_THAT(
RetryEINTR(connect)(conn_fd2.get(), AsSockAddr(&conn_addr), conn_addrlen),
SyscallSucceeds());