mirror of https://github.com/rwf2/Rocket.git
cleanup and add new test
This commit is contained in:
parent
81293224b8
commit
bd3715cbd6
|
@ -1,38 +1,33 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
use rocket::figment::{providers::{Format as _, Toml}, Figment};
|
use rocket::{Build, Config, Rocket};
|
||||||
use rocket::{custom, fairing::AdHoc, Build, Orbit, Rocket};
|
use rocket::fairing::AdHoc;
|
||||||
|
use rocket::figment::Figment;
|
||||||
|
|
||||||
struct AsyncDropInAsync;
|
struct AsyncDropInAsync;
|
||||||
|
|
||||||
impl Drop for AsyncDropInAsync {
|
impl Drop for AsyncDropInAsync {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Attempt to fetch the current runtime while dropping
|
// Ensure that managed state is dropped inside of an async context by
|
||||||
// Pools in rocket_sync_db_pools (and maybe rocket_db_pools)
|
// ensuring that we do not panic when fetching the current runtime.
|
||||||
// do use this capability. They spawn tasks to asyncronously
|
//
|
||||||
// complete shutdown of the pool, which triggers the same panic.
|
// Crates like rocket_sync_db_pools spawn tasks to asynchronously
|
||||||
|
// complete pool shutdown which must be done in an async context or else
|
||||||
|
// the spawn will panic. We want to ensure that does not happen.
|
||||||
let _ = rocket::tokio::runtime::Handle::current();
|
let _ = rocket::tokio::runtime::Handle::current();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rocket() -> Rocket<Build> {
|
fn rocket() -> Rocket<Build> {
|
||||||
let mut config = rocket::Config::default();
|
let figment = Figment::from(Config::debug_default())
|
||||||
#[cfg(feature = "secrets")]
|
.merge(("address", "tcp:127.0.0.1:0"));
|
||||||
{ config.secret_key = rocket::config::SecretKey::generate().unwrap(); }
|
|
||||||
let figment = Figment::from(config).merge(Toml::string(r#"
|
rocket::custom(figment)
|
||||||
[default]
|
.manage(AsyncDropInAsync)
|
||||||
address = "tcp:127.0.0.1:0"
|
.attach(AdHoc::on_liftoff("Shutdown", |rocket| Box::pin(async {
|
||||||
port = 0
|
rocket.shutdown().notify();
|
||||||
"#).nested());
|
})))
|
||||||
custom(figment).manage(AsyncDropInAsync).attach(AdHoc::on_liftoff(
|
|
||||||
"Shutdown immediately",
|
|
||||||
|rocket: &Rocket<Orbit>| {
|
|
||||||
Box::pin(async {
|
|
||||||
rocket.shutdown().notify();
|
|
||||||
})
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mod launch {
|
mod launch {
|
||||||
|
@ -40,6 +35,7 @@ mod launch {
|
||||||
fn launch() -> _ {
|
fn launch() -> _ {
|
||||||
super::rocket()
|
super::rocket()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_launch() {
|
fn test_launch() {
|
||||||
main();
|
main();
|
||||||
|
@ -49,22 +45,23 @@ mod launch {
|
||||||
mod main {
|
mod main {
|
||||||
#[rocket::main]
|
#[rocket::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
super::rocket()
|
super::rocket().launch().await.unwrap();
|
||||||
.launch()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_main() {
|
fn test_main() {
|
||||||
main();
|
main();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_execute() {
|
fn test_execute() {
|
||||||
rocket::execute(async {
|
rocket::execute(async {
|
||||||
super::rocket()
|
super::rocket().launch().await.unwrap();
|
||||||
.launch()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_execute_directly() {
|
||||||
|
rocket::execute(super::rocket().launch()).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue