gpt4 book ai didi

testing - 在集成测试期间如何运行服务器(进程)?

转载 作者:行者123 更新时间:2023-12-03 11:42:59 28 4
gpt4 key购买 nike

我的问题是在上一次集成测试后服务器进程没有关闭。
integration.rs中,我有:

lazy_static! {
static ref SERVER: Arc<Mutex<duct::ReaderHandle>> = {
println!("Starting server");
Arc::new(Mutex::new(
cmd!("cargo", "run", "--", "13000")
.reader()
.expect("Valid server"),
))
};
}

async fn wait_for_server() {
lazy_static::initialize(&SERVER);
// Code to wait
}

#[tokio::test]
async fn integration_test_query_amount() -> Result<()> {
wait_for_server().await;
let client = reqwest::Client::new();
// Etc...
}
测试工作正常,但是 cargo test调用完成后,服务器保持运行。有启动和关闭这样的服务器的好方法吗?

最佳答案

您可以为进程创建Drop包装器,当其超出范围时将杀死该包装器。类似于以下内容:

struct KillOnDrop(std::process::Child);

impl Drop for KillOnDrop {
fn drop(&mut self) {
self.0.kill()
}
}
或者,看起来您已经在使用 tokiotokio::process supports this out of the box

关于testing - 在集成测试期间如何运行服务器(进程)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62843366/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com