#[test]
fn with_session_sets_and_clears_thread_local() {
assert!(aft::log_ctx::current_session().is_none());
aft::log_ctx::with_session(Some("test_session_1".to_string()), || {
assert_eq!(
aft::log_ctx::current_session(),
Some("test_session_1".to_string())
);
});
assert!(aft::log_ctx::current_session().is_none());
aft::log_ctx::with_session(Some("outer".to_string()), || {
assert_eq!(aft::log_ctx::current_session(), Some("outer".to_string()));
aft::log_ctx::with_session(Some("inner".to_string()), || {
assert_eq!(aft::log_ctx::current_session(), Some("inner".to_string()));
});
assert_eq!(aft::log_ctx::current_session(), Some("outer".to_string()));
});
assert!(aft::log_ctx::current_session().is_none());
}
#[test]
fn with_session_restores_after_panic() {
use std::panic::{catch_unwind, AssertUnwindSafe};
assert!(aft::log_ctx::current_session().is_none());
let result = catch_unwind(AssertUnwindSafe(|| {
aft::log_ctx::with_session(Some("panic_session".to_string()), || {
assert_eq!(
aft::log_ctx::current_session(),
Some("panic_session".to_string())
);
panic!("intentional panic for with_session guard test");
});
}));
assert!(result.is_err());
assert!(aft::log_ctx::current_session().is_none());
}
#[test]
fn set_session_direct_manipulation() {
assert!(aft::log_ctx::current_session().is_none());
aft::log_ctx::set_session(Some("direct_session".to_string()));
assert_eq!(
aft::log_ctx::current_session(),
Some("direct_session".to_string())
);
aft::log_ctx::set_session(None);
assert!(aft::log_ctx::current_session().is_none());
}
#[test]
fn slog_macros_prepend_session_tag() {
aft::log_ctx::with_session(Some("abcd1234".to_string()), || {
let prefix = aft::log_ctx::session_prefix();
assert_eq!(prefix, "[ses_abcd1234] ");
let body = format!(
"{}semantic index: rebuilding from scratch (450 files)",
prefix
);
assert_eq!(
body,
"[ses_abcd1234] semantic index: rebuilding from scratch (450 files)"
);
});
aft::log_ctx::with_session(None, || {
let prefix = aft::log_ctx::session_prefix();
assert_eq!(prefix, "[ses_abcd1234] ");
});
}