tests: investigating catch2
[awesomized/libmemcached] / testing / lib / Poll.cpp
1 #include "Poll.hpp"
2
3 #include <cmath>
4 #include <cstdio>
5 #include <algorithm>
6 #include <iostream>
7
8 #include <poll.h>
9
10 using namespace std;
11
12 bool Poll::operator() (const vector<int> &fds) {
13 vector<pollfd> pfds;
14
15 pfds.reserve(fds.size());
16 for (auto fd : fds) {
17 pfds.emplace_back(pollfd{fd, events, 0});
18 }
19
20 while (!pfds.empty() && timeout <= max) {
21 auto nfds = poll(pfds.data(), pfds.size(), timeout);
22
23 if (nfds == -1) {
24 perror("Poll::() poll()");
25 return false;
26 }
27
28 /* timeout */
29 if (!nfds) {
30 timeout = ceil(static_cast<float>(timeout) * growth);
31 continue;
32 }
33
34 auto pred = [](const pollfd &pfd){
35 return pfd.revents & POLLHUP
36 || pfd.revents & POLLERR
37 || pfd.revents & pfd.events;
38 };
39 auto iter = remove_if(pfds.begin(), pfds.end(), pred);
40 pfds.erase(iter, pfds.end());
41 }
42
43 return pfds.empty();
44 }