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