1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <gtest/gtest.h> 18 19 #include <thread> 20 21 #include "socket.h" 22 #include "sysdeps.h" 23 24 class FdeventTest : public ::testing::Test { 25 protected: 26 int dummy = -1; 27 28 static void SetUpTestCase() { 29 #if !defined(_WIN32) 30 ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN)); 31 #endif 32 } 33 34 void SetUp() override { 35 fdevent_reset(); 36 ASSERT_EQ(0u, fdevent_installed_count()); 37 } 38 39 // Register a dummy socket used to wake up the fdevent loop to tell it to die. 40 void PrepareThread() { 41 int dummy_fds[2]; 42 if (adb_socketpair(dummy_fds) != 0) { 43 FAIL() << "failed to create socketpair: " << strerror(errno); 44 } 45 46 asocket* dummy_socket = create_local_socket(dummy_fds[1]); 47 if (!dummy_socket) { 48 FAIL() << "failed to create local socket: " << strerror(errno); 49 } 50 dummy_socket->ready(dummy_socket); 51 dummy = dummy_fds[0]; 52 } 53 54 size_t GetAdditionalLocalSocketCount() { 55 #if ADB_HOST 56 // dummy socket installed in PrepareThread() + fdevent_run_on_main_thread socket 57 return 2; 58 #else 59 // dummy socket + fdevent_run_on_main_thread + fdevent_subproc_setup() sockets 60 return 3; 61 #endif 62 } 63 64 void TerminateThread(std::thread& thread) { 65 fdevent_terminate_loop(); 66 ASSERT_TRUE(WriteFdExactly(dummy, "", 1)); 67 thread.join(); 68 ASSERT_EQ(0, adb_close(dummy)); 69 } 70 }; 71