dev-async 0.9.1

Async-specific validation for Rust. Deadlocks, task leaks, hung futures, graceful shutdown. Part of the dev-* verification suite.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! # dev-async
//!
//! Async-specific validation for Rust. Deadlocks, task leaks, hung
//! futures, graceful shutdown. Part of the `dev-*` verification suite.
//!
//! Async Rust fails in subtle ways that synchronous unit tests can't
//! catch: a future that never completes, a task that gets dropped
//! without cleanup, a shutdown that hangs because one worker is stuck
//! in a blocking call. `dev-async` provides primitives for catching
//! these issues programmatically.
//!
//! ## Quick example
//!
//! Run a future with a hard timeout. If it doesn't finish in time, you
//! get a `Fail` verdict, not a hang.
//!
//! ```no_run
//! use dev_async::run_with_timeout;
//! use std::time::Duration;
//!
//! # async fn example() {
//! let _check = run_with_timeout(
//!     "user_login",
//!     Duration::from_secs(2),
//!     async { do_login().await }
//! ).await;
//! # }
//! # async fn do_login() {}
//! ```
//!
//! ## Modules
//!
//! - [`deadlock`] — `try_lock_with_timeout` helpers.
//! - [`tasks`] — `TrackedTaskGroup` for leak detection.
//! - [`shutdown`] — `ShutdownProbe` for graceful-shutdown verification.
//! - `blocking` (feature `block-detect`) — heuristic blocking-call
//!   detection inside async tasks (visible in rustdoc when the
//!   feature is enabled).

#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]

use std::future::Future;
use std::time::{Duration, Instant};

use dev_report::{CheckResult, Evidence, Producer, Report, Severity};

pub mod deadlock;
pub mod shutdown;
pub mod tasks;

#[cfg(feature = "block-detect")]
#[cfg_attr(docsrs, doc(cfg(feature = "block-detect")))]
pub mod blocking;

/// Run a future with a hard timeout. Produces a [`CheckResult`] tagged
/// `async`.
///
/// If the future completes before the timeout, the verdict is `Pass`
/// and the duration is recorded.
///
/// If the future does not complete in time, the verdict is `Fail` with
/// severity `Error`. The future itself is dropped (cancelled) when the
/// timeout expires.
///
/// The returned `CheckResult` carries numeric `Evidence` for
/// `timeout_ms` and (on the pass path) `elapsed_ms`.
///
/// # Example
///
/// ```no_run
/// use dev_async::run_with_timeout;
/// use std::time::Duration;
///
/// # async fn ex() {
/// let check = run_with_timeout("op", Duration::from_millis(50), async {}).await;
/// assert!(check.has_tag("async"));
/// # }
/// ```
pub async fn run_with_timeout<F, T>(
    name: impl Into<String>,
    timeout: Duration,
    fut: F,
) -> CheckResult
where
    F: Future<Output = T>,
{
    let name = name.into();
    let started = Instant::now();
    match tokio::time::timeout(timeout, fut).await {
        Ok(_value) => {
            let elapsed = started.elapsed();
            let mut c = CheckResult::pass(format!("async::{name}"))
                .with_duration_ms(elapsed.as_millis() as u64);
            c.tags = vec!["async".to_string()];
            c.evidence = vec![
                Evidence::numeric("elapsed_ms", elapsed.as_millis() as f64),
                Evidence::numeric("timeout_ms", timeout.as_millis() as f64),
            ];
            c
        }
        Err(_elapsed) => {
            let mut c = CheckResult::fail(format!("async::{name}"), Severity::Error)
                .with_detail(format!("future did not complete within {timeout:?}"));
            c.tags = vec![
                "async".to_string(),
                "timeout".to_string(),
                "regression".to_string(),
            ];
            c.evidence = vec![Evidence::numeric("timeout_ms", timeout.as_millis() as f64)];
            c
        }
    }
}

