blob: 8d853c369ea4bda1cc161fb36185d184d9d4fb6a [file] [log] [blame]
Josh Gao022d4472016-02-10 14:49:00 -08001/*
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 Gao7ab55712018-03-29 16:27:13 -070019#include <condition_variable>
20#include <mutex>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070021#include <thread>
22
Josh Gao7badb332018-10-19 15:24:53 -070023#include "adb_io.h"
Josh Gao022d4472016-02-10 14:49:00 -080024#include "socket.h"
25#include "sysdeps.h"
Josh Gao7ab55712018-03-29 16:27:13 -070026#include "sysdeps/chrono.h"
Josh Gao022d4472016-02-10 14:49:00 -080027
Josh Gaofa30bf32018-03-29 16:23:43 -070028static void WaitForFdeventLoop() {
29 // Sleep for a bit to make sure that network events have propagated.
30 std::this_thread::sleep_for(100ms);
31
32 // fdevent_run_on_main_thread has a guaranteed ordering, and is guaranteed to happen after
33 // socket events, so as soon as our function is called, we know that we've processed all
34 // previous events.
35 std::mutex mutex;
36 std::condition_variable cv;
37 std::unique_lock<std::mutex> lock(mutex);
38 fdevent_run_on_main_thread([&]() {
39 mutex.lock();
40 mutex.unlock();
41 cv.notify_one();
42 });
43 cv.wait(lock);
44}
45
Josh Gao022d4472016-02-10 14:49:00 -080046class FdeventTest : public ::testing::Test {
47 protected:
48 int dummy = -1;
49
50 static void SetUpTestCase() {
51#if !defined(_WIN32)
52 ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN));
53#endif
54 }
55
56 void SetUp() override {
57 fdevent_reset();
58 ASSERT_EQ(0u, fdevent_installed_count());
59 }
60
61 // Register a dummy socket used to wake up the fdevent loop to tell it to die.
62 void PrepareThread() {
63 int dummy_fds[2];
64 if (adb_socketpair(dummy_fds) != 0) {
65 FAIL() << "failed to create socketpair: " << strerror(errno);
66 }
67
68 asocket* dummy_socket = create_local_socket(dummy_fds[1]);
69 if (!dummy_socket) {
70 FAIL() << "failed to create local socket: " << strerror(errno);
71 }
72 dummy_socket->ready(dummy_socket);
73 dummy = dummy_fds[0];
Josh Gao7ab55712018-03-29 16:27:13 -070074
75 thread_ = std::thread([]() { fdevent_loop(); });
76 WaitForFdeventLoop();
Josh Gao022d4472016-02-10 14:49:00 -080077 }
78
Yabin Cui2407d7c2016-04-25 19:48:19 -070079 size_t GetAdditionalLocalSocketCount() {
Josh Gao4c936392017-05-03 14:10:39 -070080 // dummy socket installed in PrepareThread() + fdevent_run_on_main_thread socket
Yabin Cui2407d7c2016-04-25 19:48:19 -070081 return 2;
Yabin Cui2407d7c2016-04-25 19:48:19 -070082 }
83
Josh Gao7ab55712018-03-29 16:27:13 -070084 void TerminateThread() {
Josh Gao022d4472016-02-10 14:49:00 -080085 fdevent_terminate_loop();
86 ASSERT_TRUE(WriteFdExactly(dummy, "", 1));
Josh Gao7ab55712018-03-29 16:27:13 -070087 thread_.join();
Josh Gao022d4472016-02-10 14:49:00 -080088 ASSERT_EQ(0, adb_close(dummy));
89 }
Josh Gao7ab55712018-03-29 16:27:13 -070090
91 std::thread thread_;
Josh Gao022d4472016-02-10 14:49:00 -080092};