Skip to content

Commit

Permalink
fix: remove tracing initialization from tests
Browse files Browse the repository at this point in the history
Currently, tests use `init_tracing` for prettier logging, but this conflicts with `Infrastructure`, which initializes logging inside itself.
  • Loading branch information
Zoe Spellman committed Mar 7, 2024
1 parent 2a6da11 commit c474ead
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/features/admin/log_level/service/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -32,7 +32,7 @@ pub async fn set_log_level(
pub async fn get_log_level(
extract::Extension(app_context): extract::Extension<AppContext>,
) -> Result<extract::Json<GetLogLevelResponse>, 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(),
Expand Down
11 changes: 9 additions & 2 deletions src/utils/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ 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};

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<Handle<LevelFilter, Registry>> = OnceCell::const_new();

/// Resources important to the server, but are not necessarily in-memory
#[derive(Clone)]
pub struct Infrastructure {
Expand All @@ -24,7 +29,7 @@ pub struct Infrastructure {
/// The JWT instance
pub jwt: Arc<Jwt>,
/// The reload handle for the logger
pub log_reload_handle: Handle<LevelFilter, Registry>,
pub log_reload_handle: &'static Handle<LevelFilter, Registry>,
}

impl Infrastructure {
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion tests/api_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ async fn main() {

AuthenticationWorld::cucumber()
.repeat_skipped()
.init_tracing()
.run_and_exit("tests/features/user/authentication.feature")
.await
}
1 change: 0 additions & 1 deletion tests/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/log_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ async fn main() {

VotingWorld::cucumber()
.repeat_skipped()
.init_tracing()
.run_and_exit("tests/features/user/voting.feature")
.await
}

0 comments on commit c474ead

Please sign in to comment.