#timeout #shutdown #test #should-panic #task #max

macro tokio-test-shutdown-timeout

Tokio test attribute with a shutdown timeout

1 unstable release

0.0.2 Feb 23, 2025

#634 in Testing

Download history 127/week @ 2025-02-23 13/week @ 2025-03-02 32/week @ 2025-03-09 29/week @ 2025-03-16 5/week @ 2025-03-23 4/week @ 2025-03-30 60/week @ 2025-04-06

114 downloads per month

MIT license

6KB
58 lines

tokio-test-shutdown-timeout

A wrapper for #[tokio::test] that allows specifying a shutdown timeout.

Useful in situations where normal tokio tests hang because dropping the tokio runtime waits forever for all tasks to finish.

Usage

#[tokio_test_shutdown_timeout::test(1)]
#[should_panic]
async fn minimal_async_test() {

    // Start a forever-running blocking tokio task
    tokio::task::spawn_blocking(|| thread::sleep(Duration::MAX));

    // panic to fail the test
    panic!();
    
    // Normal tokio::test would block here, as dropping the tokio runtime
    // blocks until all tasks quit, and we have a task that never quits.
    // This custom version uses tokio's shutdown_timeout to avoid the deadlock.
}

Dependencies

~200–630KB
~15K SLoC