I am writing some unit tests for my Rust http server handlers. But when I am running one of the tests it get stuck at the end of the inner function. Here is relevant part of the code:
async fn generate(request: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let result = process_request(request).await;
println!("This message doesn't get printed!!");
let (spec, override) = match result {
Ok((s, o)) => (s, o),
Err(process_error) => {
return Ok(Response::new(Body::from(format!("{}", process_error))));
},
};
...
Ok(Response::new(Body::from(format!("{}", response))))
}
async fn process_request(request: Request<Body>) -> Result<(Spec, Option<Config>), Error> {
let body = body::to_bytes(request.into_body()).await;
let payload: Payload = serde_json::from_slice(&body.unwrap().to_vec()).unwrap();
let spec_str = payload.spec.to_owned();
...
println!("Function runs to this point and prints this message");
Ok((spec, override))
}
#[tokio::test]
async fn test_gen() {
let payload = Payload {
spec: a_spec(),
};
let payload_json = serde_json::to_string_pretty(&payload).unwrap();
let request = Request::builder().body(Body::from(payload_json));
let result = generate(request.unwrap()).await.unwrap();
// Some asserts ...
}
I am wondering what I am doing wrong?
Looks like the inner function starts another thread, so the solution was to decorate the test with:
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
This resolved the issue for my unit tests.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.