1个不稳定版本
0.2.0 | 2022年12月29日 |
---|
#41 in #concurrent
9KB
nonparallelex
基于用户指定的锁定表达式对nonparallel
的分支。
这是一个Rust的过程宏,允许您确保函数(例如单元测试)不会同时运行。这对于集成测试非常有用,其中写入同一数据库表的测试不应该并行运行。
这是通过在注释函数的开始执行提供的表达式(例如互斥锁)并保留返回值(例如锁卫)来实现的,这样在函数返回(或恐慌)时释放锁。
这对于向被其他宏(如tokio::test
)修改的函数中注入锁很有用,而您无法通过其他方式注入锁表达式。
当与tokio:test
和类似的宏一起使用时,请确保将nonparallel调用放在最后。这将使用锁包装整个运行时。如果您将nonparallel放在tokio::test
之上,锁将在运行时内部执行,这意味着运行时将在释放锁后关闭,可能会导致您打算在互斥锁内运行的代码在运行时实际上关闭之前在互斥锁之外运行。
使用方法
use tokio::sync::Mutex;
use nonparallelex::nonparallel;
// Create two locks
static MUT_A: Mutex<()> = Mutex::const_new(());
static MUT_B: Mutex<()> = Mutex::const_new(());
// Mutually exclude parallel runs of functions using those two locks.
#[tokio::test]
#[nonparallel(MUT_A.blocking_lock())]
async fn function_a1() {
// This will not run in parallel to function_a2.
}
#[tokio::test]
#[nonparallel(MUT_A.blocking_lock())]
async fn function_a2() {
// This will not run in parallel to function_a1.
}
#[tokio::test]
#[nonparallel(MUT_B.blocking_lock())]
async fn function_b() {
// This may run in parallel to function_a*.
}
许可证
根据您的选择许可
- Apache许可证第2版 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
。
依赖项
~1.5MB
~35K SLoC