11#[ macro_use]
22extern crate rocket;
33
4- use rocket:: figment:: { providers:: { Format as _, Toml } , Figment } ;
5- use rocket:: { custom, fairing:: AdHoc , Build , Orbit , Rocket } ;
4+ use rocket:: { Build , Config , Rocket } ;
5+ use rocket:: fairing:: AdHoc ;
6+ use rocket:: figment:: Figment ;
67
78struct AsyncDropInAsync ;
89
910impl Drop for AsyncDropInAsync {
1011 fn drop ( & mut self ) {
11- // Attempt to fetch the current runtime while dropping
12- // Pools in rocket_sync_db_pools (and maybe rocket_db_pools)
13- // do use this capability. They spawn tasks to asyncronously
14- // complete shutdown of the pool, which triggers the same panic.
12+ // Ensure that managed state is dropped inside of an async context by
13+ // ensuring that we do not panic when fetching the current runtime.
14+ //
15+ // Crates like rocket_sync_db_pools spawn tasks to asynchronously
16+ // complete pool shutdown which must be done in an async context or else
17+ // the spawn will panic. We want to ensure that does not happen.
1518 let _ = rocket:: tokio:: runtime:: Handle :: current ( ) ;
1619 }
1720}
1821
1922fn rocket ( ) -> Rocket < Build > {
20- let mut config = rocket:: Config :: default ( ) ;
21- #[ cfg( feature = "secrets" ) ]
22- { config. secret_key = rocket:: config:: SecretKey :: generate ( ) . unwrap ( ) ; }
23- let figment = Figment :: from ( config) . merge ( Toml :: string ( r#"
24- [default]
25- address = "tcp:127.0.0.1:0"
26- port = 0
27- "# ) . nested ( ) ) ;
28- custom ( figment) . manage ( AsyncDropInAsync ) . attach ( AdHoc :: on_liftoff (
29- "Shutdown immediately" ,
30- |rocket : & Rocket < Orbit > | {
31- Box :: pin ( async {
32- rocket. shutdown ( ) . notify ( ) ;
33- } )
34- } ,
35- ) )
23+ let figment = Figment :: from ( Config :: debug_default ( ) )
24+ . merge ( ( "address" , "tcp:127.0.0.1:0" ) ) ;
25+
26+ rocket:: custom ( figment)
27+ . manage ( AsyncDropInAsync )
28+ . attach ( AdHoc :: on_liftoff ( "Shutdown" , |rocket| Box :: pin ( async {
29+ rocket. shutdown ( ) . notify ( ) ;
30+ } ) ) )
3631}
3732
3833mod launch {
3934 #[ launch]
4035 fn launch ( ) -> _ {
4136 super :: rocket ( )
4237 }
38+
4339 #[ test]
4440 fn test_launch ( ) {
4541 main ( ) ;
@@ -49,22 +45,23 @@ mod launch {
4945mod main {
5046 #[ rocket:: main]
5147 async fn main ( ) {
52- super :: rocket ( )
53- . launch ( )
54- . await
55- . unwrap ( ) ;
48+ super :: rocket ( ) . launch ( ) . await . unwrap ( ) ;
5649 }
50+
5751 #[ test]
5852 fn test_main ( ) {
5953 main ( ) ;
6054 }
55+
6156 #[ test]
6257 fn test_execute ( ) {
6358 rocket:: execute ( async {
64- super :: rocket ( )
65- . launch ( )
66- . await
67- . unwrap ( ) ;
59+ super :: rocket ( ) . launch ( ) . await . unwrap ( ) ;
6860 } ) ;
6961 }
62+
63+ #[ test]
64+ fn test_execute_directly ( ) {
65+ rocket:: execute ( super :: rocket ( ) . launch ( ) ) . unwrap ( ) ;
66+ }
7067}
0 commit comments