| 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 | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 19 | #include <condition_variable> | 
|  | 20 | #include <mutex> | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 21 | #include <thread> | 
|  | 22 |  | 
| Josh Gao | 7badb33 | 2018-10-19 15:24:53 -0700 | [diff] [blame] | 23 | #include "adb_io.h" | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 24 | #include "adb_unique_fd.h" | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 25 | #include "socket.h" | 
|  | 26 | #include "sysdeps.h" | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 27 | #include "sysdeps/chrono.h" | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 28 |  | 
| Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 29 | static void WaitForFdeventLoop() { | 
|  | 30 | // Sleep for a bit to make sure that network events have propagated. | 
|  | 31 | std::this_thread::sleep_for(100ms); | 
|  | 32 |  | 
|  | 33 | // fdevent_run_on_main_thread has a guaranteed ordering, and is guaranteed to happen after | 
|  | 34 | // socket events, so as soon as our function is called, we know that we've processed all | 
|  | 35 | // previous events. | 
|  | 36 | std::mutex mutex; | 
|  | 37 | std::condition_variable cv; | 
|  | 38 | std::unique_lock<std::mutex> lock(mutex); | 
|  | 39 | fdevent_run_on_main_thread([&]() { | 
|  | 40 | mutex.lock(); | 
|  | 41 | mutex.unlock(); | 
|  | 42 | cv.notify_one(); | 
|  | 43 | }); | 
|  | 44 | cv.wait(lock); | 
|  | 45 | } | 
|  | 46 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 47 | class FdeventTest : public ::testing::Test { | 
|  | 48 | protected: | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 49 | unique_fd dummy; | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | static void SetUpTestCase() { | 
|  | 52 | #if !defined(_WIN32) | 
|  | 53 | ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN)); | 
|  | 54 | #endif | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | void SetUp() override { | 
|  | 58 | fdevent_reset(); | 
|  | 59 | ASSERT_EQ(0u, fdevent_installed_count()); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | // Register a dummy socket used to wake up the fdevent loop to tell it to die. | 
|  | 63 | void PrepareThread() { | 
|  | 64 | int dummy_fds[2]; | 
|  | 65 | if (adb_socketpair(dummy_fds) != 0) { | 
|  | 66 | FAIL() << "failed to create socketpair: " << strerror(errno); | 
|  | 67 | } | 
|  | 68 |  | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 69 | asocket* dummy_socket = create_local_socket(unique_fd(dummy_fds[1])); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 70 | if (!dummy_socket) { | 
|  | 71 | FAIL() << "failed to create local socket: " << strerror(errno); | 
|  | 72 | } | 
|  | 73 | dummy_socket->ready(dummy_socket); | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 74 | dummy.reset(dummy_fds[0]); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 75 |  | 
|  | 76 | thread_ = std::thread([]() { fdevent_loop(); }); | 
|  | 77 | WaitForFdeventLoop(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 80 | size_t GetAdditionalLocalSocketCount() { | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 81 | // dummy socket installed in PrepareThread() + fdevent_run_on_main_thread socket | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 82 | return 2; | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 85 | void TerminateThread() { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 86 | fdevent_terminate_loop(); | 
|  | 87 | ASSERT_TRUE(WriteFdExactly(dummy, "", 1)); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 88 | thread_.join(); | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 89 | dummy.reset(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 90 | } | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 91 |  | 
|  | 92 | std::thread thread_; | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 93 | }; |