use std::time::Duration;
use aiur::toy_rt::{self};
const SLEEP_MODE: toy_rt::SleepMode = toy_rt::SleepMode::Emulated;
struct WaitOnDrop<'rt> {
rt: &'rt toy_rt::Runtime,
drop_length: Duration,
}
impl<'rt> WaitOnDrop<'rt> {
fn new_onesec(rt: &'rt toy_rt::Runtime) -> Self {
Self::new(rt, Duration::from_millis(1000))
}
fn new(rt: &'rt toy_rt::Runtime, drop_length: Duration) -> Self {
Self { rt, drop_length }
}
async fn drop_async(&self) {
toy_rt::sleep(self.rt, self.drop_length).await;
}
}
impl<'rt> Drop for WaitOnDrop<'rt> {
fn drop(&mut self) {
self.rt.nested_loop(self.drop_async());
}
}
#[test]
fn async_drop_proof() {
async fn measured(rt: &toy_rt::Runtime) {
let _long_drop = WaitOnDrop::new_onesec(rt);
}
async fn async_starter(rt: &toy_rt::Runtime, _: ()) {
let start = rt.io().now32();
measured(rt).await;
let elapsed = rt.io().now32() - start;
assert!(elapsed >= 1 * 1000);
assert!(elapsed < 2 * 1000);
}
toy_rt::with_runtime_in_mode(SLEEP_MODE, async_starter, ());
}