From c905bcbdd2f23ad86d51c61e77954c6cdf0d736c Mon Sep 17 00:00:00 2001 From: Matthew Pomes Date: Fri, 9 Aug 2024 23:01:18 -0500 Subject: [PATCH] Add test to validate new behavior --- core/lib/tests/drop-in-async-context.rs | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 core/lib/tests/drop-in-async-context.rs diff --git a/core/lib/tests/drop-in-async-context.rs b/core/lib/tests/drop-in-async-context.rs new file mode 100644 index 0000000000..78965fa99b --- /dev/null +++ b/core/lib/tests/drop-in-async-context.rs @@ -0,0 +1,51 @@ +#[macro_use] extern crate rocket; + +use rocket::{Rocket, Build, build, fairing::AdHoc, Orbit}; + +struct AsyncDropInAsync; + +impl Drop for AsyncDropInAsync { + fn drop(&mut self) { + // Attempt to fetch the current runtime while dropping + // Pools in rocket_sync_db_pools (and maybe rocket_db_pools) + // do use this capability. They spawn tasks to asyncronously + // complete shutdown of the pool, which triggers the same panic. + let _ = rocket::tokio::runtime::Handle::current(); + } +} + +fn rocket() -> Rocket { + build().manage(AsyncDropInAsync).attach(AdHoc::on_liftoff( + "Shutdown immediately", + |rocket: &Rocket| Box::pin(async { + rocket.shutdown().notify(); + } + ))) +} + +mod launch { + #[launch] + fn launch() -> _ { + super::rocket() + } + #[test] + fn test_launch() { + main(); + } +} + +mod main { + #[rocket::main] + async fn main() { + super::rocket().launch().await.unwrap(); + } + #[test] + fn test_main() { + main(); + } + #[test] + fn test_execute() { + rocket::execute(super::rocket().launch()).unwrap(); + } +} +