/// Verify that all spawned tasks finish within the given timeout.
///
/// Pass a vector of `JoinHandle`s. Returns one [`CheckResult`] per task,
/// each tagged `async` with numeric `Evidence` for the index and
/// timeout / elapsed.
///
/// Verdicts:
/// - Task completed -> `Pass`, with `elapsed_ms` evidence.
/// - Task panicked or was cancelled -> `Fail (Critical)`, with
///   `task_panicked` tag.
/// - Task did not finish in time -> `Fail (Error)`, with `timeout` tag.
pub async fn join_all_with_timeout<T>(
    name: impl Into<String>,
    timeout: Duration,
    handles: Vec<tokio::task::JoinHandle<T>>,
) -> Vec<CheckResult> {
    let name = name.into();
    let mut results = Vec::with_capacity(handles.len());
    for (i, h) in handles.into_iter().enumerate() {
        let task_name = format!("async::{name}::task{i}");
        let started = Instant::now();
        let evidence_base = vec![
            Evidence::numeric("task_index", i as f64),
            Evidence::numeric("timeout_ms", timeout.as_millis() as f64),
        ];
        let result = match tokio::time::timeout(timeout, h).await {
            Ok(Ok(_)) => {
                let elapsed = started.elapsed();
                let mut c =
                    CheckResult::pass(task_name).with_duration_ms(elapsed.as_millis() as u64);
                c.tags = vec!["async".to_string()];
                c.evidence = {
                    let mut e = evidence_base;
                    e.push(Evidence::numeric("elapsed_ms", elapsed.as_millis() as f64));
                    e
                };
                c
            }
            Ok(Err(join_err)) => {
                let mut c = CheckResult::fail(task_name, Severity::Critical)
                    .with_detail(format!("task panicked or was cancelled: {join_err}"));
                c.tags = vec![
                    "async".to_string(),
                    "task_panicked".to_string(),
                    "regression".to_string(),
                ];
                c.evidence = evidence_base;
                c
            }
            Err(_) => {
                let mut c = CheckResult::fail(task_name, Severity::Error)
                    .with_detail(format!("task did not complete within {timeout:?}"));
                c.tags = vec![
                    "async".to_string(),
                    "timeout".to_string(),
                    "regression".to_string(),
                ];
                c.evidence = evidence_base;
                c
            }
        };
        results.push(result);
    }
    results
}

/// A trait for any async harness that produces a verdict via a future.
///
/// `dev-report::Producer` is synchronous, which doesn't fit async
/// harnesses. `AsyncCheck` is the async equivalent.
///
/// # Example
///
/// ```no_run
/// use dev_async::AsyncCheck;
/// use dev_report::CheckResult;
/// use std::future::Future;
/// use std::pin::Pin;
///
/// struct PingCheck;
/// impl AsyncCheck for PingCheck {
///     type Output = CheckResult;
///     type Fut = Pin<Box<dyn Future<Output = CheckResult> + Send>>;
///     fn run(self) -> Self::Fut {
///         Box::pin(async move { CheckResult::pass("ping") })
///     }
/// }
/// ```
pub trait AsyncCheck {
    /// Output of the check. Typically `CheckResult`.
    type Output;
    /// The future returned by `run`.
    type Fut: Future<Output = Self::Output>;
    /// Run the check.
    fn run(self) -> Self::Fut;
}

/// An async producer that builds a `Report`.
///
/// `dev-report::Producer` is synchronous. `AsyncProducer` is the
/// async equivalent that returns a future. Bridge to a sync
/// `Producer` via [`BlockingAsyncProducer`].
pub trait AsyncProducer {
    /// The future returned by `produce`.
    type Fut: Future<Output = Report>;
    /// Run the producer and return a finalized [`Report`].
    fn produce(self) -> Self::Fut;
}

