36 #include <gtest/gtest.h> 38 #include <sys/socket.h> 42 #include <boost/bind.hpp> 43 #include <boost/thread.hpp> 71 if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets_) != 0)
75 if(fcntl(sockets_[0], F_SETFL, O_NONBLOCK) == -1)
79 if(fcntl(sockets_[1], F_SETFL, O_NONBLOCK) == -1)
98 , pollouts_received_(0)
107 while(read(socket_, &b, 1) > 0)
113 if (events & POLLOUT)
115 ++pollouts_received_;
124 if (::write(socket_, &b, 1) > 0)
143 write(sockets_[1], &b, 1);
148 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, POLLIN));
152 write(sockets_[1], &b, 1);
156 ASSERT_TRUE(poll_set_.delEvents(sh.
socket_, POLLIN));
157 write(sockets_[1], &b, 1);
161 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, POLLIN));
165 ASSERT_TRUE(poll_set_.delSocket(sockets_[0]));
174 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, POLLOUT));
181 ASSERT_TRUE(poll_set_.delEvents(sh.
socket_, POLLOUT));
194 ASSERT_TRUE(poll_set_.addEvents(sh1.
socket_, POLLIN));
195 ASSERT_TRUE(poll_set_.addEvents(sh2.
socket_, POLLIN));
208 ASSERT_TRUE(poll_set_.addEvents(sh1.
socket_, POLLOUT));
209 ASSERT_TRUE(poll_set_.addEvents(sh2.
socket_, POLLOUT));
216 ASSERT_TRUE(poll_set_.delEvents(sh1.
socket_, POLLOUT));
217 ASSERT_TRUE(poll_set_.delEvents(sh2.
socket_, POLLOUT));
231 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, 0));
232 ASSERT_FALSE(poll_set_.addEvents(sh.
socket_ + 1, 0));
234 ASSERT_TRUE(poll_set_.delEvents(sh.
socket_, 0));
235 ASSERT_FALSE(poll_set_.delEvents(sh.
socket_ + 1, 0));
237 ASSERT_FALSE(poll_set_.delSocket(sh.
socket_ + 1));
238 ASSERT_TRUE(poll_set_.delSocket(sh.
socket_));
270 for (
int i = 0; i < 100; ++i)
275 const int thread_count = 100;
278 boost::barrier barrier(thread_count + 1);
280 boost::thread_group tg;
281 for (
int i = 0; i < thread_count/2; ++i)
283 tg.create_thread(boost::bind(
addThread, &poll_set_, &sh1, &barrier));
284 tg.create_thread(boost::bind(
addThread, &poll_set_, &sh2, &barrier));
307 boost::barrier barrier(thread_count + 1);
309 boost::thread_group tg;
310 for (
int i = 0; i < thread_count/2; ++i)
312 tg.create_thread(boost::bind(
delThread, &poll_set_, &sh1, &barrier));
313 tg.create_thread(boost::bind(
delThread, &poll_set_, &sh2, &barrier));
336 for (
int i = 0; i < count; ++i)
346 boost::this_thread::sleep(boost::posix_time::microseconds(100));
365 boost::barrier barrier(2);
366 volatile bool done =
false;
367 const int count = 1000;
390 poll_set_.update(-1);
397 int main(
int argc,
char** argv)
399 testing::InitGoogleTest(&argc, argv);
401 signal(SIGPIPE, SIG_IGN);
403 return RUN_ALL_TESTS();
bool addEvents(int sock, int events)
void addThread(PollSet *ps, SocketHelper *sh, boost::barrier *barrier)
void processEvents(int events)
bool delEvents(int sock, int events)
void delThread(PollSet *ps, SocketHelper *sh, boost::barrier *barrier)
void addDelManyTimesThread(PollSet *ps, SocketHelper *sh1, SocketHelper *sh2, boost::barrier *barrier, int count, volatile bool *done)
bool addSocket(int sock, const SocketUpdateFunc &update_func, const TransportPtr &transport=TransportPtr())
int main(int argc, char **argv)