1 个不稳定版本
0.1.0 | 2023 年 5 月 26 日 |
---|
#384 在 测试 中
1,510 每月下载量
在 6 crates 中使用
8KB
73 行
tokio-shared-rt
允许 #[tokio::test]
使用一个 共享 运行时,以便连接到 tokio 运行时的静态变量在不同的测试用例之间仍然有效。
例如,如果您有一个全局、静态的数据库连接池,它内部持有一些 TCP 连接的引用,这些连接绑定到创建它的运行时。所以如果您有两个都被标记为 #[tokio::test]
的测试用例,并且访问该 db 池,其中一个很可能失败。
线程 't3' 在
Result::unwrap()
上恐慌:值为Err
:Custom { kind: Other, error: "A Tokio 1.x context was found, but it is being shutdown." }
此 crate 提供了一个宏,使用一个 共享 tokio 运行时 来运行测试用例以避免此问题。只需将您的 #[tokio::test]
替换为 #[tokio_shared_rt::test]
并 vualá! 现在测试通过了。
用法
#[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;
}
依赖关系
~2.3–4MB
~65K SLoC