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:
@@ -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,
|
||||||
};
|
|
||||||
let r#in = [
|
|
||||||
wasi::Subscription {
|
|
||||||
userdata: 1,
|
|
||||||
u: wasi::SubscriptionU {
|
u: wasi::SubscriptionU {
|
||||||
tag: wasi::EVENTTYPE_FD_WRITE,
|
tag: wasi::EVENTTYPE_CLOCK,
|
||||||
u: wasi::SubscriptionUU {
|
u: wasi::SubscriptionUU {
|
||||||
fd_write: stdout_readwrite,
|
clock: wasi::SubscriptionClock {
|
||||||
|
id: wasi::CLOCKID_MONOTONIC,
|
||||||
|
timeout: 5_000_000u64, // 5 milliseconds
|
||||||
|
precision: 0,
|
||||||
|
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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user