|
2 | 2 | #include <poll.h>
|
3 | 3 | #include <time.h>
|
4 | 4 | #include <unistd.h>
|
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
5 | 7 |
|
6 | 8 | int main(void) {
|
7 |
| - struct pollfd fds[2]; |
| 9 | + struct pollfd fds[4]; |
8 | 10 | time_t before, now;
|
9 | 11 | int ret;
|
| 12 | + char* platform; |
| 13 | + int is_aix; |
| 14 | + int is_win; |
10 | 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. |
11 | 47 | fds[0] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
|
12 | 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}; |
13 | 51 |
|
14 | 52 | ret = poll(fds, 2, -1);
|
15 | 53 | assert(ret == 2);
|
16 | 54 | assert(fds[0].revents == POLLOUT);
|
17 | 55 | assert(fds[1].revents == POLLOUT);
|
| 56 | + assert(fds[2].revents == 0); |
| 57 | + assert(fds[3].revents == 0); |
18 | 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. |
19 | 63 | fds[0] = (struct pollfd){.fd = 0, .events = POLLIN, .revents = 0};
|
20 |
| - time(&before); |
21 | 64 | ret = poll(fds, 1, 2000);
|
22 |
| - time(&now); |
23 |
| - assert(ret == 0); |
24 |
| - assert(now - before >= 2); |
| 65 | + assert(ret == 1); |
25 | 66 |
|
26 |
| - sleep(1); |
27 |
| - time(&now); |
28 |
| - assert(now - before >= 3); |
| 67 | + if (is_aix) |
| 68 | + assert(fds[0].revents == POLLIN); |
| 69 | + else |
| 70 | + assert(fds[0].revents == (POLLHUP | POLLIN)); |
29 | 71 |
|
30 | 72 | return 0;
|
31 | 73 | }
|
0 commit comments