Skip to content

Commit

Permalink
Run server on a regular tokio task in tests (#323)
Browse files Browse the repository at this point in the history
I missed this in my original client implementation. Using
`spawn_blocking` means that tests tend to not terminate if something
goes wrong, but the server _is_ async, so that is just completely
unnecessary. This neatly handles panics in tasks and properly finishes
the test if something goes wrong.
  • Loading branch information
einarmo committed May 1, 2024
1 parent bf0856e commit fa65dbe
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions integration/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,32 +624,32 @@ pub async fn regular_server_test(
let server = Arc::new(RwLock::new(server));
let server2 = server.clone();

// Server runs on its own thread
let t = tokio::task::spawn_blocking(move || {
Server::run_server(server);
info!("Server thread has finished");
});
let server_fut = Server::new_server_task(server);
tokio::pin!(server_fut);

// Listen for quit command, if we get one then finish
loop {
if let Some(command) = rx_server_command.recv().await {
match command {
ServerCommand::Quit => {
// Tell the server to quit
{
info!("1. ------------------------ Server test received quit");
let mut server = server2.write();
server.abort();
select! {
command = rx_server_command.recv() => {
match command {
Some(ServerCommand::Quit) | None => {
// Tell the server to quit
{
info!("1. ------------------------ Server test received quit");
let mut server = server2.write();
server.abort();
}
// wait for server thread to quit
let _ = server_fut.await;
info!("2. ------------------------ Server has now terminated after quit");
break;
}
// wait for server thread to quit
let _ = t.await.unwrap();
info!("2. ------------------------ Server has now terminated after quit");
break;
}
}
} else {
info!("Receiver broke so terminating server test loop");
break;
_ = &mut server_fut => {
warn!("Server finished unexpectedly");
break;
}
}
}
}
Expand Down

0 comments on commit fa65dbe

Please sign in to comment.