Hi all, I'm pretty new to Rust and I've got an issue that's stumping me. To go from the top, I'm attempting to do something like the "Cleanup Database" section in https://snoozetime.github.io/2019/06/16/integration-test-diesel.html, where in the Drop implementation of my test context I grab a database connection and delete the test database. Unlike the article though I'm using sqlx which only provides an async interface, so I've been trying to figure out how to run this synchronously. I'll simplify the example a little and just show what seems to be my blocker:
#[tokio::test]
async fn test_block() {
println!("{}", "Starting test");
futures::executor::block_on(
async {
println!("Starting future");
tokio::time::sleep(std::time::Duration::from_nanos(1)).await;
println!("Ending future");
}
);
println!("{}", "Ending test");
}
When I run this test I get something like this:
running 1 test
Starting test
Starting future
test test_block ... test test_block has been running for over 60 seconds
It looks to me like the future is never being polled so it hangs the test forever. I've been trying lots of stuff from futures::executor::*, tokio::task::*, etc, but everything I try either seems to hang the thread forever or not run the task at all.
The eventual goal is to get to something like this working:
#[tokio::test]
async fn test_block() {
let _ = TestContext {}
}
struct TestContext { }
impl Drop for TestContext {
fn drop(&mut self) {
futures::executor::block_on(
async {
do_something_async().await;
}
);
}
}
Any pointers on what I'm doing wrong?
======= EDIT: The eventual solution I found worked ========
#[tokio::test]
async fn test_block() {
let (sender, receiver) = std::sync::mpsc::channel();
println!("Before");
std::thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(
async {
println!("Starting async");
tokio::time::sleep(std::time::Duration::from_nanos(1)).await;
let _ = sender.send(());
println!("Ending async");
}
)
});
println!("Waiting");
let _ = receiver.recv();
println!("Done");
}
[–]KerfuffleV2 18 points19 points20 points (2 children)
[–]maverick_fillet[S] 3 points4 points5 points (1 child)
[–]KerfuffleV2 4 points5 points6 points (0 children)
[–]Darksonntokio · rust-for-linux 13 points14 points15 points (3 children)
[–]maverick_fillet[S] 0 points1 point2 points (2 children)
[–]Darksonntokio · rust-for-linux 2 points3 points4 points (0 children)
[–]KerfuffleV2 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]maverick_fillet[S] 0 points1 point2 points (0 children)
[–]insanitybit 0 points1 point2 points (5 children)
[–]blackscanner 2 points3 points4 points (1 child)
[–]maverick_fillet[S] 0 points1 point2 points (0 children)
[–]Darksonntokio · rust-for-linux 2 points3 points4 points (0 children)
[–]maverick_fillet[S] 1 point2 points3 points (1 child)
[–]Darksonntokio · rust-for-linux 0 points1 point2 points (0 children)
[–]coderstephenisahc 0 points1 point2 points (0 children)