1 use core::future::Future as _; 2 use core::pin::pin; 3 use core::task::{Context, Poll, Waker}; 4 5 use test_programs::p3::wasi::clocks::monotonic_clock; 6 7 struct Component; 8 9 test_programs::p3::export!(Component); 10 11 impl test_programs::p3::exports::wasi::cli::run::Guest for Component { 12 async fn run() -> Result<(), ()> { 13 sleep_10ms().await; 14 sleep_0ms(); 15 sleep_backwards_in_time(); 16 Ok(()) 17 } 18 } 19 20 async fn sleep_10ms() { 21 let dur = 10_000_000; 22 monotonic_clock::wait_until(monotonic_clock::now() + dur).await; 23 monotonic_clock::wait_for(dur).await; 24 } 25 26 fn sleep_0ms() { 27 let mut cx = Context::from_waker(Waker::noop()); 28 29 assert_eq!( 30 pin!(monotonic_clock::wait_until(monotonic_clock::now())).poll(&mut cx), 31 Poll::Ready(()), 32 "waiting until now() is ready immediately", 33 ); 34 assert_eq!( 35 pin!(monotonic_clock::wait_for(0)).poll(&mut cx), 36 Poll::Ready(()), 37 "waiting for 0 is ready immediately", 38 ); 39 } 40 41 fn sleep_backwards_in_time() { 42 let mut cx = Context::from_waker(Waker::noop()); 43 44 assert_eq!( 45 pin!(monotonic_clock::wait_until(monotonic_clock::now() - 1)).poll(&mut cx), 46 Poll::Ready(()), 47 "waiting until instant which has passed is ready immediately", 48 ); 49 } 50 51 fn main() {} 52