1 #include <assert.h>
2 #include <poll.h>
3 #include <time.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7
main(void)8 int main(void) {
9 struct pollfd fds[4];
10 time_t before, now;
11 int ret;
12 char* platform;
13 int is_aix;
14 int is_win;
15
16 platform = getenv("NODE_PLATFORM");
17 is_aix = platform != NULL && 0 == strcmp(platform, "aix");
18 is_win = platform != NULL && 0 == strcmp(platform, "win32");
19
20 // Test sleep() behavior.
21 time(&before);
22 sleep(1);
23 time(&now);
24 assert(now - before >= 1);
25
26 // Test poll() timeout behavior.
27 fds[0] = (struct pollfd){.fd = -1, .events = 0, .revents = 0};
28 time(&before);
29 ret = poll(fds, 1, 2000);
30 time(&now);
31 assert(ret == 0);
32 assert(now - before >= 2);
33
34 // The rest of the test is unsupported on Windows.
35 if (is_win)
36 return 0;
37
38 fds[0] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
39 fds[1] = (struct pollfd){.fd = 2, .events = POLLOUT, .revents = 0};
40
41 ret = poll(fds, 2, -1);
42 assert(ret == 2);
43 assert(fds[0].revents == POLLOUT);
44 assert(fds[1].revents == POLLOUT);
45
46 // Make a poll() call with duplicate file descriptors.
47 fds[0] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
48 fds[1] = (struct pollfd){.fd = 2, .events = POLLOUT, .revents = 0};
49 fds[2] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
50 fds[3] = (struct pollfd){.fd = 1, .events = POLLIN, .revents = 0};
51
52 ret = poll(fds, 2, -1);
53 assert(ret == 2);
54 assert(fds[0].revents == POLLOUT);
55 assert(fds[1].revents == POLLOUT);
56 assert(fds[2].revents == 0);
57 assert(fds[3].revents == 0);
58
59 // The original version of this test expected a timeout and return value of
60 // zero. In the Node test suite, STDIN is not a TTY, and poll() returns one,
61 // with revents = POLLHUP | POLLIN, except on AIX whose poll() does not
62 // support POLLHUP.
63 fds[0] = (struct pollfd){.fd = 0, .events = POLLIN, .revents = 0};
64 ret = poll(fds, 1, 2000);
65 assert(ret == 1);
66
67 if (is_aix)
68 assert(fds[0].revents == POLLIN);
69 else
70 assert(fds[0].revents == (POLLHUP | POLLIN));
71
72 return 0;
73 }
74