better unit test
This commit is contained in:
@@ -5,7 +5,9 @@ use cap_std::time::Instant;
|
|||||||
pub mod subscription;
|
pub mod subscription;
|
||||||
pub use cap_std::time::Duration;
|
pub use cap_std::time::Duration;
|
||||||
|
|
||||||
use subscription::{MonotonicClockSubscription, RwSubscription, Subscription, SubscriptionResult};
|
pub use subscription::{
|
||||||
|
MonotonicClockSubscription, RwEventFlags, RwSubscription, Subscription, SubscriptionResult,
|
||||||
|
};
|
||||||
|
|
||||||
#[wiggle::async_trait]
|
#[wiggle::async_trait]
|
||||||
pub trait WasiSched {
|
pub trait WasiSched {
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
use anyhow::{Context, Error};
|
use anyhow::{Context, Error};
|
||||||
use std::ops::Deref;
|
|
||||||
use wasi_common::{
|
use wasi_common::{
|
||||||
file::{FdFlags, OFlags},
|
file::{FdFlags, OFlags},
|
||||||
sched::{Poll, Userdata},
|
sched::{Poll, RwEventFlags, SubscriptionResult, Userdata},
|
||||||
WasiDir, WasiFile,
|
WasiDir,
|
||||||
};
|
};
|
||||||
use wasi_tokio::{sched::poll_oneoff, Dir, File};
|
use wasi_tokio::{sched::poll_oneoff, Dir};
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread")]
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
async fn main() -> Result<(), Error> {
|
async fn empty_file_readable() -> Result<(), Error> {
|
||||||
let workspace = unsafe { cap_tempfile::tempdir().expect("create tempdir") };
|
let workspace = unsafe { cap_tempfile::tempdir().expect("create tempdir") };
|
||||||
workspace.create_dir("d").context("create dir")?;
|
workspace.create_dir("d").context("create dir")?;
|
||||||
let d = workspace.open_dir("d").context("open dir")?;
|
let d = workspace.open_dir("d").context("open dir")?;
|
||||||
@@ -21,7 +20,48 @@ async fn main() -> Result<(), Error> {
|
|||||||
|
|
||||||
let mut poll = Poll::new();
|
let mut poll = Poll::new();
|
||||||
poll.subscribe_read(&mut *readable_f, Userdata::from(123));
|
poll.subscribe_read(&mut *readable_f, Userdata::from(123));
|
||||||
let poll_events = poll_oneoff(&mut poll).await?;
|
poll_oneoff(&mut poll).await?;
|
||||||
|
|
||||||
|
let events = poll.results();
|
||||||
|
|
||||||
|
assert_eq!(events.len(), 1);
|
||||||
|
match events[0] {
|
||||||
|
(SubscriptionResult::Read(Ok((0, flags))), ud) => {
|
||||||
|
assert_eq!(flags, RwEventFlags::empty());
|
||||||
|
assert_eq!(ud, Userdata::from(123));
|
||||||
|
}
|
||||||
|
_ => panic!(""),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
|
async fn empty_file_writable() -> Result<(), Error> {
|
||||||
|
let workspace = unsafe { cap_tempfile::tempdir().expect("create tempdir") };
|
||||||
|
workspace.create_dir("d").context("create dir")?;
|
||||||
|
let d = workspace.open_dir("d").context("open dir")?;
|
||||||
|
let d = Dir::from_cap_std(d);
|
||||||
|
|
||||||
|
let mut writable_f = d
|
||||||
|
.open_file(false, "f", OFlags::CREATE, true, true, FdFlags::empty())
|
||||||
|
.await
|
||||||
|
.context("create writable file")?;
|
||||||
|
|
||||||
|
let mut poll = Poll::new();
|
||||||
|
poll.subscribe_write(&mut *writable_f, Userdata::from(123));
|
||||||
|
poll_oneoff(&mut poll).await?;
|
||||||
|
|
||||||
|
let events = poll.results();
|
||||||
|
|
||||||
|
assert_eq!(events.len(), 1);
|
||||||
|
match events[0] {
|
||||||
|
(SubscriptionResult::Write(Ok((0, flags))), ud) => {
|
||||||
|
assert_eq!(flags, RwEventFlags::empty());
|
||||||
|
assert_eq!(ud, Userdata::from(123));
|
||||||
|
}
|
||||||
|
_ => panic!(""),
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user