#test #tokio #run-time #shared #test-cases

dev tokio-shared-rt

Allow #[tokio::test] to use a shared runtime

1 unstable release

0.1.0 May 26, 2023

#405 in Testing

Download history 140/week @ 2023-12-14 25/week @ 2023-12-21 39/week @ 2023-12-28 85/week @ 2024-01-04 152/week @ 2024-01-11 164/week @ 2024-01-18 191/week @ 2024-01-25 229/week @ 2024-02-01 466/week @ 2024-02-08 141/week @ 2024-02-15 243/week @ 2024-02-22 142/week @ 2024-02-29 189/week @ 2024-03-07 279/week @ 2024-03-14 310/week @ 2024-03-21 330/week @ 2024-03-28

1,132 downloads per month
Used in 5 crates

MIT license

8KB
73 lines

tokio-shared-rt

Allow #[tokio::test] to use a shared runtime, so that static variables that connect to a tokio runtime inside are still valid between different test cases.

For example, if you have a global, static database connection pool, it internally holds references to some TCP connections which are bound to the runtime that created it. So if you have two test cases that are both marked #[tokio::test] and access that db pool, good chance is that one of them will fail.

thread 't3' panicked at 'called Result::unwrap() on an Err value: Custom { kind: Other, error: "A Tokio 1.x context was found, but it is being shutdown." }'

This crate provides a macro that uses a shared tokio runtime to run test cases to avoid this problem. Just replace your #[tokio::test] with #[tokio_shared_rt::test(shared)] and vualá! Now test passes.

Usage

#[tokio_shared_rt::test]
async fn t1() {
    db_pool().foo().await;
}
#[tokio_shared_rt::test(shared)]
async fn t2() {
    db_pool().foo().await;
}
#[tokio_shared_rt::test(shared = true)]
async fn t3() {
    db_pool().foo().await;
}
#[tokio_shared_rt::test(shared = false)]
async fn delicate_runtime_test() {
    db_pool().foo().await;
}

Dependencies

~2.4–4MB
~66K SLoC