From 2407d7c3d936eb9649d2e1614f440f89bf9d5bed Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Mon, 25 Apr 2016 19:48:19 -0700 Subject: [PATCH] adb: fix socket tests. Bug: 28386497 Change-Id: I9f37ecadf3cfc6bb4e5da502d40f49e1f3a16d8a --- adb/fdevent_test.h | 10 ++++++++++ adb/socket_test.cpp | 34 ++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/adb/fdevent_test.h b/adb/fdevent_test.h index c853bceec..ef65b747f 100644 --- a/adb/fdevent_test.h +++ b/adb/fdevent_test.h @@ -49,6 +49,16 @@ class FdeventTest : public ::testing::Test { dummy = dummy_fds[0]; } + size_t GetAdditionalLocalSocketCount() { +#if ADB_HOST + // dummy socket installed in PrepareThread() + return 1; +#else + // dummy socket and one more socket installed in fdevent_subproc_setup() + return 2; +#endif + } + void TerminateThread(adb_thread_t thread) { fdevent_terminate_loop(); ASSERT_TRUE(WriteFdExactly(dummy, "", 1)); diff --git a/adb/socket_test.cpp b/adb/socket_test.cpp index 20a3bbbfc..d2ce2d875 100644 --- a/adb/socket_test.cpp +++ b/adb/socket_test.cpp @@ -44,6 +44,8 @@ static void FdEventThreadFunc(void*) { fdevent_loop(); } +const size_t SLEEP_FOR_FDEVENT_IN_MS = 100; + TEST_F(LocalSocketTest, smoke) { // Join two socketpairs with a chain of intermediate socketpairs. int first[2]; @@ -99,7 +101,8 @@ TEST_F(LocalSocketTest, smoke) { ASSERT_EQ(0, adb_close(last[1])); // Wait until the local sockets are closed. - adb_sleep_ms(100); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -151,12 +154,13 @@ TEST_F(LocalSocketTest, close_socket_with_packet) { ASSERT_TRUE(adb_thread_create(reinterpret_cast(CloseWithPacketThreadFunc), &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(100); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); ASSERT_EQ(0, adb_close(cause_close_fd[0])); - adb_sleep_ms(100); - EXPECT_EQ(2u, fdevent_installed_count()); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(0, adb_close(socket_fd[0])); - + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -175,10 +179,10 @@ TEST_F(LocalSocketTest, read_from_closing_socket) { ASSERT_TRUE(adb_thread_create(reinterpret_cast(CloseWithPacketThreadFunc), &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(100); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); ASSERT_EQ(0, adb_close(cause_close_fd[0])); - adb_sleep_ms(100); - EXPECT_EQ(2u, fdevent_installed_count()); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); // Verify if we can read successfully. std::vector buf(arg.bytes_written); @@ -186,6 +190,8 @@ TEST_F(LocalSocketTest, read_from_closing_socket) { ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); ASSERT_EQ(0, adb_close(socket_fd[0])); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -208,10 +214,12 @@ TEST_F(LocalSocketTest, write_error_when_having_packets) { &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(100); - EXPECT_EQ(3u, fdevent_installed_count()); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(0, adb_close(socket_fd[0])); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -260,12 +268,14 @@ TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(100); - EXPECT_EQ(2u, fdevent_installed_count()); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); // Wait until the client closes its socket. ASSERT_TRUE(adb_thread_join(client_thread)); + adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); }