blob: a9746bbc8bff453bc43b95a1cc204a2ca5f52253 [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>
Pirama Arumuga Nainar29e3dd82018-08-08 10:33:24 -070022#include <memory>
Yabin Cuibec02fc2015-08-28 15:44:27 -070023#include <queue>
24#include <string>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070025#include <thread>
Yabin Cuibec02fc2015-08-28 15:44:27 -070026#include <vector>
27
Yabin Cuibec02fc2015-08-28 15:44:27 -070028#include "adb_io.h"
Josh Gao022d4472016-02-10 14:49:00 -080029#include "fdevent_test.h"
Yabin Cuibec02fc2015-08-28 15:44:27 -070030
Yabin Cuibec02fc2015-08-28 15:44:27 -070031class FdHandler {
32 public:
Josh Gaoc162c712019-01-25 15:30:25 -080033 FdHandler(int read_fd, int write_fd, bool use_new_callback)
34 : read_fd_(read_fd), write_fd_(write_fd) {
35 if (use_new_callback) {
36 read_fde_ = fdevent_create(read_fd_, FdEventNewCallback, this);
37 write_fde_ = fdevent_create(write_fd_, FdEventNewCallback, this);
38 } else {
39 read_fde_ = fdevent_create(read_fd_, FdEventCallback, this);
40 write_fde_ = fdevent_create(write_fd_, FdEventCallback, this);
41 }
Josh Gao71f775a2018-05-14 11:14:33 -070042 fdevent_add(read_fde_, FDE_READ);
Yabin Cuia1080162015-09-04 16:19:56 -070043 }
44
45 ~FdHandler() {
Josh Gao71f775a2018-05-14 11:14:33 -070046 fdevent_destroy(read_fde_);
47 fdevent_destroy(write_fde_);
Yabin Cuibec02fc2015-08-28 15:44:27 -070048 }
49
50 private:
51 static void FdEventCallback(int fd, unsigned events, void* userdata) {
52 FdHandler* handler = reinterpret_cast<FdHandler*>(userdata);
53 ASSERT_EQ(0u, (events & ~(FDE_READ | FDE_WRITE))) << "unexpected events: " << events;
54 if (events & FDE_READ) {
55 ASSERT_EQ(fd, handler->read_fd_);
56 char c;
Josh Gao022d4472016-02-10 14:49:00 -080057 ASSERT_EQ(1, adb_read(fd, &c, 1));
Yabin Cuibec02fc2015-08-28 15:44:27 -070058 handler->queue_.push(c);
Josh Gao71f775a2018-05-14 11:14:33 -070059 fdevent_add(handler->write_fde_, FDE_WRITE);
Yabin Cuibec02fc2015-08-28 15:44:27 -070060 }
61 if (events & FDE_WRITE) {
62 ASSERT_EQ(fd, handler->write_fd_);
63 ASSERT_FALSE(handler->queue_.empty());
64 char c = handler->queue_.front();
65 handler->queue_.pop();
Josh Gao022d4472016-02-10 14:49:00 -080066 ASSERT_EQ(1, adb_write(fd, &c, 1));
Yabin Cuibec02fc2015-08-28 15:44:27 -070067 if (handler->queue_.empty()) {
Josh Gao71f775a2018-05-14 11:14:33 -070068 fdevent_del(handler->write_fde_, FDE_WRITE);
Yabin Cuibec02fc2015-08-28 15:44:27 -070069 }
70 }
71 }
72
Josh Gaoc162c712019-01-25 15:30:25 -080073 static void FdEventNewCallback(fdevent* fde, unsigned events, void* userdata) {
74 int fd = fde->fd.get();
75 FdHandler* handler = reinterpret_cast<FdHandler*>(userdata);
76 ASSERT_EQ(0u, (events & ~(FDE_READ | FDE_WRITE))) << "unexpected events: " << events;
77 if (events & FDE_READ) {
78 ASSERT_EQ(fd, handler->read_fd_);
79 char c;
80 ASSERT_EQ(1, adb_read(fd, &c, 1));
81 handler->queue_.push(c);
82 fdevent_add(handler->write_fde_, FDE_WRITE);
83 }
84 if (events & FDE_WRITE) {
85 ASSERT_EQ(fd, handler->write_fd_);
86 ASSERT_FALSE(handler->queue_.empty());
87 char c = handler->queue_.front();
88 handler->queue_.pop();
89 ASSERT_EQ(1, adb_write(fd, &c, 1));
90 if (handler->queue_.empty()) {
91 fdevent_del(handler->write_fde_, FDE_WRITE);
92 }
93 }
94 }
95
Yabin Cuibec02fc2015-08-28 15:44:27 -070096 private:
97 const int read_fd_;
98 const int write_fd_;
Josh Gao71f775a2018-05-14 11:14:33 -070099 fdevent* read_fde_;
100 fdevent* write_fde_;
Yabin Cuibec02fc2015-08-28 15:44:27 -0700101 std::queue<char> queue_;
102};
103
Yabin Cuibec02fc2015-08-28 15:44:27 -0700104struct ThreadArg {
105 int first_read_fd;
106 int last_write_fd;
107 size_t middle_pipe_count;
108};
109
Josh Gao022d4472016-02-10 14:49:00 -0800110TEST_F(FdeventTest, fdevent_terminate) {
Josh Gao022d4472016-02-10 14:49:00 -0800111 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -0700112 TerminateThread();
Yabin Cuibec02fc2015-08-28 15:44:27 -0700113}
114
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700115TEST_F(FdeventTest, smoke) {
Josh Gaoc162c712019-01-25 15:30:25 -0800116 for (bool use_new_callback : {true, false}) {
117 fdevent_reset();
118 const size_t PIPE_COUNT = 10;
119 const size_t MESSAGE_LOOP_COUNT = 100;
120 const std::string MESSAGE = "fdevent_test";
121 int fd_pair1[2];
122 int fd_pair2[2];
123 ASSERT_EQ(0, adb_socketpair(fd_pair1));
124 ASSERT_EQ(0, adb_socketpair(fd_pair2));
125 ThreadArg thread_arg;
126 thread_arg.first_read_fd = fd_pair1[0];
127 thread_arg.last_write_fd = fd_pair2[1];
128 thread_arg.middle_pipe_count = PIPE_COUNT;
129 int writer = fd_pair1[1];
130 int reader = fd_pair2[0];
Yabin Cuibec02fc2015-08-28 15:44:27 -0700131
Josh Gaoc162c712019-01-25 15:30:25 -0800132 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -0700133
Josh Gaoc162c712019-01-25 15:30:25 -0800134 std::vector<std::unique_ptr<FdHandler>> fd_handlers;
135 fdevent_run_on_main_thread([&thread_arg, &fd_handlers, use_new_callback]() {
136 std::vector<int> read_fds;
137 std::vector<int> write_fds;
Josh Gao7ab55712018-03-29 16:27:13 -0700138
Josh Gaoc162c712019-01-25 15:30:25 -0800139 read_fds.push_back(thread_arg.first_read_fd);
140 for (size_t i = 0; i < thread_arg.middle_pipe_count; ++i) {
141 int fds[2];
142 ASSERT_EQ(0, adb_socketpair(fds));
143 read_fds.push_back(fds[0]);
144 write_fds.push_back(fds[1]);
145 }
146 write_fds.push_back(thread_arg.last_write_fd);
147
148 for (size_t i = 0; i < read_fds.size(); ++i) {
149 fd_handlers.push_back(
150 std::make_unique<FdHandler>(read_fds[i], write_fds[i], use_new_callback));
151 }
152 });
153 WaitForFdeventLoop();
154
155 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
156 std::string read_buffer = MESSAGE;
157 std::string write_buffer(MESSAGE.size(), 'a');
158 ASSERT_TRUE(WriteFdExactly(writer, read_buffer.c_str(), read_buffer.size()));
159 ASSERT_TRUE(ReadFdExactly(reader, &write_buffer[0], write_buffer.size()));
160 ASSERT_EQ(read_buffer, write_buffer);
Josh Gao7ab55712018-03-29 16:27:13 -0700161 }
Josh Gao7ab55712018-03-29 16:27:13 -0700162
Josh Gaoc162c712019-01-25 15:30:25 -0800163 fdevent_run_on_main_thread([&fd_handlers]() { fd_handlers.clear(); });
164 WaitForFdeventLoop();
Yabin Cuibec02fc2015-08-28 15:44:27 -0700165
Josh Gaoc162c712019-01-25 15:30:25 -0800166 TerminateThread();
167 ASSERT_EQ(0, adb_close(writer));
168 ASSERT_EQ(0, adb_close(reader));
Yabin Cuibec02fc2015-08-28 15:44:27 -0700169 }
Yabin Cuibec02fc2015-08-28 15:44:27 -0700170}
Yabin Cuia1080162015-09-04 16:19:56 -0700171
172struct InvalidFdArg {
Josh Gao71f775a2018-05-14 11:14:33 -0700173 fdevent* fde;
Yabin Cuia1080162015-09-04 16:19:56 -0700174 unsigned expected_events;
175 size_t* happened_event_count;
176};
177
Josh Gao7ab55712018-03-29 16:27:13 -0700178static void InvalidFdEventCallback(int, unsigned events, void* userdata) {
Yabin Cuia1080162015-09-04 16:19:56 -0700179 InvalidFdArg* arg = reinterpret_cast<InvalidFdArg*>(userdata);
180 ASSERT_EQ(arg->expected_events, events);
Josh Gao71f775a2018-05-14 11:14:33 -0700181 fdevent_destroy(arg->fde);
Yabin Cuia1080162015-09-04 16:19:56 -0700182 if (++*(arg->happened_event_count) == 2) {
Josh Gao022d4472016-02-10 14:49:00 -0800183 fdevent_terminate_loop();
Yabin Cuia1080162015-09-04 16:19:56 -0700184 }
185}
186
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700187static void InvalidFdThreadFunc() {
Yabin Cuia1080162015-09-04 16:19:56 -0700188 const int INVALID_READ_FD = std::numeric_limits<int>::max() - 1;
189 size_t happened_event_count = 0;
190 InvalidFdArg read_arg;
191 read_arg.expected_events = FDE_READ | FDE_ERROR;
192 read_arg.happened_event_count = &happened_event_count;
Josh Gao71f775a2018-05-14 11:14:33 -0700193 read_arg.fde = fdevent_create(INVALID_READ_FD, InvalidFdEventCallback, &read_arg);
194 fdevent_add(read_arg.fde, FDE_READ);
Yabin Cuia1080162015-09-04 16:19:56 -0700195
196 const int INVALID_WRITE_FD = std::numeric_limits<int>::max();
197 InvalidFdArg write_arg;
198 write_arg.expected_events = FDE_READ | FDE_ERROR;
199 write_arg.happened_event_count = &happened_event_count;
Josh Gao71f775a2018-05-14 11:14:33 -0700200 write_arg.fde = fdevent_create(INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg);
201 fdevent_add(write_arg.fde, FDE_WRITE);
Yabin Cuia1080162015-09-04 16:19:56 -0700202 fdevent_loop();
203}
204
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700205TEST_F(FdeventTest, invalid_fd) {
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700206 std::thread thread(InvalidFdThreadFunc);
207 thread.join();
Yabin Cuia1080162015-09-04 16:19:56 -0700208}
Josh Gao4c936392017-05-03 14:10:39 -0700209
210TEST_F(FdeventTest, run_on_main_thread) {
211 std::vector<int> vec;
212
213 PrepareThread();
Josh Gao4c936392017-05-03 14:10:39 -0700214
Josh Gao1222abc2018-03-19 15:19:45 -0700215 // Block the main thread for a long time while we queue our callbacks.
216 fdevent_run_on_main_thread([]() {
217 check_main_thread();
218 std::this_thread::sleep_for(std::chrono::seconds(1));
219 });
220
221 for (int i = 0; i < 1000000; ++i) {
Josh Gao4c936392017-05-03 14:10:39 -0700222 fdevent_run_on_main_thread([i, &vec]() {
223 check_main_thread();
224 vec.push_back(i);
225 });
226 }
227
Josh Gao7ab55712018-03-29 16:27:13 -0700228 TerminateThread();
Josh Gao4c936392017-05-03 14:10:39 -0700229
Josh Gao1222abc2018-03-19 15:19:45 -0700230 ASSERT_EQ(1000000u, vec.size());
231 for (int i = 0; i < 1000000; ++i) {
Josh Gao4c936392017-05-03 14:10:39 -0700232 ASSERT_EQ(i, vec[i]);
233 }
234}
Josh Gaoe39ccd32018-02-23 14:37:07 -0800235
236static std::function<void()> make_appender(std::vector<int>* vec, int value) {
237 return [vec, value]() {
238 check_main_thread();
239 if (value == 100) {
240 return;
241 }
242
243 vec->push_back(value);
244 fdevent_run_on_main_thread(make_appender(vec, value + 1));
245 };
246}
247
248TEST_F(FdeventTest, run_on_main_thread_reentrant) {
249 std::vector<int> vec;
250
251 PrepareThread();
Josh Gaoe39ccd32018-02-23 14:37:07 -0800252 fdevent_run_on_main_thread(make_appender(&vec, 0));
Josh Gao7ab55712018-03-29 16:27:13 -0700253 TerminateThread();
Josh Gaoe39ccd32018-02-23 14:37:07 -0800254
255 ASSERT_EQ(100u, vec.size());
256 for (int i = 0; i < 100; ++i) {
257 ASSERT_EQ(i, vec[i]);
258 }
259}