Skip to content

Commit a45430b

Browse files
committed
SR-4026: dispatch_test_check_evfilt_read_for_fd for epoll
Implement dispatch_test_check_evfilt_read_for_fd test method for the event_epoll backend.
1 parent fcc1924 commit a45430b

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

tests/dispatch_test.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#if __has_include(<sys/event.h>)
3232
#define HAS_SYS_EVENT_H 1
3333
#include <sys/event.h>
34+
#else
35+
#include <sys/epoll.h>
36+
#include <sys/eventfd.h>
3437
#endif
3538
#include <assert.h>
3639

@@ -65,8 +68,39 @@ dispatch_test_check_evfilt_read_for_fd(int fd)
6568
close(kq);
6669
return r > 0;
6770
#else
68-
// TODO: Need to write a real check for epoll-backend here
69-
return true;
71+
struct stat sb;
72+
struct epoll_event ev, events[1];
73+
int epollfd, numReady, r;
74+
int dummyfd = -1;
75+
76+
if (fstat(fd, &sb) < 0) {
77+
return false;
78+
}
79+
80+
epollfd = epoll_create1(0);
81+
assert(epollfd != -1);
82+
83+
if (S_ISREG(sb.st_mode)) {
84+
dummyfd = eventfd(1, EFD_CLOEXEC | EFD_NONBLOCK); // dummy eventfd that is always readable
85+
fd = dummyfd;
86+
}
87+
88+
ev.events = EPOLLIN;
89+
ev.data.fd = fd;
90+
r = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
91+
assert(r != -1);
92+
93+
numReady = epoll_wait(epollfd, events, 1, 0);
94+
assert(numReady != -1);
95+
96+
r = close(epollfd);
97+
assert(r != -1);
98+
if (dummyfd != -1) {
99+
r = close(dummyfd);
100+
assert(r != -1);
101+
}
102+
103+
return numReady > 0;
70104
#endif
71105
}
72106

0 commit comments

Comments
 (0)