accept fdread event as valid behavior of stdin poll

This commit is contained in:
Pat Hickey
2021-02-01 15:25:00 -08:00
parent 0c4aec391e
commit bb3e391a27
2 changed files with 20 additions and 28 deletions

View File

@@ -1,5 +1,4 @@
use anyhow::Context;
use std::convert::TryInto;
use std::path::Path;
use wasi_cap_std_sync::WasiCtxBuilder;
use wasi_common::pipe::{ReadPipe, WritePipe};
@@ -89,21 +88,6 @@ pub fn instantiate_inherit_stdio(
let r = {
let store = Store::default();
// Tests assume that stdin does not have any bytes available to read. Make sure this is the
// case, regardless of the test environment:
use std::io::Read;
use system_interface::io::ReadReady;
let nbytes = std::io::stdin()
.num_ready_bytes()
.expect("get stdin's ready bytes");
if nbytes > 0 {
let mut stdin_contents = Vec::new();
stdin_contents.resize(nbytes.try_into().expect("ready bytes fits in memory"), 0);
std::io::stdin()
.read(stdin_contents.as_mut_slice())
.expect("read stdin to end");
}
// Create our wasi context.
// Additionally register any preopened directories if we have them.
let mut builder = WasiCtxBuilder::new();

View File

@@ -2,6 +2,7 @@ use std::mem::MaybeUninit;
use wasi_tests::{assert_errno, STDERR_FD, STDIN_FD, STDOUT_FD};
const CLOCK_ID: wasi::Userdata = 0x0123_45678;
const STDIN_ID: wasi::Userdata = 0x8765_43210;
unsafe fn poll_oneoff_impl(r#in: &[wasi::Subscription]) -> Result<Vec<wasi::Event>, wasi::Error> {
let mut out: Vec<wasi::Event> = Vec::new();
@@ -23,6 +24,7 @@ unsafe fn test_stdin_read() {
let fd_readwrite = wasi::SubscriptionFdReadwrite {
file_descriptor: STDIN_FD,
};
// Either stdin can be ready for reading, or this poll can timeout.
let r#in = [
wasi::Subscription {
userdata: CLOCK_ID,
@@ -31,9 +33,8 @@ unsafe fn test_stdin_read() {
u: wasi::SubscriptionUU { clock },
},
},
// Make sure that timeout is returned only once even if there are multiple read events
wasi::Subscription {
userdata: 1,
userdata: STDIN_ID,
u: wasi::SubscriptionU {
tag: wasi::EVENTTYPE_FD_READ,
u: wasi::SubscriptionUU {
@@ -43,18 +44,25 @@ unsafe fn test_stdin_read() {
},
];
let out = poll_oneoff_impl(&r#in).unwrap();
// The result should be either a timeout, or that stdin is ready for reading.
// Both are valid behaviors that depend on the test environment.
assert_eq!(out.len(), 1, "should return 1 event");
let event = &out[0];
assert_errno!(event.error, wasi::ERRNO_SUCCESS);
assert_eq!(
event.r#type,
wasi::EVENTTYPE_CLOCK,
"the event.type should equal clock"
);
assert_eq!(
event.userdata, CLOCK_ID,
"the event.userdata should contain clock_id specified by the user"
);
if event.r#type == wasi::EVENTTYPE_CLOCK {
assert_errno!(event.error, wasi::ERRNO_SUCCESS);
assert_eq!(
event.userdata, CLOCK_ID,
"the event.userdata should contain CLOCK_ID",
);
} else if event.r#type == wasi::EVENTTYPE_FD_READ {
assert_errno!(event.error, wasi::ERRNO_SUCCESS);
assert_eq!(
event.userdata, STDIN_ID,
"the event.userdata should contain STDIN_ID",
);
} else {
panic!("unexpected event type {}", event.r#type);
}
}
unsafe fn test_stdout_stderr_write() {