blob: 2f0ff180503f91b901969580d25e23a20c998c4d [file] [log] [blame]
Yabin Cuibec02fc2015-08-28 15:44:27 -07001/*
2 * Copyright (C) 2015 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 "fdevent.h"
18
19#include <gtest/gtest.h>
20
Yabin Cuia1080162015-09-04 16:19:56 -070021#include <limits>
Yabin Cuibec02fc2015-08-28 15:44:27 -070022#include <queue>
23#include <string>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070024#include <thread>
Yabin Cuibec02fc2015-08-28 15:44:27 -070025#include <vector>
26
Yabin Cuibec02fc2015-08-28 15:44:27 -070027#include "adb_io.h"
Josh Gao022d4472016-02-10 14:49:00 -080028#include "fdevent_test.h"
Josh Gao2e1e7892018-03-23 13:03:28 -070029#include "sysdeps/memory.h"
Yabin Cuibec02fc2015-08-28 15:44:27 -070030
Yabin Cuibec02fc2015-08-28 15:44:27 -070031class FdHandler {
32 public:
33 FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) {
34 fdevent_install(&read_fde_, read_fd_, FdEventCallback, this);
Yabin Cuia1080162015-09-04 16:19:56 -070035 fdevent_add(&read_fde_, FDE_READ);
Yabin Cuibec02fc2015-08-28 15:44:27 -070036 fdevent_install(&write_fde_, write_fd_, FdEventCallback, this);
Yabin Cuia1080162015-09-04 16:19:56 -070037 }
38
39 ~FdHandler() {
40 fdevent_remove(&read_fde_);
41 fdevent_remove(&write_fde_);
Yabin Cuibec02fc2015-08-28 15:44:27 -070042 }
43
44 private:
45 static void FdEventCallback(int fd, unsigned events, void* userdata) {
46 FdHandler* handler = reinterpret_cast<FdHandler*>(userdata);
47 ASSERT_EQ(0u, (events & ~(FDE_READ | FDE_WRITE))) << "unexpected events: " << events;
48 if (events & FDE_READ) {
49 ASSERT_EQ(fd, handler->read_fd_);
50 char c;
Josh Gao022d4472016-02-10 14:49:00 -080051 ASSERT_EQ(1, adb_read(fd, &c, 1));
Yabin Cuibec02fc2015-08-28 15:44:27 -070052 handler->queue_.push(c);
53 fdevent_add(&handler->write_fde_, FDE_WRITE);
54 }
55 if (events & FDE_WRITE) {
56 ASSERT_EQ(fd, handler->write_fd_);
57 ASSERT_FALSE(handler->queue_.empty());
58 char c = handler->queue_.front();
59 handler->queue_.pop();
Josh Gao022d4472016-02-10 14:49:00 -080060 ASSERT_EQ(1, adb_write(fd, &c, 1));
Yabin Cuibec02fc2015-08-28 15:44:27 -070061 if (handler->queue_.empty()) {
62 fdevent_del(&handler->write_fde_, FDE_WRITE);
63 }
64 }
65 }
66
67 private:
68 const int read_fd_;
69 const int write_fd_;
70 fdevent read_fde_;
71 fdevent write_fde_;
72 std::queue<char> queue_;
73};
74
Yabin Cuibec02fc2015-08-28 15:44:27 -070075struct ThreadArg {
76 int first_read_fd;
77 int last_write_fd;
78 size_t middle_pipe_count;
79};
80
Josh Gao022d4472016-02-10 14:49:00 -080081TEST_F(FdeventTest, fdevent_terminate) {
Josh Gao022d4472016-02-10 14:49:00 -080082 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -070083 TerminateThread();
Yabin Cuibec02fc2015-08-28 15:44:27 -070084}
85
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070086TEST_F(FdeventTest, smoke) {
Yabin Cuibec02fc2015-08-28 15:44:27 -070087 const size_t PIPE_COUNT = 10;
88 const size_t MESSAGE_LOOP_COUNT = 100;
89 const std::string MESSAGE = "fdevent_test";
90 int fd_pair1[2];
91 int fd_pair2[2];
Josh Gao022d4472016-02-10 14:49:00 -080092 ASSERT_EQ(0, adb_socketpair(fd_pair1));
93 ASSERT_EQ(0, adb_socketpair(fd_pair2));
Yabin Cuibec02fc2015-08-28 15:44:27 -070094 ThreadArg thread_arg;
95 thread_arg.first_read_fd = fd_pair1[0];
96 thread_arg.last_write_fd = fd_pair2[1];
97 thread_arg.middle_pipe_count = PIPE_COUNT;
98 int writer = fd_pair1[1];
99 int reader = fd_pair2[0];
100
Josh Gao022d4472016-02-10 14:49:00 -0800101 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -0700102
103 std::vector<std::unique_ptr<FdHandler>> fd_handlers;
104 fdevent_run_on_main_thread([&thread_arg, &fd_handlers]() {
105 std::vector<int> read_fds;
106 std::vector<int> write_fds;
107
108 read_fds.push_back(thread_arg.first_read_fd);
109 for (size_t i = 0; i < thread_arg.middle_pipe_count; ++i) {
110 int fds[2];
111 ASSERT_EQ(0, adb_socketpair(fds));
112 read_fds.push_back(fds[0]);
113 write_fds.push_back(fds[1]);
114 }
115 write_fds.push_back(thread_arg.last_write_fd);
116
117 for (size_t i = 0; i < read_fds.size(); ++i) {
118 fd_handlers.push_back(std::make_unique<FdHandler>(read_fds[i], write_fds[i]));
119 }
120 });
121 WaitForFdeventLoop();
Yabin Cuibec02fc2015-08-28 15:44:27 -0700122
123 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
124 std::string read_buffer = MESSAGE;
125 std::string write_buffer(MESSAGE.size(), 'a');
126 ASSERT_TRUE(WriteFdExactly(writer, read_buffer.c_str(), read_buffer.size()));
127 ASSERT_TRUE(ReadFdExactly(reader, &write_buffer[0], write_buffer.size()));
128 ASSERT_EQ(read_buffer, write_buffer);
129 }
130
Josh Gao7ab55712018-03-29 16:27:13 -0700131 fdevent_run_on_main_thread([&fd_handlers]() { fd_handlers.clear(); });
132 WaitForFdeventLoop();
133
134 TerminateThread();
Josh Gao022d4472016-02-10 14:49:00 -0800135 ASSERT_EQ(0, adb_close(writer));
136 ASSERT_EQ(0, adb_close(reader));
Yabin Cuibec02fc2015-08-28 15:44:27 -0700137}
Yabin Cuia1080162015-09-04 16:19:56 -0700138
139struct InvalidFdArg {
140 fdevent fde;
141 unsigned expected_events;
142 size_t* happened_event_count;
143};
144
Josh Gao7ab55712018-03-29 16:27:13 -0700145static void InvalidFdEventCallback(int, unsigned events, void* userdata) {
Yabin Cuia1080162015-09-04 16:19:56 -0700146 InvalidFdArg* arg = reinterpret_cast<InvalidFdArg*>(userdata);
147 ASSERT_EQ(arg->expected_events, events);
148 fdevent_remove(&arg->fde);
149 if (++*(arg->happened_event_count) == 2) {
Josh Gao022d4472016-02-10 14:49:00 -0800150 fdevent_terminate_loop();
Yabin Cuia1080162015-09-04 16:19:56 -0700151 }
152}
153
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700154static void InvalidFdThreadFunc() {
Yabin Cuia1080162015-09-04 16:19:56 -0700155 const int INVALID_READ_FD = std::numeric_limits<int>::max() - 1;
156 size_t happened_event_count = 0;
157 InvalidFdArg read_arg;
158 read_arg.expected_events = FDE_READ | FDE_ERROR;
159 read_arg.happened_event_count = &happened_event_count;
160 fdevent_install(&read_arg.fde, INVALID_READ_FD, InvalidFdEventCallback, &read_arg);
161 fdevent_add(&read_arg.fde, FDE_READ);
162
163 const int INVALID_WRITE_FD = std::numeric_limits<int>::max();
164 InvalidFdArg write_arg;
165 write_arg.expected_events = FDE_READ | FDE_ERROR;
166 write_arg.happened_event_count = &happened_event_count;
167 fdevent_install(&write_arg.fde, INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg);
168 fdevent_add(&write_arg.fde, FDE_WRITE);
169 fdevent_loop();
170}
171
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700172TEST_F(FdeventTest, invalid_fd) {
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700173 std::thread thread(InvalidFdThreadFunc);
174 thread.join();
Yabin Cuia1080162015-09-04 16:19:56 -0700175}
Josh Gao4c936392017-05-03 14:10:39 -0700176
177TEST_F(FdeventTest, run_on_main_thread) {
178 std::vector<int> vec;
179
180 PrepareThread();
Josh Gao4c936392017-05-03 14:10:39 -0700181
Josh Gao1222abc2018-03-19 15:19:45 -0700182 // Block the main thread for a long time while we queue our callbacks.
183 fdevent_run_on_main_thread([]() {
184 check_main_thread();
185 std::this_thread::sleep_for(std::chrono::seconds(1));
186 });
187
188 for (int i = 0; i < 1000000; ++i) {
Josh Gao4c936392017-05-03 14:10:39 -0700189 fdevent_run_on_main_thread([i, &vec]() {
190 check_main_thread();
191 vec.push_back(i);
192 });
193 }
194
Josh Gao7ab55712018-03-29 16:27:13 -0700195 TerminateThread();
Josh Gao4c936392017-05-03 14:10:39 -0700196
Josh Gao1222abc2018-03-19 15:19:45 -0700197 ASSERT_EQ(1000000u, vec.size());
198 for (int i = 0; i < 1000000; ++i) {
Josh Gao4c936392017-05-03 14:10:39 -0700199 ASSERT_EQ(i, vec[i]);
200 }
201}
Josh Gaoe39ccd32018-02-23 14:37:07 -0800202
203static std::function<void()> make_appender(std::vector<int>* vec, int value) {
204 return [vec, value]() {
205 check_main_thread();
206 if (value == 100) {
207 return;
208 }
209
210 vec->push_back(value);
211 fdevent_run_on_main_thread(make_appender(vec, value + 1));
212 };
213}
214
215TEST_F(FdeventTest, run_on_main_thread_reentrant) {
216 std::vector<int> vec;
217
218 PrepareThread();
Josh Gaoe39ccd32018-02-23 14:37:07 -0800219 fdevent_run_on_main_thread(make_appender(&vec, 0));
Josh Gao7ab55712018-03-29 16:27:13 -0700220 TerminateThread();
Josh Gaoe39ccd32018-02-23 14:37:07 -0800221
222 ASSERT_EQ(100u, vec.size());
223 for (int i = 0; i < 100; ++i) {
224 ASSERT_EQ(i, vec[i]);
225 }
226}