From bb3e391a27bc266889e346f459f140fe5212f975 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Mon, 1 Feb 2021 15:25:00 -0800 Subject: [PATCH] accept fdread event as valid behavior of stdin poll --- .../tests/wasm_tests/runtime/cap_std_sync.rs | 16 ---------- .../wasi-tests/src/bin/poll_oneoff_stdio.rs | 32 ++++++++++++------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs b/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs index df641d23f8..f969218b07 100644 --- a/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs +++ b/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs @@ -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(); diff --git a/crates/test-programs/wasi-tests/src/bin/poll_oneoff_stdio.rs b/crates/test-programs/wasi-tests/src/bin/poll_oneoff_stdio.rs index 6a007c41ff..761b83aa58 100644 --- a/crates/test-programs/wasi-tests/src/bin/poll_oneoff_stdio.rs +++ b/crates/test-programs/wasi-tests/src/bin/poll_oneoff_stdio.rs @@ -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, wasi::Error> { let mut out: Vec = 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() {