/// Adapter that wraps an `async fn` returning a [`Report`] and
/// implements `dev_report::Producer` by calling
/// `tokio::runtime::Handle::current().block_on(...)`.
///
/// MUST be invoked from a sync context that *is not* itself running
/// inside a `current_thread` runtime. Calling `block_on` from inside
/// an async runtime would deadlock; if you need that, use
/// [`AsyncProducer`] directly without going through `Producer`.
///
/// # Example
///
/// ```no_run
/// use dev_async::{run_with_timeout, BlockingAsyncProducer};
/// use dev_report::{Producer, Report};
/// use std::time::Duration;
///
/// fn build_report() -> impl std::future::Future<Output = Report> {
///     async {
///         let check = run_with_timeout("op", Duration::from_millis(50), async {}).await;
///         let mut r = Report::new("crate", "0.1.0").with_producer("dev-async");
///         r.push(check);
///         r.finish();
///         r
///     }
/// }
///
/// // From a sync test or main:
/// // let rt = tokio::runtime::Runtime::new().unwrap();
/// // let handle = rt.handle().clone();
/// // let producer = BlockingAsyncProducer::new(handle, build_report);
/// // let report = producer.produce();
/// ```
pub struct BlockingAsyncProducer<F, Fut>
where
    F: Fn() -> Fut,
    Fut: Future<Output = Report>,
{
    handle: tokio::runtime::Handle,
    /// Owned runtime, when constructed via `with_new_runtime` /
    /// `with_current_thread_runtime`. `None` when borrowing an
    /// externally-supplied handle. Kept alive for the lifetime of the
    /// producer so `block_on` always has a valid runtime.
    _owned_runtime: Option<tokio::runtime::Runtime>,
    factory: F,
}

impl<F, Fut> BlockingAsyncProducer<F, Fut>
where
    F: Fn() -> Fut,
    Fut: Future<Output = Report>,
{
    /// Build a new adapter bound to an externally-supplied `handle`.
    ///
    /// `factory` is invoked once per `produce()` call and must return
    /// a fresh future each time.
    ///
    /// Use this when you already have a `tokio::runtime::Handle`
    /// (e.g. from a long-lived runtime in your test harness). For the
    /// common case of "I just want to drive an async producer from a
    /// sync test", prefer [`with_new_runtime`](Self::with_new_runtime).
    pub fn new(handle: tokio::runtime::Handle, factory: F) -> Self {
        Self {
            handle,
            _owned_runtime: None,
            factory,
        }
    }

    /// Build a new adapter that owns a fresh multi-thread `tokio::runtime::Runtime`.
    ///
    /// The runtime lives for the lifetime of the producer and is
    /// dropped (along with all its workers) when the producer is
    /// dropped. Use this when you don't already have a runtime and
    /// just want to drive an async producer from sync code.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use dev_async::{run_with_timeout, BlockingAsyncProducer};
    /// use dev_report::{Producer, Report};
    /// use std::time::Duration;
    ///
    /// let producer = BlockingAsyncProducer::with_new_runtime(|| async {
    ///     let check = run_with_timeout("op", Duration::from_millis(50), async {}).await;
    ///     let mut r = Report::new("crate", "0.1.0").with_producer("dev-async");
    ///     r.push(check);
    ///     r.finish();
    ///     r
    /// })
    /// .expect("build runtime");
    /// let _report = producer.produce();
    /// ```
    pub fn with_new_runtime(factory: F) -> std::io::Result<Self> {
        let rt = tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()?;
        let handle = rt.handle().clone();
        Ok(Self {
            handle,
            _owned_runtime: Some(rt),
            factory,
        })
    }

    /// Build a new adapter that owns a fresh `current_thread`
    /// `tokio::runtime::Runtime`.
    ///
    /// Lighter-weight than [`with_new_runtime`](Self::with_new_runtime):
    /// no worker threads are spawned. Suitable for tests and
    /// single-threaded harnesses.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use dev_async::BlockingAsyncProducer;
    /// use dev_report::{Producer, Report};
    ///
    /// let producer = BlockingAsyncProducer::with_current_thread_runtime(|| async {
    ///     Report::new("c", "0.1.0").with_producer("dev-async")
    /// })
    /// .expect("build runtime");
    /// let _r = producer.produce();
    /// ```
    pub fn with_current_thread_runtime(factory: F) -> std::io::Result<Self> {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()?;
        let handle = rt.handle().clone();
        Ok(Self {
            handle,
            _owned_runtime: Some(rt),
            factory,
        })
    }
}

