poll_oneoff_stdio test: loosen up contract

permit both readable events to be delivered in very short interval,
rather than simultaneously.
This commit is contained in:
Pat Hickey
2021-05-07 14:27:23 -07:00
parent 9b09272936
commit ee8a8a2a90

View File

@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::mem::MaybeUninit; 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};
@@ -46,7 +47,7 @@ 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. // The result should be either a timeout, or that stdin is ready for reading.
// Both are valid behaviors that depend on the test environment. // Both are valid behaviors that depend on the test environment.
assert!(out.len() >= 1, "should return at least 1 event"); assert!(out.len() >= 1, "stdin read should return at least 1 event");
for event in out { for event in out {
if event.r#type == wasi::EVENTTYPE_CLOCK { if event.r#type == wasi::EVENTTYPE_CLOCK {
assert_errno!(event.error, wasi::ERRNO_SUCCESS); assert_errno!(event.error, wasi::ERRNO_SUCCESS);
@@ -66,55 +67,60 @@ unsafe fn test_stdin_read() {
} }
} }
fn writable_subs(h: &HashMap<u64, wasi::Fd>) -> Vec<wasi::Subscription> {
h.iter()
.map(|(ud, fd)| wasi::Subscription {
userdata: *ud,
u: wasi::SubscriptionU {
tag: wasi::EVENTTYPE_FD_WRITE,
u: wasi::SubscriptionUU {
fd_write: wasi::SubscriptionFdReadwrite {
file_descriptor: *fd,
},
},
},
})
.collect()
}
unsafe fn test_stdout_stderr_write() { unsafe fn test_stdout_stderr_write() {
let stdout_readwrite = wasi::SubscriptionFdReadwrite { let mut writable: HashMap<u64, wasi::Fd> =
file_descriptor: STDOUT_FD, vec![(1, STDOUT_FD), (2, STDERR_FD)].into_iter().collect();
};
let stderr_readwrite = wasi::SubscriptionFdReadwrite { let clock = wasi::Subscription {
file_descriptor: STDERR_FD, userdata: CLOCK_ID,
}; u: wasi::SubscriptionU {
let r#in = [ tag: wasi::EVENTTYPE_CLOCK,
wasi::Subscription { u: wasi::SubscriptionUU {
userdata: 1, clock: wasi::SubscriptionClock {
u: wasi::SubscriptionU { id: wasi::CLOCKID_MONOTONIC,
tag: wasi::EVENTTYPE_FD_WRITE, timeout: 5_000_000u64, // 5 milliseconds
u: wasi::SubscriptionUU { precision: 0,
fd_write: stdout_readwrite, flags: 0,
}, },
}, },
}, },
wasi::Subscription { };
userdata: 2, while !writable.is_empty() {
u: wasi::SubscriptionU { let mut subs = writable_subs(&writable);
tag: wasi::EVENTTYPE_FD_WRITE, subs.push(clock.clone());
u: wasi::SubscriptionUU { let out = poll_oneoff_impl(&subs).unwrap();
fd_write: stderr_readwrite, for event in out {
}, match event.userdata {
}, CLOCK_ID => {
}, panic!("timed out with the following pending subs: {:?}", writable)
]; }
let out = poll_oneoff_impl(&r#in).unwrap(); ud => {
assert_eq!(out.len(), 2, "should return 2 events"); if let Some(_) = writable.remove(&ud) {
assert_eq!( assert_eq!(event.r#type, wasi::EVENTTYPE_FD_WRITE);
out[0].userdata, 1, assert_errno!(event.error, wasi::ERRNO_SUCCESS);
"the event.userdata should contain fd userdata specified by the user" } else {
); panic!("Unknown userdata {}, pending sub: {:?}", ud, writable)
assert_errno!(out[0].error, wasi::ERRNO_SUCCESS); }
assert_eq!( }
out[0].r#type, }
wasi::EVENTTYPE_FD_WRITE, }
"the event.type should equal FD_WRITE" }
);
assert_eq!(
out[1].userdata, 2,
"the event.userdata should contain fd userdata specified by the user"
);
assert_errno!(out[1].error, wasi::ERRNO_SUCCESS);
assert_eq!(
out[1].r#type,
wasi::EVENTTYPE_FD_WRITE,
"the event.type should equal FD_WRITE"
);
} }
unsafe fn test_poll_oneoff() { unsafe fn test_poll_oneoff() {