1
1
#[ macro_use]
2
2
extern crate rocket;
3
3
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 ;
6
7
7
8
struct AsyncDropInAsync ;
8
9
9
10
impl Drop for AsyncDropInAsync {
10
11
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.
15
18
let _ = rocket:: tokio:: runtime:: Handle :: current ( ) ;
16
19
}
17
20
}
18
21
19
22
fn 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
+ } ) ) )
36
31
}
37
32
38
33
mod launch {
39
34
#[ launch]
40
35
fn launch ( ) -> _ {
41
36
super :: rocket ( )
42
37
}
38
+
43
39
#[ test]
44
40
fn test_launch ( ) {
45
41
main ( ) ;
@@ -49,22 +45,23 @@ mod launch {
49
45
mod main {
50
46
#[ rocket:: main]
51
47
async fn main ( ) {
52
- super :: rocket ( )
53
- . launch ( )
54
- . await
55
- . unwrap ( ) ;
48
+ super :: rocket ( ) . launch ( ) . await . unwrap ( ) ;
56
49
}
50
+
57
51
#[ test]
58
52
fn test_main ( ) {
59
53
main ( ) ;
60
54
}
55
+
61
56
#[ test]
62
57
fn test_execute ( ) {
63
58
rocket:: execute ( async {
64
- super :: rocket ( )
65
- . launch ( )
66
- . await
67
- . unwrap ( ) ;
59
+ super :: rocket ( ) . launch ( ) . await . unwrap ( ) ;
68
60
} ) ;
69
61
}
62
+
63
+ #[ test]
64
+ fn test_execute_directly ( ) {
65
+ rocket:: execute ( super :: rocket ( ) . launch ( ) ) . unwrap ( ) ;
66
+ }
70
67
}
0 commit comments