36 #include <gtest/gtest.h>
39 # include <sys/socket.h>
44 #include <boost/bind/bind.hpp>
45 #include <boost/thread.hpp>
52 if (fcntl(socket, F_SETFL, O_NONBLOCK) == -1)
57 u_long non_blocking = 1;
58 if (ioctlsocket(socket, FIONBIO, &non_blocking) != 0)
60 return WSAGetLastError();
69 return socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair);
71 socket_pair[0] = INVALID_SOCKET;
72 socket_pair[1] = INVALID_SOCKET;
78 listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
79 if (listen_socket == INVALID_SOCKET)
81 return WSAGetLastError();
86 if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*
>(&reuse),
static_cast<socklen_t
>(
sizeof(reuse))) == SOCKET_ERROR)
88 ::closesocket(listen_socket);
89 return WSAGetLastError();
94 struct sockaddr_in inaddr;
98 memset(&a, 0,
sizeof(a));
99 a.inaddr.sin_family = AF_INET;
100 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
103 a.inaddr.sin_port = 0;
105 if (bind(listen_socket, &a.addr,
sizeof(a.inaddr)) == SOCKET_ERROR)
107 ::closesocket(listen_socket);
108 return WSAGetLastError();
112 socklen_t addrlen =
static_cast<socklen_t
>(
sizeof(a.inaddr));
113 if (getsockname(listen_socket, &a.addr, &addrlen) == SOCKET_ERROR)
115 ::closesocket(listen_socket);
116 return WSAGetLastError();
119 if (listen(listen_socket, 1) == SOCKET_ERROR)
121 ::closesocket(listen_socket);
122 return WSAGetLastError();
128 DWORD overlapped_flag = 0;
129 socket_pair[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, overlapped_flag);
130 if (socket_pair[0] == INVALID_SOCKET)
132 ::closesocket(listen_socket);
133 ::closesocket(socket_pair[0]);
134 return WSAGetLastError();
138 if (connect(socket_pair[0], &a.addr,
sizeof(a.inaddr)) == SOCKET_ERROR)
140 ::closesocket(listen_socket);
141 ::closesocket(socket_pair[0]);
142 return WSAGetLastError();
148 socket_pair[1] = accept(listen_socket, NULL, NULL);
149 if (socket_pair[1] == INVALID_SOCKET)
151 ::closesocket(listen_socket);
152 ::closesocket(socket_pair[0]);
153 return WSAGetLastError();
159 ::closesocket(listen_socket);
171 WSAStartup(MAKEWORD(2, 0), &wsaData);
177 ::close(sockets_[0]);
178 ::close(sockets_[1]);
187 boost::this_thread::sleep(boost::posix_time::microseconds(100000));
223 , pollouts_received_(0)
232 while(read(socket_, &b, 1) > 0)
238 if (events & POLLOUT)
240 ++pollouts_received_;
249 if (::write(socket_, &b, 1) > 0)
268 write(sockets_[1], &b, 1);
273 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, POLLIN));
277 write(sockets_[1], &b, 1);
281 ASSERT_TRUE(poll_set_.delEvents(sh.
socket_, POLLIN));
282 write(sockets_[1], &b, 1);
286 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, POLLIN));
290 ASSERT_TRUE(poll_set_.delSocket(sockets_[0]));
299 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, POLLOUT));
306 ASSERT_TRUE(poll_set_.delEvents(sh.
socket_, POLLOUT));
319 ASSERT_TRUE(poll_set_.addEvents(sh1.
socket_, POLLIN));
320 ASSERT_TRUE(poll_set_.addEvents(sh2.
socket_, POLLIN));
333 ASSERT_TRUE(poll_set_.addEvents(sh1.
socket_, POLLOUT));
334 ASSERT_TRUE(poll_set_.addEvents(sh2.
socket_, POLLOUT));
341 ASSERT_TRUE(poll_set_.delEvents(sh1.
socket_, POLLOUT));
342 ASSERT_TRUE(poll_set_.delEvents(sh2.
socket_, POLLOUT));
356 ASSERT_TRUE(poll_set_.addEvents(sh.
socket_, 0));
357 ASSERT_FALSE(poll_set_.addEvents(sh.
socket_ + 1, 0));
359 ASSERT_TRUE(poll_set_.delEvents(sh.
socket_, 0));
360 ASSERT_FALSE(poll_set_.delEvents(sh.
socket_ + 1, 0));
362 ASSERT_FALSE(poll_set_.delSocket(sh.
socket_ + 1));
363 ASSERT_TRUE(poll_set_.delSocket(sh.
socket_));
395 for (
int i = 0; i < 100; ++i)
400 const int thread_count = 100;
403 boost::barrier barrier(thread_count + 1);
405 boost::thread_group tg;
406 for (
int i = 0; i < thread_count/2; ++i)
408 tg.create_thread(boost::bind(
addThread, &poll_set_, &sh1, &barrier));
409 tg.create_thread(boost::bind(
addThread, &poll_set_, &sh2, &barrier));
432 boost::barrier barrier(thread_count + 1);
434 boost::thread_group tg;
435 for (
int i = 0; i < thread_count/2; ++i)
437 tg.create_thread(boost::bind(
delThread, &poll_set_, &sh1, &barrier));
438 tg.create_thread(boost::bind(
delThread, &poll_set_, &sh2, &barrier));
461 for (
int i = 0; i < count; ++i)
471 boost::this_thread::sleep(boost::posix_time::microseconds(100));
490 boost::barrier barrier(2);
491 volatile bool done =
false;
492 const int count = 1000;
515 poll_set_.update(-1);
518 boost::this_thread::sleep(boost::posix_time::microseconds(50000));
522 int main(
int argc,
char** argv)
524 testing::InitGoogleTest(&argc, argv);
527 signal(SIGPIPE, SIG_IGN);
530 return RUN_ALL_TESTS();