How to do debug prints in async tests?

I have a binary with Axum server, and want to test if there are deadlocks with RwLock. But debug prints aren't visible in the shell.

How to make them be printed? (In all docs I saw, there's no such issue: println from a thread does print.)

Maybe threads are to blame. I use them to guarantee requests come in parallel (no idea how to do this in pure async).

#[tokio::main]
async fn main() {
    ...
    let app = Router::new().route("/query/", get(handle_read)).with_state(...);
    let listener = tokio::net::TcpListener::bind("0.0.0.0:8833").await.unwrap();
	println!("running the server at {addr}");
	axum::serve(listener, app).await.unwrap();
}

async fn handle_read(Path (_): Path<String>, State(ast): State<AppState2>) -> Result<(HeaderMap, String), (StatusCode, String)> {
	//let mut t = Timer::new();
	println!("read: got request");
	...
	println!("read: replying");
	Ok((headers, body))
}
#[cfg(test)]
mod mytests {
    #[tokio::test]
    async fn tttt() {
        let mut threads: Vec<JoinHandle<()>> = vec![];
        // server
        threads.push(spawn(|| {
            println!("server thread: starting");
            main();
        }));

        // read request 1
        threads.push(spawn(|| {
            sleep(Duration::from_secs(1));
            println!("read thread: making request");
            let resp = reqwest::blocking::get("http://localhost:8833/query/").unwrap().text().unwrap();
            println!("{:?}", resp);
        }));
        panic!()
    }
}

Are you looking for cargo test -- --nocapture? Link to docs.

2 Likes

Thanks!

I figured out why it wasn't printing with panic(): the main thread would panic immediately, before other ones could print anything.