30 #include <netinet/in.h> 33 #include <sys/socket.h> 34 #include <sys/types.h> 42 #include <gtest/gtest.h> 51 int __wrap_poll(
struct pollfd *fds, nfds_t nfds,
int timeout) {
66 int mock_poll(
struct pollfd *fds, nfds_t nfds,
int timeout) {
73 for(nfds_t i=0; i<nfds && i<
poll_fds.size(); i++) {
74 EXPECT_EQ(
poll_fds[i].fd, fds[i].fd);
75 EXPECT_EQ(
poll_fds[i].events, fds[i].events);
76 fds[i].revents =
poll_fds[i].revents;
86 ts.tv_sec = timeout / 1000;
87 ts.tv_nsec = (timeout % 1000) * 1000000;
92 ret = nanosleep(&ts, &ts);
93 }
while( ret != 0 && errno == EINTR);
100 void Expect_poll(std::vector<pollfd> fds,
int timeout,
int _errno,
int ret) {
101 EXPECT_EQ(0,
poll_calls) <<
"Test bug: Cannot expect more than one call to poll";
114 : handleEvent_calls(0), last_event(0), event_result(0),
close_calls(0) {
123 last_event = eventType;
139 #define EXPECT_CLOSE_CALLS(n) \ 141 EXPECT_EQ(m.close_calls, n); \ 145 #define EXPECT_EVENTS(n) \ 147 EXPECT_EQ(m.handleEvent_calls, n); \ 148 m.handleEvent_calls = 0; \ 151 #define EXPECT_EVENT(event) \ 153 EXPECT_EQ(m.last_event, event); \ 154 EXPECT_EQ(m.handleEvent_calls, 1); \ 155 m.handleEvent_calls = 0; \ 161 pollfd f = { .fd = 4, .events = 0, .revents = 0 };
214 EXPECT_EQ(dispatch._sources.size(), 1u);
217 fds[0].events = POLLIN;
226 fds[0].events = POLLIN;
227 fds[0].revents = POLLIN;
238 EXPECT_EQ(dispatch._sources.size(), 1u);
241 fds[0].events = POLLOUT;
242 fds[0].revents = POLLOUT;
250 EXPECT_EQ(dispatch._sources.size(), 0u);
262 EXPECT_EQ(dispatch._sources.size(), 1u);
265 fds[0].events = POLLOUT;
271 EXPECT_EQ(dispatch._sources.size(), 1u);
277 EXPECT_EQ(dispatch._sources.size(), 1u);
281 fds[0].events = POLLOUT;
282 fds[0].revents = POLLOUT;
291 EXPECT_EQ(dispatch._sources.size(), 0u);
303 EXPECT_EQ(dispatch._sources.size(), 1u);
306 fds[0].events = POLLPRI;
314 fds[0].events = POLLPRI;
315 fds[0].revents = POLLPRI;
327 EXPECT_EQ(dispatch._sources.size(), 1u);
331 fds[0].events = POLLOUT;
332 fds[0].revents = POLLOUT;
337 EXPECT_EQ(dispatch._sources.size(), 1u);
340 int main(
int argc,
char **argv)
342 ::testing::InitGoogleTest(&argc, argv);
343 return RUN_ALL_TESTS();
connected/data can be written without blocking
TEST_F(MockSourceTest, ReadEvent)
int mock_poll(struct pollfd *fds, nfds_t nfds, int timeout)
void Expect_poll(std::vector< pollfd > fds, int timeout, int _errno, int ret)
An RPC source represents a file descriptor to monitor.
std::deque< int > close_calls
std::vector< pollfd > fds
virtual unsigned handleEvent(unsigned eventType)
Return true to continue monitoring this source.
#define EXPECT_CLOSE_CALLS(n)
int(* fake_poll)(struct pollfd *, nfds_t, int)=0
std::vector< pollfd > poll_fds
int main(int argc, char **argv)
out-of-band data has arrived
#define EXPECT_EVENT(event)
virtual void close()
Close the owned fd. If deleteOnClose was specified at construction, the object is deleted...
int __wrap_poll(struct pollfd *fds, nfds_t nfds, int timeout)