Skip to content

Commit 2e0758b

Browse files
Jeff-A-Martingvisor-bot
authored andcommitted
Internal change.
PiperOrigin-RevId: 827961623
1 parent 6d029e9 commit 2e0758b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

test/syscalls/linux/socket_inet_loopback_isolated.cc

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <netinet/tcp.h>
1616
#include <sys/socket.h>
1717

18+
#include <cerrno>
1819
#include <cstring>
1920

2021
#include "gmock/gmock.h"
@@ -205,12 +206,26 @@ TEST_P(SocketInetLoopbackIsolatedTest, TCPFinWait2Test) {
205206
ASSERT_THAT(bind(conn_fd2.get(), AsSockAddr(&conn_bound_addr), conn_addrlen),
206207
SyscallFailsWithErrno(EADDRINUSE));
207208

208-
// Sleep for a little over the linger timeout to reduce flakiness in
209-
// save/restore tests.
210-
absl::SleepFor(absl::Seconds(kTCPLingerTimeout + 2));
211-
212-
ASSERT_THAT(bind(conn_fd2.get(), AsSockAddr(&conn_bound_addr), conn_addrlen),
213-
SyscallSucceeds());
209+
// Sleep for the linger timeout to allow the FIN_WAIT2 timer to expire.
210+
absl::SleepFor(absl::Seconds(kTCPLingerTimeout));
211+
212+
// Verify that we can bind and connect to the address.
213+
// Try multiple times to make the test robust against timing variation.
214+
constexpr int kMaxAttempts = 5;
215+
int attempts = 0;
216+
while (true) {
217+
if (bind(conn_fd2.get(), AsSockAddr(&conn_bound_addr), conn_addrlen) >= 0) {
218+
break; // Bind Succeeded.
219+
}
220+
if (errno != EADDRINUSE) {
221+
FAIL() << "bind failed with unexpected errno: " << errno;
222+
}
223+
attempts++;
224+
if (attempts >= kMaxAttempts) {
225+
FAIL() << "bind failed after " << attempts << " attempts";
226+
}
227+
absl::SleepFor(absl::Seconds(1));
228+
}
214229

215230
// Close the `accepted` end otherwise connect can return ECONNREFUSED.
216231
constexpr int kTCPLingerTimeout0 = 0;

0 commit comments

Comments
 (0)