Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi: refactor and enable poll_oneoff() test #33521

Merged
merged 2 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deps/uvwasi/src/poll_oneoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ uvwasi_errno_t uvwasi__poll_oneoff_state_cleanup(
for (i = 0; i < state->handle_cnt; i++)
uv_close((uv_handle_t*) &state->poll_handles[i], NULL);

uv_run(&state->loop, UV_RUN_NOWAIT);

state->max_fds = 0;
state->fdevent_cnt = 0;
state->handle_cnt = 0;
Expand Down
58 changes: 50 additions & 8 deletions test/wasi/c/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,72 @@
#include <poll.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
struct pollfd fds[2];
struct pollfd fds[4];
time_t before, now;
int ret;
char* platform;
int is_aix;
int is_win;

platform = getenv("NODE_PLATFORM");
is_aix = platform != NULL && 0 == strcmp(platform, "aix");
is_win = platform != NULL && 0 == strcmp(platform, "win32");

// Test sleep() behavior.
time(&before);
sleep(1);
time(&now);
assert(now - before >= 1);

// Test poll() timeout behavior.
fds[0] = (struct pollfd){.fd = -1, .events = 0, .revents = 0};
time(&before);
ret = poll(fds, 1, 2000);
time(&now);
assert(ret == 0);
assert(now - before >= 2);

// The rest of the test is unsupported on Windows.
if (is_win)
return 0;

fds[0] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
fds[1] = (struct pollfd){.fd = 2, .events = POLLOUT, .revents = 0};

ret = poll(fds, 2, -1);
assert(ret == 2);
assert(fds[0].revents == POLLOUT);
assert(fds[1].revents == POLLOUT);

// Make a poll() call with duplicate file descriptors.
fds[0] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
fds[1] = (struct pollfd){.fd = 2, .events = POLLOUT, .revents = 0};
fds[2] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
fds[3] = (struct pollfd){.fd = 1, .events = POLLIN, .revents = 0};

ret = poll(fds, 2, -1);
assert(ret == 2);
assert(fds[0].revents == POLLOUT);
assert(fds[1].revents == POLLOUT);
assert(fds[2].revents == 0);
assert(fds[3].revents == 0);

// The original version of this test expected a timeout and return value of
// zero. In the Node test suite, STDIN is not a TTY, and poll() returns one,
// with revents = POLLHUP | POLLIN, except on AIX whose poll() does not
// support POLLHUP.
fds[0] = (struct pollfd){.fd = 0, .events = POLLIN, .revents = 0};
time(&before);
ret = poll(fds, 1, 2000);
time(&now);
assert(ret == 0);
assert(now - before >= 2);
assert(ret == 1);

sleep(1);
time(&now);
assert(now - before >= 3);
if (is_aix)
assert(fds[0].revents == POLLIN);
else
assert(fds[0].revents == (POLLHUP | POLLIN));

return 0;
}
10 changes: 8 additions & 2 deletions test/wasi/test-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ if (process.argv[2] === 'wasi-child') {

function runWASI(options) {
console.log('executing', options.test);
const opts = { env: { ...process.env, NODE_DEBUG_NATIVE: 'wasi' } };
const opts = {
env: {
...process.env,
NODE_DEBUG_NATIVE: 'wasi',
NODE_PLATFORM: process.platform
}
};

if (options.stdin !== undefined)
opts.input = options.stdin;
Expand Down Expand Up @@ -75,7 +81,7 @@ if (process.argv[2] === 'wasi-child') {
runWASI({ test: 'link' });
runWASI({ test: 'main_args' });
runWASI({ test: 'notdir' });
// runWASI({ test: 'poll' });
runWASI({ test: 'poll' });
runWASI({ test: 'preopen_populates' });
runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` });
runWASI({
Expand Down
Binary file modified test/wasi/wasm/poll.wasm
Binary file not shown.