使用带有 actix-web 的工作 POST 路由的测试超时



我已经成功地将Web应用程序从Rocket迁移到actix-web。通过使用邮递员,我已经验证了相同的请求会产生相同的响应。然而,迁移测试已被证明有点棘手。

我的 POST 在运行cargo test时请求超时,即使应用程序在运行cargo run时只是接受并返回预期的响应。

#[cfg(test)]
mod test {
use actix_web::{client, http, test, App};
#[test]
fn main() {
let mut ts = TestSuite::new();
assert!(ts.get_request("/health").status().is_success()); // works
let filter_test_body =
r#"{"key_1":[{"id":"a string"},{"id":"another string"}],"int_1":100}"#;
assert!(
ts.post_request("/post_route", filter_test_body)
.status()
.is_success()
); // times out while actual app returns success with same payload within a few ms
}
fn create_app() -> App<project_name::AppState> {
// {...} some initialization and defining app_state
return App::with_state(&app_state)
.resource("/health", |r| {
r.method(http::Method::GET).f(project_name::health)
})
.resource("/post_route", |r| {
r.method(http::Method::POST)
.with(project_name::post_route_handler)
});
}
struct TestSuite {
server: test::TestServer,
}
impl TestSuite {
pub fn new() -> TestSuite {
TestSuite {
server: test::TestServer::with_factory(create_app),
}
}
pub fn get_request(&mut self, endpoint: &str) -> client::ClientResponse {
let request = self.server
.client(http::Method::GET, endpoint)
.finish()
.unwrap();
self.server.execute(request.send()).unwrap()
}
pub fn post_request(&mut self, endpoint: &str, body: &str) -> client::ClientResponse {
let request_payload: other_lib::request::RequestPayload =
serde_json::from_str(body).unwrap();
let request = self.server
.client(http::Method::POST, endpoint)
.header(http::header::CONTENT_TYPE, "application/json")
.json(request_payload)
//.body(body.to_string())
.unwrap();
self.server.execute(request.send()).unwrap() // thread 'test::main' panicked at 'called `Result::unwrap()` on an `Err` value: Timeout'
}
}
}

相比之下,这是使用Rocket的代码库的POST测试。

#[test]
fn test_filters() {
let client = mount_test_rocket(); // getting access to the test server (rocket::local::Client)
let mut response = client.post("/post_route").header(ContentType::JSON)
.body(r#"{"key_1":[{"id":"a string"},{"id":"another string"}],"int_1":100}"#).dispatch();
assert_eq!(response.status(), Status::Ok);
// + more assertions looking into the actual response body
}

解决方案很简单。事实证明,请求确实在cargo test超时。一个潜在的解决方案是将timeout方法放入链中,或者在我的情况下更简单,通过传入--release标志来打开我在运行应用程序时使用的相同优化。

最新更新