| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 21 | #include <limits> | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 22 | #include <queue> | 
 | 23 | #include <string> | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 24 | #include <thread> | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 25 | #include <vector> | 
 | 26 |  | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 27 | #include "adb_io.h" | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 28 | #include "fdevent_test.h" | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 29 |  | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 30 | class FdHandler { | 
 | 31 |   public: | 
 | 32 |     FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) { | 
 | 33 |         fdevent_install(&read_fde_, read_fd_, FdEventCallback, this); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 34 |         fdevent_add(&read_fde_, FDE_READ); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 35 |         fdevent_install(&write_fde_, write_fd_, FdEventCallback, this); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 36 |     } | 
 | 37 |  | 
 | 38 |     ~FdHandler() { | 
 | 39 |         fdevent_remove(&read_fde_); | 
 | 40 |         fdevent_remove(&write_fde_); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 41 |     } | 
 | 42 |  | 
 | 43 |   private: | 
 | 44 |     static void FdEventCallback(int fd, unsigned events, void* userdata) { | 
 | 45 |         FdHandler* handler = reinterpret_cast<FdHandler*>(userdata); | 
 | 46 |         ASSERT_EQ(0u, (events & ~(FDE_READ | FDE_WRITE))) << "unexpected events: " << events; | 
 | 47 |         if (events & FDE_READ) { | 
 | 48 |             ASSERT_EQ(fd, handler->read_fd_); | 
 | 49 |             char c; | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 50 |             ASSERT_EQ(1, adb_read(fd, &c, 1)); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 51 |             handler->queue_.push(c); | 
 | 52 |             fdevent_add(&handler->write_fde_, FDE_WRITE); | 
 | 53 |         } | 
 | 54 |         if (events & FDE_WRITE) { | 
 | 55 |             ASSERT_EQ(fd, handler->write_fd_); | 
 | 56 |             ASSERT_FALSE(handler->queue_.empty()); | 
 | 57 |             char c = handler->queue_.front(); | 
 | 58 |             handler->queue_.pop(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 59 |             ASSERT_EQ(1, adb_write(fd, &c, 1)); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 60 |             if (handler->queue_.empty()) { | 
 | 61 |               fdevent_del(&handler->write_fde_, FDE_WRITE); | 
 | 62 |             } | 
 | 63 |         } | 
 | 64 |     } | 
 | 65 |  | 
 | 66 |   private: | 
 | 67 |     const int read_fd_; | 
 | 68 |     const int write_fd_; | 
 | 69 |     fdevent read_fde_; | 
 | 70 |     fdevent write_fde_; | 
 | 71 |     std::queue<char> queue_; | 
 | 72 | }; | 
 | 73 |  | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 74 | struct ThreadArg { | 
 | 75 |     int first_read_fd; | 
 | 76 |     int last_write_fd; | 
 | 77 |     size_t middle_pipe_count; | 
 | 78 | }; | 
 | 79 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 80 | TEST_F(FdeventTest, fdevent_terminate) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 81 |     PrepareThread(); | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 82 |  | 
 | 83 |     std::thread thread(fdevent_loop); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 84 |     TerminateThread(thread); | 
 | 85 | } | 
 | 86 |  | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 87 | static void FdEventThreadFunc(ThreadArg* arg) { | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 88 |     std::vector<int> read_fds; | 
 | 89 |     std::vector<int> write_fds; | 
 | 90 |  | 
 | 91 |     read_fds.push_back(arg->first_read_fd); | 
 | 92 |     for (size_t i = 0; i < arg->middle_pipe_count; ++i) { | 
 | 93 |         int fds[2]; | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 94 |         ASSERT_EQ(0, adb_socketpair(fds)); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 95 |         read_fds.push_back(fds[0]); | 
 | 96 |         write_fds.push_back(fds[1]); | 
 | 97 |     } | 
 | 98 |     write_fds.push_back(arg->last_write_fd); | 
 | 99 |  | 
 | 100 |     std::vector<std::unique_ptr<FdHandler>> fd_handlers; | 
 | 101 |     for (size_t i = 0; i < read_fds.size(); ++i) { | 
 | 102 |         fd_handlers.push_back(std::unique_ptr<FdHandler>(new FdHandler(read_fds[i], write_fds[i]))); | 
 | 103 |     } | 
 | 104 |  | 
 | 105 |     fdevent_loop(); | 
 | 106 | } | 
 | 107 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 108 | TEST_F(FdeventTest, smoke) { | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 109 |     const size_t PIPE_COUNT = 10; | 
 | 110 |     const size_t MESSAGE_LOOP_COUNT = 100; | 
 | 111 |     const std::string MESSAGE = "fdevent_test"; | 
 | 112 |     int fd_pair1[2]; | 
 | 113 |     int fd_pair2[2]; | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 114 |     ASSERT_EQ(0, adb_socketpair(fd_pair1)); | 
 | 115 |     ASSERT_EQ(0, adb_socketpair(fd_pair2)); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 116 |     ThreadArg thread_arg; | 
 | 117 |     thread_arg.first_read_fd = fd_pair1[0]; | 
 | 118 |     thread_arg.last_write_fd = fd_pair2[1]; | 
 | 119 |     thread_arg.middle_pipe_count = PIPE_COUNT; | 
 | 120 |     int writer = fd_pair1[1]; | 
 | 121 |     int reader = fd_pair2[0]; | 
 | 122 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 123 |     PrepareThread(); | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 124 |     std::thread thread(FdEventThreadFunc, &thread_arg); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 125 |  | 
 | 126 |     for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) { | 
 | 127 |         std::string read_buffer = MESSAGE; | 
 | 128 |         std::string write_buffer(MESSAGE.size(), 'a'); | 
 | 129 |         ASSERT_TRUE(WriteFdExactly(writer, read_buffer.c_str(), read_buffer.size())); | 
 | 130 |         ASSERT_TRUE(ReadFdExactly(reader, &write_buffer[0], write_buffer.size())); | 
 | 131 |         ASSERT_EQ(read_buffer, write_buffer); | 
 | 132 |     } | 
 | 133 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 134 |     TerminateThread(thread); | 
 | 135 |     ASSERT_EQ(0, adb_close(writer)); | 
 | 136 |     ASSERT_EQ(0, adb_close(reader)); | 
