use std::time;
use context_async::{Context, Timer};
async fn a_very_heavy_task() {
tokio::time::sleep(tokio::time::Duration::from_secs(100)).await;
}
#[tokio::main]
async fn main() {
let root = Timer::with_timeout(tokio::time::Duration::from_secs(10));
let root2 = root.clone(); let root_job = tokio::spawn(async move {
let result = root2.handle(a_very_heavy_task()).await;
println!("root2 result: {:?}", result);
});
let t1 = root.spawn().await; let t1_to_cancel = t1.clone(); let t11 = t1.spawn_with_timeout(time::Duration::from_secs(5)).await; let t11_to_cancel = t11.clone();
let j1 = tokio::spawn(async move {
let result = t1.handle(a_very_heavy_task()).await;
println!("t1 result: {:?}", result);
});
let j11 = tokio::spawn(async move {
let result = t11.handle(a_very_heavy_task()).await;
println!("t11 result: {:?}", result);
});
t1_to_cancel.cancel().await;
assert!(t1_to_cancel.is_cancelled().await);
assert!(t11_to_cancel.is_cancelled().await);
assert!( ! root.is_cancelled().await );
root_job.await.unwrap();
j1.await.unwrap();
j11.await.unwrap();
}