Files
khadhroony-solana-project/crates/ksp-logging-lib/tests/runtime.rs
2026-08-14 18:24:28 +02:00

76 lines
3.0 KiB
Rust

// file: crates/ksp-logging-lib/tests/runtime.rs
// version: 1
//! Integration tests for global initialization, takeover filtering and hot reload.
const LOGGING_TARGET: &str = "ksp-logging-lib";
const OTHER_KSP_TARGET: &str = "ksp-store-lib";
const EXTERNAL_TARGET: &str = "sqlx";
fn logging_trace_enabled() -> bool {
return tracing::enabled!(target: LOGGING_TARGET, tracing::Level::TRACE);
}
fn other_ksp_info_enabled() -> bool {
return tracing::enabled!(target: OTHER_KSP_TARGET, tracing::Level::INFO);
}
fn other_ksp_debug_enabled() -> bool {
return tracing::enabled!(target: OTHER_KSP_TARGET, tracing::Level::DEBUG);
}
fn external_error_enabled() -> bool {
return tracing::enabled!(target: EXTERNAL_TARGET, tracing::Level::ERROR);
}
#[test]
fn global_runtime_supports_takeover_hot_reload_and_single_initialization() {
let disabled = ksp_logging_lib::LoggingSettings::new(
ksp_logging_lib::LogFilterLevel::Info,
ksp_logging_lib::SpanEvents::Off,
std::option::Option::None,
std::option::Option::None,
);
let initialize_result = ksp_logging_lib::initialize(&disabled);
assert!(initialize_result.is_ok());
let mut guard = match initialize_result {
std::result::Result::Ok(guard) => guard,
std::result::Result::Err(_) => return,
};
assert!(!logging_trace_enabled());
assert!(!other_ksp_info_enabled());
assert!(!external_error_enabled());
let enabled = ksp_logging_lib::LoggingSettings::new(
ksp_logging_lib::LogFilterLevel::Info,
ksp_logging_lib::SpanEvents::NewAndClose,
std::option::Option::Some(ksp_logging_lib::ConsoleSettings::stderr()),
std::option::Option::None,
)
.with_target_filter(ksp_logging_lib::TargetFilter::new(LOGGING_TARGET, ksp_logging_lib::LogFilterLevel::Trace));
let reload_result = ksp_logging_lib::reinitialize(&mut guard, &enabled);
assert!(reload_result.is_ok());
assert_eq!(guard.settings(), &enabled);
assert!(logging_trace_enabled());
assert!(other_ksp_info_enabled());
assert!(!other_ksp_debug_enabled());
assert!(!external_error_enabled());
let unsupported_file = ksp_logging_lib::LoggingSettings::new(
ksp_logging_lib::LogFilterLevel::Error,
ksp_logging_lib::SpanEvents::Full,
std::option::Option::None,
std::option::Option::Some(ksp_logging_lib::FileSettings::new("logs", "ksp", ksp_logging_lib::FileRotation::Daily)),
);
let failed_reload = ksp_logging_lib::reinitialize(&mut guard, &unsupported_file);
assert!(failed_reload.is_err());
assert_eq!(guard.settings(), &enabled);
assert!(logging_trace_enabled());
assert!(!external_error_enabled());
let second_initialize = ksp_logging_lib::initialize(&disabled);
assert!(second_initialize.is_err());
let error = match second_initialize {
std::result::Result::Ok(_) => return,
std::result::Result::Err(error) => error,
};
assert_eq!(error.code(), ksp_logging_lib::ERROR_CODE_ALREADY_INITIALIZED);
}