Skip to content

Commit 5e5be99

Browse files
cjihrigcodebytere
authored andcommitted
wasi: refactor and enable poll_oneoff() test
This commit refactors and enables the poll_oneoff() WASI test. The refactor includes testing additional cases, as well as some platform specific checks. PR-URL: #33521 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jiawen Geng <[email protected]>
1 parent 9ee9688 commit 5e5be99

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

test/wasi/c/poll.c

+50-8
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,72 @@
22
#include <poll.h>
33
#include <time.h>
44
#include <unistd.h>
5+
#include <stdlib.h>
6+
#include <string.h>
57

68
int main(void) {
7-
struct pollfd fds[2];
9+
struct pollfd fds[4];
810
time_t before, now;
911
int ret;
12+
char* platform;
13+
int is_aix;
14+
int is_win;
1015

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.
1147
fds[0] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
1248
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};
1351

1452
ret = poll(fds, 2, -1);
1553
assert(ret == 2);
1654
assert(fds[0].revents == POLLOUT);
1755
assert(fds[1].revents == POLLOUT);
56+
assert(fds[2].revents == 0);
57+
assert(fds[3].revents == 0);
1858

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.
1963
fds[0] = (struct pollfd){.fd = 0, .events = POLLIN, .revents = 0};
20-
time(&before);
2164
ret = poll(fds, 1, 2000);
22-
time(&now);
23-
assert(ret == 0);
24-
assert(now - before >= 2);
65+
assert(ret == 1);
2566

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));
2971

3072
return 0;
3173
}

test/wasi/test-wasi.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ if (process.argv[2] === 'wasi-child') {
3838

3939
function runWASI(options) {
4040
console.log('executing', options.test);
41-
const opts = { env: { ...process.env, NODE_DEBUG_NATIVE: 'wasi' } };
41+
const opts = {
42+
env: {
43+
...process.env,
44+
NODE_DEBUG_NATIVE: 'wasi',
45+
NODE_PLATFORM: process.platform
46+
}
47+
};
4248

4349
if (options.stdin !== undefined)
4450
opts.input = options.stdin;
@@ -75,7 +81,7 @@ if (process.argv[2] === 'wasi-child') {
7581
runWASI({ test: 'link' });
7682
runWASI({ test: 'main_args' });
7783
runWASI({ test: 'notdir' });
78-
// runWASI({ test: 'poll' });
84+
runWASI({ test: 'poll' });
7985
runWASI({ test: 'preopen_populates' });
8086
runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` });
8187
runWASI({

test/wasi/wasm/poll.wasm

2.58 KB
Binary file not shown.

0 commit comments

Comments
 (0)