use error_forge::{macros::ErrorContext, AppError, ForgeError};
use std::sync::atomic::Ordering;
#[test]
fn test_error_hook_thread_safety() {
use std::sync::atomic::AtomicUsize;
static TEST_SPECIFIC_COUNTER: AtomicUsize = AtomicUsize::new(0);
TEST_SPECIFIC_COUNTER.store(0, Ordering::SeqCst);
let test_hook = |_: ErrorContext| {
TEST_SPECIFIC_COUNTER.fetch_add(1, Ordering::SeqCst);
};
for i in 0..8 {
let msg = format!("Test error {}", i);
let err = AppError::config(msg);
let level = if err.is_fatal() {
error_forge::macros::ErrorLevel::Critical
} else {
error_forge::macros::ErrorLevel::Error
};
test_hook(ErrorContext::new(
err.caption(),
err.kind(),
level,
err.is_fatal(),
err.is_retryable(),
));
}
let count = TEST_SPECIFIC_COUNTER.load(Ordering::SeqCst);
assert_eq!(count, 8, "Expected 8 hook calls but got {}", count);
}
#[test]
fn test_multiple_hook_registrations() {
use std::sync::OnceLock;
static TEST_LOCK: OnceLock<u32> = OnceLock::new();
assert!(TEST_LOCK.set(42).is_ok(), "First set should succeed");
assert!(TEST_LOCK.set(24).is_err(), "Second set should fail");
assert_eq!(
*TEST_LOCK.get().unwrap(),
42,
"Value should be from first set"
);
}