| Yabin Cui | bec02fc | 2015-08-28 15:44:27 -0700 | [diff] [blame] | 137 | } | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 138 |  | 
 | 139 | struct InvalidFdArg { | 
 | 140 |     fdevent fde; | 
 | 141 |     unsigned expected_events; | 
 | 142 |     size_t* happened_event_count; | 
 | 143 | }; | 
 | 144 |  | 
 | 145 | static void InvalidFdEventCallback(int fd, unsigned events, void* userdata) { | 
 | 146 |     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 Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 150 |         fdevent_terminate_loop(); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 151 |     } | 
 | 152 | } | 
 | 153 |  | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 154 | static void InvalidFdThreadFunc() { | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 155 |     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 Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 172 | TEST_F(FdeventTest, invalid_fd) { | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 173 |     std::thread thread(InvalidFdThreadFunc); | 
 | 174 |     thread.join(); | 
| Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 175 | } | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 176 |  | 
 | 177 | TEST_F(FdeventTest, run_on_main_thread) { | 
 | 178 |     std::vector<int> vec; | 
 | 179 |  | 
 | 180 |     PrepareThread(); | 
 | 181 |     std::thread thread(fdevent_loop); | 
 | 182 |  | 
 | 183 |     for (int i = 0; i < 100; ++i) { | 
 | 184 |         fdevent_run_on_main_thread([i, &vec]() { | 
 | 185 |             check_main_thread(); | 
 | 186 |             vec.push_back(i); | 
 | 187 |         }); | 
 | 188 |     } | 
 | 189 |  | 
 | 190 |     TerminateThread(thread); | 
 | 191 |  | 
 | 192 |     ASSERT_EQ(100u, vec.size()); | 
 | 193 |     for (int i = 0; i < 100; ++i) { | 
 | 194 |         ASSERT_EQ(i, vec[i]); | 
 | 195 |     } | 
 | 196 | } |