Skip to content

Commit 2722be7

Browse files
committed
cleanup and add new test
1 parent 6ce8280 commit 2722be7

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed
+29-30
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
#[macro_use]
22
extern 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

78
struct AsyncDropInAsync;
89

910
impl 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

1922
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+
})))
3631
}
3732

3833
mod launch {
3934
#[launch]
4035
fn launch() -> _ {
4136
super::rocket()
4237
}
38+
4339
#[test]
4440
fn test_launch() {
4541
main();
@@ -49,22 +45,25 @@ mod launch {
4945
mod 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();
60+
});
61+
}
62+
63+
#[test]
64+
fn test_execute_directly() {
65+
rocket::execute(async {
66+
rocket::execute(super::rocket().launch()).unwrap();
6867
});
6968
}
7069
}

0 commit comments

Comments
 (0)