Skip to main content

run_with_timeout/
run_with_timeout.rs

1//! Run two futures under a hard timeout — one that finishes in time, one
2//! that does not — and print the resulting `CheckResult`s.
3//!
4//! ```text
5//! cargo run --example run_with_timeout
6//! ```
7//!
8//! Demonstrates the headline `dev_async::run_with_timeout` helper:
9//! finished futures pass with elapsed-time evidence, hung futures get a
10//! `Fail (Error)` verdict with a "timeout" tag — the calling task never
11//! hangs.
12
13use std::time::Duration;
14
15use dev_async::run_with_timeout;
16
17#[tokio::main(flavor = "current_thread")]
18async fn main() {
19    let fast = run_with_timeout("fast_op", Duration::from_millis(500), async {
20        tokio::time::sleep(Duration::from_millis(10)).await;
21    })
22    .await;
23    println!(
24        "fast:    {:?}  tags={:?}  duration={:?}ms",
25        fast.verdict, fast.tags, fast.duration_ms
26    );
27
28    let hung = run_with_timeout("hung_op", Duration::from_millis(50), async {
29        tokio::time::sleep(Duration::from_secs(60)).await;
30    })
31    .await;
32    println!(
33        "hung:    {:?}  tags={:?}  severity={:?}",
34        hung.verdict, hung.tags, hung.severity
35    );
36    if let Some(detail) = &hung.detail {
37        println!("         detail: {}", detail);
38    }
39}