poll oneoff tests: what if we read a non-empty file?
This commit is contained in:
@@ -150,16 +150,37 @@ unsafe fn test_fd_readwrite(readable_fd: wasi::Fd, writable_fd: wasi::Fd, error_
|
|||||||
|
|
||||||
unsafe fn test_fd_readwrite_valid_fd(dir_fd: wasi::Fd) {
|
unsafe fn test_fd_readwrite_valid_fd(dir_fd: wasi::Fd) {
|
||||||
// Create a file in the scratch directory.
|
// Create a file in the scratch directory.
|
||||||
let readable_fd = wasi::path_open(
|
let nonempty_file = wasi::path_open(
|
||||||
dir_fd,
|
dir_fd,
|
||||||
0,
|
0,
|
||||||
"readable_file",
|
"readable_file",
|
||||||
wasi::OFLAGS_CREAT,
|
wasi::OFLAGS_CREAT,
|
||||||
|
wasi::RIGHTS_FD_WRITE,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
.expect("create writable file");
|
||||||
|
// Write to file
|
||||||
|
let contents = &[1u8];
|
||||||
|
let ciovec = wasi::Ciovec {
|
||||||
|
buf: contents.as_ptr() as *const _,
|
||||||
|
buf_len: contents.len(),
|
||||||
|
};
|
||||||
|
wasi::fd_write(nonempty_file, &[ciovec]).expect("write");
|
||||||
|
wasi::fd_close(nonempty_file).expect("close");
|
||||||
|
|
||||||
|
// Now open the file for reading
|
||||||
|
let readable_fd = wasi::path_open(
|
||||||
|
dir_fd,
|
||||||
|
0,
|
||||||
|
"readable_file",
|
||||||
|
0,
|
||||||
wasi::RIGHTS_FD_READ | wasi::RIGHTS_POLL_FD_READWRITE,
|
wasi::RIGHTS_FD_READ | wasi::RIGHTS_POLL_FD_READWRITE,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
.expect("opening a readable file");
|
.expect("opening a readable file");
|
||||||
|
|
||||||
assert_gt!(
|
assert_gt!(
|
||||||
readable_fd,
|
readable_fd,
|
||||||
libc::STDERR_FILENO as wasi::Fd,
|
libc::STDERR_FILENO as wasi::Fd,
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ pub enum Subscription<'a> {
|
|||||||
MonotonicClock(MonotonicClockSubscription<'a>),
|
MonotonicClock(MonotonicClockSubscription<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum SubscriptionResult {
|
pub enum SubscriptionResult {
|
||||||
Read(Result<(u64, RwEventFlags), Error>),
|
Read(Result<(u64, RwEventFlags), Error>),
|
||||||
Write(Result<(u64, RwEventFlags), Error>),
|
Write(Result<(u64, RwEventFlags), Error>),
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use anyhow::{Context, Error};
|
|||||||
use wasi_common::{
|
use wasi_common::{
|
||||||
file::{FdFlags, OFlags},
|
file::{FdFlags, OFlags},
|
||||||
sched::{Poll, RwEventFlags, SubscriptionResult, Userdata},
|
sched::{Poll, RwEventFlags, SubscriptionResult, Userdata},
|
||||||
WasiDir,
|
WasiDir, WasiFile,
|
||||||
};
|
};
|
||||||
use wasi_tokio::{sched::poll_oneoff, Dir};
|
use wasi_tokio::{sched::poll_oneoff, Dir};
|
||||||
|
|
||||||
@@ -13,24 +13,34 @@ async fn empty_file_readable() -> Result<(), Error> {
|
|||||||
let d = workspace.open_dir("d").context("open dir")?;
|
let d = workspace.open_dir("d").context("open dir")?;
|
||||||
let d = Dir::from_cap_std(d);
|
let d = Dir::from_cap_std(d);
|
||||||
|
|
||||||
let mut readable_f = d
|
let mut f = d
|
||||||
.open_file(false, "f", OFlags::CREATE, true, false, FdFlags::empty())
|
.open_file(false, "f", OFlags::CREATE, false, true, FdFlags::empty())
|
||||||
.await
|
.await
|
||||||
.context("create readable file")?;
|
.context("create writable file f")?;
|
||||||
|
let to_write: Vec<u8> = vec![0];
|
||||||
|
f.write_vectored(&vec![std::io::IoSlice::new(&to_write)])
|
||||||
|
.await
|
||||||
|
.context("write to f")?;
|
||||||
|
drop(f);
|
||||||
|
|
||||||
|
let mut f = d
|
||||||
|
.open_file(false, "f", OFlags::empty(), true, false, FdFlags::empty())
|
||||||
|
.await
|
||||||
|
.context("open f as readable")?;
|
||||||
|
|
||||||
let mut poll = Poll::new();
|
let mut poll = Poll::new();
|
||||||
poll.subscribe_read(&mut *readable_f, Userdata::from(123));
|
poll.subscribe_read(&mut *f, Userdata::from(123));
|
||||||
poll_oneoff(&mut poll).await?;
|
poll_oneoff(&mut poll).await?;
|
||||||
|
|
||||||
let events = poll.results();
|
let events = poll.results();
|
||||||
|
|
||||||
assert_eq!(events.len(), 1);
|
assert_eq!(events.len(), 1, "expected 1 event, got: {:?}", events);
|
||||||
match events[0] {
|
match events[0] {
|
||||||
(SubscriptionResult::Read(Ok((0, flags))), ud) => {
|
(SubscriptionResult::Read(Ok((1, flags))), ud) => {
|
||||||
assert_eq!(flags, RwEventFlags::empty());
|
assert_eq!(flags, RwEventFlags::empty());
|
||||||
assert_eq!(ud, Userdata::from(123));
|
assert_eq!(ud, Userdata::from(123));
|
||||||
}
|
}
|
||||||
_ => panic!(""),
|
_ => panic!("expected (Read(Ok(1, empty), 123), got: {:?}", events[0]),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user