asserhttp
asserhttp copied to clipboard
Assertion with `reqwest` hangs in `tokio::test` environment
Hello, this is the minimal example I could make:
fn main() {
}
#[cfg(test)]
mod tests {
#[tokio::test]
async fn test() {
use asserhttp::*;
let mut response = reqwest::get("https://github.com").await.unwrap();
response.expect_body_text(|t| assert!(!t.is_empty()));
}
}
and this in my Cargo.toml
:
[dependencies]
tokio = { version = "1.5", features = ["macros", "rt"] }
asserhttp = { version = "0.5", features = ["reqwest"] }
reqwest = "0.11"
When running cargo test
, the test just hangs indefinitely. If the same code is ran in main
(with tokio::main
) it works fine.
The issue is with this line, the call to futures_lite::future::block_on
. The issue seems to be that tokio::test
uses a single-threaded executor, whereas tokio::main
is usually multi-threaded.
Hi, unfortunately I haven't been able to reproduce. My setup (where I did not use asserhttp):
reqwest = { version = "0.11", features = ["json", "blocking"], default-features = false }
tokio = { version = "1.15", features = ["macros", "rt"] }
#[tokio::test]
async fn test() {
reqwest::get("https://github.com").await.unwrap();
}
// Fails with:
// called `Result::unwrap()` on an `Err` value: reqwest::Error { kind: Request, url: Url { scheme: "https",
// cannot_be_a_base: false, username: "", password: None, host: Some(Domain("github.com")),
// port: None, path: "/", query: None, fragment: None }, source: hyper::Error(Connect, "invalid URL,
// scheme is not http") }
Anyway, I don't know the probable cause but I think here that I should timeout instead of letting this thread block forever