diff --git a/Cargo.lock b/Cargo.lock index 4fc8a705b905..5fe0859d37dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3077,6 +3077,7 @@ dependencies = [ "libc", "once_cell", "wasi", + "wit-bindgen", ] [[package]] diff --git a/crates/test-programs/tests/wasi-cap-std-sync.rs b/crates/test-programs/tests/wasi-cap-std-sync.rs index 6b7143f3b540..860da98024bb 100644 --- a/crates/test-programs/tests/wasi-cap-std-sync.rs +++ b/crates/test-programs/tests/wasi-cap-std-sync.rs @@ -250,6 +250,11 @@ fn sched_yield() { run("sched_yield", true).unwrap() } #[test_log::test] +#[should_panic] +fn sleep() { + run("sleep", true).unwrap() +} +#[test_log::test] fn stdio() { run("stdio", true).unwrap() } diff --git a/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs b/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs index 05dbd32717d9..639504fbe121 100644 --- a/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs +++ b/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs @@ -294,6 +294,11 @@ async fn sched_yield() { run("sched_yield", false).await.unwrap() } #[test_log::test(tokio::test(flavor = "multi_thread"))] +#[should_panic] +async fn sleep() { + run("sleep", false).await.unwrap() +} +#[test_log::test(tokio::test(flavor = "multi_thread"))] async fn stdio() { run("stdio", false).await.unwrap() } diff --git a/crates/test-programs/tests/wasi-preview2-components-sync.rs b/crates/test-programs/tests/wasi-preview2-components-sync.rs index 08b60217be62..df0c995c1578 100644 --- a/crates/test-programs/tests/wasi-preview2-components-sync.rs +++ b/crates/test-programs/tests/wasi-preview2-components-sync.rs @@ -272,6 +272,10 @@ fn sched_yield() { run("sched_yield", false).unwrap() } #[test_log::test] +fn sleep() { + run("sleep", false).unwrap() +} +#[test_log::test] fn stdio() { run("stdio", false).unwrap() } diff --git a/crates/test-programs/tests/wasi-preview2-components.rs b/crates/test-programs/tests/wasi-preview2-components.rs index 77a3e71ff6d8..ffcda698eeff 100644 --- a/crates/test-programs/tests/wasi-preview2-components.rs +++ b/crates/test-programs/tests/wasi-preview2-components.rs @@ -280,6 +280,10 @@ async fn sched_yield() { run("sched_yield", false).await.unwrap() } #[test_log::test(tokio::test(flavor = "multi_thread"))] +async fn sleep() { + run("sleep", false).await.unwrap() +} +#[test_log::test(tokio::test(flavor = "multi_thread"))] async fn stdio() { run("stdio", false).await.unwrap() } diff --git a/crates/test-programs/tests/wasi-tokio.rs b/crates/test-programs/tests/wasi-tokio.rs index 9f8390c917bd..ae7bc15c0388 100644 --- a/crates/test-programs/tests/wasi-tokio.rs +++ b/crates/test-programs/tests/wasi-tokio.rs @@ -256,6 +256,11 @@ async fn sched_yield() { run("sched_yield", true).await.unwrap() } #[test_log::test(tokio::test(flavor = "multi_thread"))] +#[should_panic] +async fn sleep() { + run("sleep", true).await.unwrap() +} +#[test_log::test(tokio::test(flavor = "multi_thread"))] async fn stdio() { run("stdio", true).await.unwrap() } diff --git a/crates/test-programs/wasi-tests/Cargo.toml b/crates/test-programs/wasi-tests/Cargo.toml index 1cf5c4949bcd..bf72d58ea313 100644 --- a/crates/test-programs/wasi-tests/Cargo.toml +++ b/crates/test-programs/wasi-tests/Cargo.toml @@ -9,3 +9,6 @@ publish = false libc = "0.2.65" wasi = "0.11.0" once_cell = "1.12" +wit-bindgen = { workspace = true, default-features = false, features = [ + "macros", +] } diff --git a/crates/test-programs/wasi-tests/src/bin/sleep.rs b/crates/test-programs/wasi-tests/src/bin/sleep.rs new file mode 100644 index 000000000000..a80f11bbff6a --- /dev/null +++ b/crates/test-programs/wasi-tests/src/bin/sleep.rs @@ -0,0 +1,16 @@ +use crate::wasi::{clocks::monotonic_clock, poll::poll}; + +wit_bindgen::generate!({ + path: "../../wasi/wit", + world: "wasmtime:wasi/command-extended", +}); + +fn main() { + // Sleep ten milliseconds. Note that we call the relevant host functions directly rather than go through + // libstd, since we want to ensure we're calling `monotonic_clock::subscribe` with an `absolute` parameter of + // `true`, which libstd won't necessarily do (but which e.g. CPython _will_ do). + poll::poll_oneoff(&[monotonic_clock::subscribe( + monotonic_clock::now() + 10_000_000, + true, + )]); +} diff --git a/crates/wasi/src/preview2/host/clocks.rs b/crates/wasi/src/preview2/host/clocks.rs index c8c1afc4df87..2c461bd804c8 100644 --- a/crates/wasi/src/preview2/host/clocks.rs +++ b/crates/wasi/src/preview2/host/clocks.rs @@ -64,7 +64,7 @@ impl monotonic_clock::Host for T { })))?) } else { let duration = if absolute { - Duration::from_nanos(clock_now - when) + Duration::from_nanos(when - clock_now) } else { Duration::from_nanos(when) };