aboutsummaryrefslogtreecommitdiff
path: root/socket_test.cpp
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 05:19:53 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 05:19:53 +0000
commit7a9fb41030e1e2c83a6f8e7a93d588eddfe3aec4 (patch)
tree26c4570f6cc8f5ee36233bd88a2b1236e65a8f20 /socket_test.cpp
parent5979bcb5027a90f89acb289aefbbbfb51e0d1b4b (diff)
parentc362e05a76d7238f684234fe4ad6768c0c461b8d (diff)
downloadadb-7a9fb41030e1e2c83a6f8e7a93d588eddfe3aec4.tar.gz
Change-Id: I5b8c69036ab58b779643c014c99e7d400ae32fab
Diffstat (limited to 'socket_test.cpp')
-rw-r--r--socket_test.cpp44
1 files changed, 25 insertions, 19 deletions
diff --git a/socket_test.cpp b/socket_test.cpp
index 1601ff0a..f1b64b7a 100644
--- a/socket_test.cpp
+++ b/socket_test.cpp
@@ -33,6 +33,7 @@
#include "socket.h"
#include "sysdeps.h"
#include "sysdeps/chrono.h"
+#include "test_utils/test_utils.h"
using namespace std::string_literals;
using namespace std::string_view_literals;
@@ -99,7 +100,7 @@ TEST_F(LocalSocketTest, smoke) {
// Wait until the local sockets are closed.
WaitForFdeventLoop();
- ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ ASSERT_EQ(0u, fdevent_installed_count());
TerminateThread();
}
@@ -110,7 +111,7 @@ struct CloseWithPacketArg {
};
static void CreateCloser(CloseWithPacketArg* arg) {
- fdevent_run_on_main_thread([arg]() {
+ fdevent_run_on_looper([arg]() {
asocket* s = create_local_socket(std::move(arg->socket_fd));
ASSERT_TRUE(s != nullptr);
arg->bytes_written = 0;
@@ -125,6 +126,8 @@ static void CreateCloser(CloseWithPacketArg* arg) {
data.resize(MAX_PAYLOAD);
arg->bytes_written += data.size();
int ret = s->enqueue(s, std::move(data));
+
+ // Return value of 0 implies that more data can be accepted.
if (ret == 1) {
socket_filled = true;
break;
@@ -163,11 +166,11 @@ TEST_F(LocalSocketTest, close_socket_with_packet) {
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
WaitForFdeventLoop();
- EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ EXPECT_EQ(1u, fdevent_installed_count());
ASSERT_EQ(0, adb_close(socket_fd[0]));
WaitForFdeventLoop();
- ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ ASSERT_EQ(0u, fdevent_installed_count());
TerminateThread();
}
@@ -188,16 +191,18 @@ TEST_F(LocalSocketTest, read_from_closing_socket) {
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
WaitForFdeventLoop();
- EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ EXPECT_EQ(1u, fdevent_installed_count());
// Verify if we can read successfully.
std::vector<char> buf(arg.bytes_written);
ASSERT_NE(0u, arg.bytes_written);
- ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
+
+ ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); // TODO: b/237341044
+
ASSERT_EQ(0, adb_close(socket_fd[0]));
WaitForFdeventLoop();
- ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ ASSERT_EQ(0u, fdevent_installed_count());
TerminateThread();
}
@@ -218,13 +223,13 @@ TEST_F(LocalSocketTest, write_error_when_having_packets) {
CreateCloser(&arg);
WaitForFdeventLoop();
- EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ EXPECT_EQ(2u, fdevent_installed_count());
ASSERT_EQ(0, adb_close(socket_fd[0]));
std::this_thread::sleep_for(2s);
WaitForFdeventLoop();
- ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ ASSERT_EQ(0u, fdevent_installed_count());
TerminateThread();
}
@@ -261,15 +266,15 @@ TEST_F(LocalSocketTest, flush_after_shutdown) {
adb_close(tail_fd[0]);
WaitForFdeventLoop();
- ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ ASSERT_EQ(0u, fdevent_installed_count());
TerminateThread();
}
#if defined(__linux__)
-static void ClientThreadFunc() {
+static void ClientThreadFunc(const int assigned_port) {
std::string error;
- int fd = network_loopback_client(5038, SOCK_STREAM, &error);
+ const int fd = network_loopback_client(assigned_port, SOCK_STREAM, &error);
ASSERT_GE(fd, 0) << error;
std::this_thread::sleep_for(1s);
ASSERT_EQ(0, adb_close(fd));
@@ -278,29 +283,30 @@ static void ClientThreadFunc() {
// This test checks if we can close sockets in CLOSE_WAIT state.
TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
std::string error;
- int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
- ASSERT_GE(listen_fd, 0);
+ // Allow the system to allocate an available port.
+ unique_fd listen_fd;
+ const int assigned_port(test_utils::GetUnassignedPort(listen_fd));
- std::thread client_thread(ClientThreadFunc);
+ std::thread client_thread(ClientThreadFunc, assigned_port);
+ const int accept_fd = adb_socket_accept(listen_fd.get(), nullptr, nullptr);
- int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
ASSERT_GE(accept_fd, 0);
PrepareThread();
- fdevent_run_on_main_thread([accept_fd]() {
+ fdevent_run_on_looper([accept_fd]() {
asocket* s = create_local_socket(unique_fd(accept_fd));
ASSERT_TRUE(s != nullptr);
});
WaitForFdeventLoop();
- EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ EXPECT_EQ(1u, fdevent_installed_count());
// Wait until the client closes its socket.
client_thread.join();
WaitForFdeventLoop();
- ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ ASSERT_EQ(0u, fdevent_installed_count());
TerminateThread();
}