Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions tests/rust/wasm32-wasip3/src/bin/clocks-sleep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
extern crate wit_bindgen;

wit_bindgen::generate!({
inline: r"
package test:test;

world test {
include wasi:clocks/imports@0.3.0-rc-2026-02-09;
include wasi:cli/command@0.3.0-rc-2026-02-09;
}
",
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
features:["clocks-timezone"],
generate_all
});
Comment on lines +3 to +15
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Drive-by) I introduced https://github.com/WebAssembly/wasi-testsuite/blob/main/tests/rust/wasm32-wasip3/src/clocks.rs, to avoid repeating the same pattern on each test, perhaps we could make use of that module here as well?


use core::future::Future;
use core::pin::pin;
use core::task::{Context, Poll, Waker};
use wasi::clocks::monotonic_clock;

struct Component;
export!(Component);
impl exports::wasi::cli::run::Guest for Component {
async fn run() -> Result<(), ()> {
sleep_10ms_wait_for().await;
sleep_10ms_wait_until().await;
sleep_0ms();
sleep_backwards_in_time();
Ok(())
}
}

async fn sleep_10ms_wait_for() {
let dur = 10_000_000;
let deadline = monotonic_clock::now() + dur;
monotonic_clock::wait_for(dur).await;
Comment thread
yoshuawuyts marked this conversation as resolved.
assert!(
monotonic_clock::now() >= deadline,
"wait_for never resolves before the deadline"
);
}

async fn sleep_10ms_wait_until() {
let dur = 10_000_000;
let deadline = monotonic_clock::now() + dur;
monotonic_clock::wait_until(deadline).await;
assert!(
monotonic_clock::now() >= deadline,
"wait_until never resolves before the deadline"
);
}

fn sleep_0ms() {
let mut cx = Context::from_waker(Waker::noop());

assert_eq!(
pin!(monotonic_clock::wait_until(monotonic_clock::now())).poll(&mut cx),
Poll::Ready(()),
"waiting until now() is ready immediately",
);
assert_eq!(
pin!(monotonic_clock::wait_for(0)).poll(&mut cx),
Poll::Ready(()),
"waiting for 0 is ready immediately",
);
}

fn sleep_backwards_in_time() {
let mut cx = Context::from_waker(Waker::noop());

assert_eq!(
pin!(monotonic_clock::wait_until(monotonic_clock::now() - 1)).poll(&mut cx),
Poll::Ready(()),
"waiting until instant which has passed is ready immediately",
);
}

fn main() {}
Loading