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