| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 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 |  | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 19 | #include <thread> | 
 | 20 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 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 |  | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 54 |     size_t GetAdditionalLocalSocketCount() { | 
 | 55 | #if ADB_HOST | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 56 |         // dummy socket installed in PrepareThread() + fdevent_run_on_main_thread socket | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 57 |         return 2; | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 58 | #else | 
 | 59 |         // dummy socket + fdevent_run_on_main_thread + fdevent_subproc_setup() sockets | 
 | 60 |         return 3; | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 61 | #endif | 
 | 62 |     } | 
 | 63 |  | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 64 |     void TerminateThread(std::thread& thread) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 65 |         fdevent_terminate_loop(); | 
 | 66 |         ASSERT_TRUE(WriteFdExactly(dummy, "", 1)); | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 67 |         thread.join(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 68 |         ASSERT_EQ(0, adb_close(dummy)); | 
 | 69 |     } | 
 | 70 | }; |