accept fdread event as valid behavior of stdin poll
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use wasi_cap_std_sync::WasiCtxBuilder;
|
use wasi_cap_std_sync::WasiCtxBuilder;
|
||||||
use wasi_common::pipe::{ReadPipe, WritePipe};
|
use wasi_common::pipe::{ReadPipe, WritePipe};
|
||||||
@@ -89,21 +88,6 @@ pub fn instantiate_inherit_stdio(
|
|||||||
let r = {
|
let r = {
|
||||||
let store = Store::default();
|
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.
|
// Create our wasi context.
|
||||||
// Additionally register any preopened directories if we have them.
|
// Additionally register any preopened directories if we have them.
|
||||||
let mut builder = WasiCtxBuilder::new();
|
let mut builder = WasiCtxBuilder::new();
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use std::mem::MaybeUninit;
|
|||||||
use wasi_tests::{assert_errno, STDERR_FD, STDIN_FD, STDOUT_FD};
|
use wasi_tests::{assert_errno, STDERR_FD, STDIN_FD, STDOUT_FD};
|
||||||
|
|
||||||
const CLOCK_ID: wasi::Userdata = 0x0123_45678;
|
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> {
|
unsafe fn poll_oneoff_impl(r#in: &[wasi::Subscription]) -> Result<Vec<wasi::Event>, wasi::Error> {
|
||||||
let mut out: Vec<wasi::Event> = Vec::new();
|
let mut out: Vec<wasi::Event> = Vec::new();
|
||||||
@@ -23,6 +24,7 @@ unsafe fn test_stdin_read() {
|
|||||||
let fd_readwrite = wasi::SubscriptionFdReadwrite {
|
let fd_readwrite = wasi::SubscriptionFdReadwrite {
|
||||||
file_descriptor: STDIN_FD,
|
file_descriptor: STDIN_FD,
|
||||||
};
|
};
|
||||||
|
// Either stdin can be ready for reading, or this poll can timeout.
|
||||||
let r#in = [
|
let r#in = [
|
||||||
wasi::Subscription {
|
wasi::Subscription {
|
||||||
userdata: CLOCK_ID,
|
userdata: CLOCK_ID,
|
||||||
@@ -31,9 +33,8 @@ unsafe fn test_stdin_read() {
|
|||||||
u: wasi::SubscriptionUU { clock },
|
u: wasi::SubscriptionUU { clock },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Make sure that timeout is returned only once even if there are multiple read events
|
|
||||||
wasi::Subscription {
|
wasi::Subscription {
|
||||||
userdata: 1,
|
userdata: STDIN_ID,
|
||||||
u: wasi::SubscriptionU {
|
u: wasi::SubscriptionU {
|
||||||
tag: wasi::EVENTTYPE_FD_READ,
|
tag: wasi::EVENTTYPE_FD_READ,
|
||||||
u: wasi::SubscriptionUU {
|
u: wasi::SubscriptionUU {
|
||||||
@@ -43,18 +44,25 @@ unsafe fn test_stdin_read() {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
let out = poll_oneoff_impl(&r#in).unwrap();
|
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");
|
assert_eq!(out.len(), 1, "should return 1 event");
|
||||||
let event = &out[0];
|
let event = &out[0];
|
||||||
assert_errno!(event.error, wasi::ERRNO_SUCCESS);
|
if event.r#type == wasi::EVENTTYPE_CLOCK {
|
||||||
assert_eq!(
|
assert_errno!(event.error, wasi::ERRNO_SUCCESS);
|
||||||
event.r#type,
|
assert_eq!(
|
||||||
wasi::EVENTTYPE_CLOCK,
|
event.userdata, CLOCK_ID,
|
||||||
"the event.type should equal clock"
|
"the event.userdata should contain CLOCK_ID",
|
||||||
);
|
);
|
||||||
assert_eq!(
|
} else if event.r#type == wasi::EVENTTYPE_FD_READ {
|
||||||
event.userdata, CLOCK_ID,
|
assert_errno!(event.error, wasi::ERRNO_SUCCESS);
|
||||||
"the event.userdata should contain clock_id specified by the user"
|
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() {
|
unsafe fn test_stdout_stderr_write() {
|
||||||
|
|||||||
Reference in New Issue
Block a user