diff --git a/src/features/admin/log_level/service/rest.rs b/src/features/admin/log_level/service/rest.rs index bb094888..a45237a8 100644 --- a/src/features/admin/log_level/service/rest.rs +++ b/src/features/admin/log_level/service/rest.rs @@ -23,7 +23,7 @@ pub async fn set_log_level( log::Level::Trace => LevelFilter::TRACE, }; - super::super::set_log_level(&app_context.infrastructure().log_reload_handle, level); + super::super::set_log_level(app_context.infrastructure().log_reload_handle, level); Ok(SetLogLevelResponse.into()) } @@ -32,7 +32,7 @@ pub async fn set_log_level( pub async fn get_log_level( extract::Extension(app_context): extract::Extension, ) -> Result, Infallible> { - let level = super::super::get_log_level(&app_context.infrastructure().log_reload_handle); + let level = super::super::get_log_level(app_context.infrastructure().log_reload_handle); Ok(GetLogLevelResponse { level: Level::from_str(level.into_level().unwrap().as_str()).unwrap(), diff --git a/src/utils/infrastructure.rs b/src/utils/infrastructure.rs index 068ff1c5..e21a6a71 100644 --- a/src/utils/infrastructure.rs +++ b/src/utils/infrastructure.rs @@ -7,6 +7,7 @@ use std::{ use snapd::SnapdClient; use sqlx::{pool::PoolConnection, postgres::PgPoolOptions, PgPool, Postgres}; +use tokio::sync::OnceCell; use tracing::level_filters::LevelFilter; use tracing_subscriber::{reload::Handle, Registry}; @@ -14,6 +15,10 @@ use crate::utils::{config::Config, jwt::Jwt}; use super::log_util; +/// The global reload handle, since [`tracing_subscriber`] is we have to be too because it panics +/// if you call init twice, which makes it so tests can't initialize [`Infrastructure`] more than once. +static RELOAD_HANDLE: tokio::sync::OnceCell> = OnceCell::const_new(); + /// Resources important to the server, but are not necessarily in-memory #[derive(Clone)] pub struct Infrastructure { @@ -24,7 +29,7 @@ pub struct Infrastructure { /// The JWT instance pub jwt: Arc, /// The reload handle for the logger - pub log_reload_handle: Handle, + pub log_reload_handle: &'static Handle, } impl Infrastructure { @@ -39,7 +44,9 @@ impl Infrastructure { let jwt = Jwt::new(&config.jwt_secret)?; let jwt = Arc::new(jwt); - let reload_handle = log_util::init_logging(&config.log_level)?; + let reload_handle = RELOAD_HANDLE + .get_or_try_init(|| async move { log_util::init_logging(&config.log_level) }) + .await?; Ok(Infrastructure { postgres, diff --git a/tests/api_info.rs b/tests/api_info.rs index 2755360d..d55cb5b7 100644 --- a/tests/api_info.rs +++ b/tests/api_info.rs @@ -67,7 +67,6 @@ async fn main() { LogWorld::cucumber() .repeat_skipped() - .init_tracing() .max_concurrent_scenarios(1) .run_and_exit("tests/features/admin/api-info.feature") .await diff --git a/tests/authentication.rs b/tests/authentication.rs index 8310ee79..236f5f08 100644 --- a/tests/authentication.rs +++ b/tests/authentication.rs @@ -98,7 +98,6 @@ async fn main() { AuthenticationWorld::cucumber() .repeat_skipped() - .init_tracing() .run_and_exit("tests/features/user/authentication.feature") .await } diff --git a/tests/chart.rs b/tests/chart.rs index df444395..3496cc02 100644 --- a/tests/chart.rs +++ b/tests/chart.rs @@ -245,7 +245,6 @@ async fn main() { ChartWorld::cucumber() .before(|_, _, _, _| clear_db().boxed_local()) .repeat_failed() - .init_tracing() .max_concurrent_scenarios(1) .run_and_exit("tests/features/chart.feature") .await diff --git a/tests/log_level.rs b/tests/log_level.rs index 38f7d907..0b8b9457 100644 --- a/tests/log_level.rs +++ b/tests/log_level.rs @@ -103,7 +103,6 @@ async fn main() { LogWorld::cucumber() .repeat_skipped() - .init_tracing() .max_concurrent_scenarios(1) .run_and_exit("tests/features/admin/log-level.feature") .await diff --git a/tests/voting.rs b/tests/voting.rs index 725cd2e0..a9c58d66 100644 --- a/tests/voting.rs +++ b/tests/voting.rs @@ -257,7 +257,6 @@ async fn main() { VotingWorld::cucumber() .repeat_skipped() - .init_tracing() .run_and_exit("tests/features/user/voting.feature") .await }