|
| 1 | +extern crate wit_bindgen; |
| 2 | + |
| 3 | +wit_bindgen::generate!({ |
| 4 | + inline: r" |
| 5 | + package test:test; |
| 6 | +
|
| 7 | + world test { |
| 8 | + include wasi:clocks/imports@0.3.0-rc-2025-09-16; |
| 9 | + include wasi:cli/command@0.3.0-rc-2025-09-16; |
| 10 | + } |
| 11 | +", |
| 12 | + // Work around https://github.com/bytecodealliance/wasm-tools/issues/2285. |
| 13 | + features:["clocks-timezone"], |
| 14 | + generate_all |
| 15 | +}); |
| 16 | + |
| 17 | +use core::future::Future; |
| 18 | +use core::pin::pin; |
| 19 | +use core::task::{Context, Poll, Waker}; |
| 20 | +use wasi::clocks::monotonic_clock; |
| 21 | + |
| 22 | +struct Component; |
| 23 | +export!(Component); |
| 24 | +impl exports::wasi::cli::run::Guest for Component { |
| 25 | + async fn run() -> Result<(), ()> { |
| 26 | + sleep_10ms().await; |
| 27 | + sleep_0ms(); |
| 28 | + sleep_backwards_in_time(); |
| 29 | + Ok(()) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +async fn sleep_10ms() { |
| 34 | + let dur = 10_000_000; |
| 35 | + monotonic_clock::wait_until(monotonic_clock::now() + dur).await; |
| 36 | + monotonic_clock::wait_for(dur).await; |
| 37 | +} |
| 38 | + |
| 39 | +fn sleep_0ms() { |
| 40 | + let mut cx = Context::from_waker(Waker::noop()); |
| 41 | + |
| 42 | + assert_eq!( |
| 43 | + pin!(monotonic_clock::wait_until(monotonic_clock::now())).poll(&mut cx), |
| 44 | + Poll::Ready(()), |
| 45 | + "waiting until now() is ready immediately", |
| 46 | + ); |
| 47 | + assert_eq!( |
| 48 | + pin!(monotonic_clock::wait_for(0)).poll(&mut cx), |
| 49 | + Poll::Ready(()), |
| 50 | + "waiting for 0 is ready immediately", |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +fn sleep_backwards_in_time() { |
| 55 | + let mut cx = Context::from_waker(Waker::noop()); |
| 56 | + |
| 57 | + assert_eq!( |
| 58 | + pin!(monotonic_clock::wait_until(monotonic_clock::now() - 1)).poll(&mut cx), |
| 59 | + Poll::Ready(()), |
| 60 | + "waiting until instant which has passed is ready immediately", |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +fn main() {} |
| 65 | + |
0 commit comments