impl<F, Fut> Producer for BlockingAsyncProducer<F, Fut>
where
    F: Fn() -> Fut,
    Fut: Future<Output = Report>,
{
    fn produce(&self) -> Report {
        let fut = (self.factory)();
        self.handle.block_on(fut)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use dev_report::Verdict;

    #[tokio::test]
    async fn timeout_pass_fast_future() {
        let check = run_with_timeout("fast", Duration::from_millis(500), async {}).await;
        assert_eq!(check.verdict, Verdict::Pass);
        assert!(check.has_tag("async"));
        let labels: Vec<&str> = check.evidence.iter().map(|e| e.label.as_str()).collect();
        assert!(labels.contains(&"elapsed_ms"));
        assert!(labels.contains(&"timeout_ms"));
    }

    #[tokio::test]
    async fn timeout_fail_slow_future() {
        let check = run_with_timeout("slow", Duration::from_millis(10), async {
            tokio::time::sleep(Duration::from_millis(200)).await;
        })
        .await;
        assert_eq!(check.verdict, Verdict::Fail);
        assert!(check.has_tag("timeout"));
        assert!(check.has_tag("regression"));
    }

    #[tokio::test]
    async fn join_all_basic() {
        let h1 = tokio::spawn(async { 1 });
        let h2 = tokio::spawn(async { 2 });
        let results = join_all_with_timeout("g", Duration::from_secs(1), vec![h1, h2]).await;
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.verdict == Verdict::Pass));
        assert!(results.iter().all(|r| r.has_tag("async")));
    }

    #[tokio::test]
    async fn join_all_panic_is_critical() {
        let h = tokio::spawn(async { panic!("oops") });
        let results = join_all_with_timeout("g", Duration::from_secs(1), vec![h]).await;
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].verdict, Verdict::Fail);
        assert_eq!(results[0].severity, Some(Severity::Critical));
        assert!(results[0].has_tag("task_panicked"));
    }

    #[tokio::test]
    async fn join_all_timeout_is_error() {
        let h = tokio::spawn(async {
            tokio::time::sleep(Duration::from_millis(500)).await;
        });
        let results = join_all_with_timeout("g", Duration::from_millis(20), vec![h]).await;
        assert_eq!(results[0].verdict, Verdict::Fail);
        assert_eq!(results[0].severity, Some(Severity::Error));
        assert!(results[0].has_tag("timeout"));
    }

    #[tokio::test]
    async fn check_evidence_includes_timeout() {
        let check = run_with_timeout("x", Duration::from_millis(50), async {}).await;
        let timeout_evidence = check
            .evidence
            .iter()
            .find(|e| e.label == "timeout_ms")
            .expect("timeout_ms evidence present");
        // The exact match isn't crucial; just ensure shape is numeric.
        if let dev_report::EvidenceData::Numeric(n) = timeout_evidence.data {
            assert_eq!(n, 50.0);
        } else {
            panic!("expected numeric");
        }
    }

    #[test]
    fn blocking_async_producer_with_new_runtime() {
        let producer = BlockingAsyncProducer::with_new_runtime(|| async {
            let mut r = Report::new("c", "0.1.0").with_producer("dev-async");
            r.push(dev_report::CheckResult::pass("x"));
            r.finish();
            r
        })
        .expect("build runtime");
        let report = producer.produce();
        assert_eq!(report.checks.len(), 1);
        assert_eq!(report.overall_verdict(), Verdict::Pass);
    }

    #[test]
    fn blocking_async_producer_with_current_thread_runtime() {
        let producer = BlockingAsyncProducer::with_current_thread_runtime(|| async {
            let mut r = Report::new("c", "0.1.0").with_producer("dev-async");
            r.push(dev_report::CheckResult::pass("y"));
            r.finish();
            r
        })
        .expect("build runtime");
        let report = producer.produce();
        assert_eq!(report.checks.len(), 1);
    }

    #[test]
    fn blocking_async_producer_can_drive_run_with_timeout() {
        let producer = BlockingAsyncProducer::with_current_thread_runtime(|| async {
            let check = run_with_timeout("op", Duration::from_millis(50), async {}).await;
            let mut r = Report::new("c", "0.1.0").with_producer("dev-async");
            r.push(check);
            r.finish();
            r
        })
        .expect("build runtime");
        let report = producer.produce();
        assert!(matches!(report.overall_verdict(), Verdict::Pass));
    }
}