blob: f7622b5f9acb87dfa42c5ac10a5a570c95169ec1 [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
21#include <queue>
22#include <string>
23#include <vector>
24
25#include <pthread.h>
26#include <signal.h>
27
28#include "adb_io.h"
29
30class SignalHandlerRegister {
31 public:
32 SignalHandlerRegister(const std::vector<int>& signums, void (*handler)(int)) {
33 for (auto& sig : signums) {
34 sig_t old_handler = signal(sig, handler);
35 saved_signal_handlers_.push_back(std::make_pair(sig, old_handler));
36 }
37 }
38
39 ~SignalHandlerRegister() {
40 for (auto& pair : saved_signal_handlers_) {
41 signal(pair.first, pair.second);
42 }
43 }
44
45 private:
46 std::vector<std::pair<int, sig_t>> saved_signal_handlers_;
47};
48
49class FdHandler {
50 public:
51 FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) {
52 fdevent_install(&read_fde_, read_fd_, FdEventCallback, this);
53 fdevent_add(&read_fde_, FDE_READ | FDE_ERROR);
54 fdevent_install(&write_fde_, write_fd_, FdEventCallback, this);
55 fdevent_add(&write_fde_, FDE_ERROR);
56 }
57
58 private:
59 static void FdEventCallback(int fd, unsigned events, void* userdata) {
60 FdHandler* handler = reinterpret_cast<FdHandler*>(userdata);
61 ASSERT_EQ(0u, (events & ~(FDE_READ | FDE_WRITE))) << "unexpected events: " << events;
62 if (events & FDE_READ) {
63 ASSERT_EQ(fd, handler->read_fd_);
64 char c;
65 ASSERT_EQ(1, read(fd, &c, 1));
66 handler->queue_.push(c);
67 fdevent_add(&handler->write_fde_, FDE_WRITE);
68 }
69 if (events & FDE_WRITE) {
70 ASSERT_EQ(fd, handler->write_fd_);
71 ASSERT_FALSE(handler->queue_.empty());
72 char c = handler->queue_.front();
73 handler->queue_.pop();
74 ASSERT_EQ(1, write(fd, &c, 1));
75 if (handler->queue_.empty()) {
76 fdevent_del(&handler->write_fde_, FDE_WRITE);
77 }
78 }
79 }
80
81 private:
82 const int read_fd_;
83 const int write_fd_;
84 fdevent read_fde_;
85 fdevent write_fde_;
86 std::queue<char> queue_;
87};
88
89static void signal_handler(int) {
90 pthread_exit(nullptr);
91}
92
93struct ThreadArg {
94 int first_read_fd;
95 int last_write_fd;
96 size_t middle_pipe_count;
97};
98
99static void FdEventThreadFunc(ThreadArg* arg) {
100 SignalHandlerRegister signal_handler_register({SIGUSR1}, signal_handler);
101
102 std::vector<int> read_fds;
103 std::vector<int> write_fds;
104
105 read_fds.push_back(arg->first_read_fd);
106 for (size_t i = 0; i < arg->middle_pipe_count; ++i) {
107 int fds[2];
108 ASSERT_EQ(0, pipe(fds));
109 read_fds.push_back(fds[0]);
110 write_fds.push_back(fds[1]);
111 }
112 write_fds.push_back(arg->last_write_fd);
113
114 std::vector<std::unique_ptr<FdHandler>> fd_handlers;
115 for (size_t i = 0; i < read_fds.size(); ++i) {
116 fd_handlers.push_back(std::unique_ptr<FdHandler>(new FdHandler(read_fds[i], write_fds[i])));
117 }
118
119 fdevent_loop();
120}
121
122TEST(fdevent, smoke) {
123 const size_t PIPE_COUNT = 10;
124 const size_t MESSAGE_LOOP_COUNT = 100;
125 const std::string MESSAGE = "fdevent_test";
126 int fd_pair1[2];
127 int fd_pair2[2];
128 ASSERT_EQ(0, pipe(fd_pair1));
129 ASSERT_EQ(0, pipe(fd_pair2));
130 pthread_t thread;
131 ThreadArg thread_arg;
132 thread_arg.first_read_fd = fd_pair1[0];
133 thread_arg.last_write_fd = fd_pair2[1];
134 thread_arg.middle_pipe_count = PIPE_COUNT;
135 int writer = fd_pair1[1];
136 int reader = fd_pair2[0];
137
138 ASSERT_EQ(0, pthread_create(&thread, nullptr,
139 reinterpret_cast<void* (*)(void*)>(FdEventThreadFunc),
140 &thread_arg));
141
142 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
143 std::string read_buffer = MESSAGE;
144 std::string write_buffer(MESSAGE.size(), 'a');
145 ASSERT_TRUE(WriteFdExactly(writer, read_buffer.c_str(), read_buffer.size()));
146 ASSERT_TRUE(ReadFdExactly(reader, &write_buffer[0], write_buffer.size()));
147 ASSERT_EQ(read_buffer, write_buffer);
148 }
149
150 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
151 ASSERT_EQ(0, pthread_join(thread, nullptr));
152}