blob: 5a417e056faf765377ff941a423ccd4d326c022a [file] [log] [blame]
Josh Gao511763b2016-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 Gaoc7bdb6d2018-03-29 16:27:13 -070019#include <condition_variable>
20#include <mutex>
Josh Gao0f3312a2017-04-12 17:00:49 -070021#include <thread>
22
Josh Gao511763b2016-02-10 14:49:00 -080023#include "socket.h"
24#include "sysdeps.h"
Josh Gaoc7bdb6d2018-03-29 16:27:13 -070025#include "sysdeps/chrono.h"
Josh Gao511763b2016-02-10 14:49:00 -080026
Josh Gaod2c6b402018-03-29 16:23:43 -070027static 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 Gao511763b2016-02-10 14:49:00 -080045class 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 Gaoc7bdb6d2018-03-29 16:27:13 -070073
74 thread_ = std::thread([]() { fdevent_loop(); });
75 WaitForFdeventLoop();
Josh Gao511763b2016-02-10 14:49:00 -080076 }
77
Yabin Cui40a47272016-04-25 19:48:19 -070078 size_t GetAdditionalLocalSocketCount() {
Josh Gao764f8c52017-05-03 14:10:39 -070079 // dummy socket installed in PrepareThread() + fdevent_run_on_main_thread socket
Yabin Cui40a47272016-04-25 19:48:19 -070080 return 2;
Yabin Cui40a47272016-04-25 19:48:19 -070081 }
82
Josh Gaoc7bdb6d2018-03-29 16:27:13 -070083 void TerminateThread() {
Josh Gao511763b2016-02-10 14:49:00 -080084 fdevent_terminate_loop();
85 ASSERT_TRUE(WriteFdExactly(dummy, "", 1));
Josh Gaoc7bdb6d2018-03-29 16:27:13 -070086 thread_.join();
Josh Gao511763b2016-02-10 14:49:00 -080087 ASSERT_EQ(0, adb_close(dummy));
88 }
Josh Gaoc7bdb6d2018-03-29 16:27:13 -070089
90 std::thread thread_;
Josh Gao511763b2016-02-10 14:49:00 -080091};