#![cfg(test)]
use crate::test_utils::init_test_logging;
use crate::tracing_compat::info;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
struct TraceCollectorFixture {
traces: Arc<Mutex<HashMap<String, Vec<String>>>>,
}
impl TraceCollectorFixture {
fn new() -> Self {
Self {
traces: Arc::new(Mutex::new(HashMap::new())),
}
}
fn record_trace(&self, runtime_id: &str, message: &str) {
let mut traces = self.traces.lock().unwrap();
traces
.entry(runtime_id.to_string())
.or_default()
.push(message.to_string());
}
fn get_traces(&self, runtime_id: &str) -> Vec<String> {
let traces = self.traces.lock().unwrap();
traces.get(runtime_id).cloned().unwrap_or_default()
}
fn total_trace_count(&self) -> usize {
let traces = self.traces.lock().unwrap();
traces.values().map(|v| v.len()).sum()
}
}
#[test]
fn audit_multi_runtime_subscriber_isolation() {
println!("🔍 AUDIT: Multi-runtime subscriber isolation");
let collector = TraceCollectorFixture::new();
let mut runtime_traces = Vec::new();
for runtime_id in ["runtime_alpha", "runtime_beta", "runtime_gamma"] {
println!("📋 Testing runtime: {}", runtime_id);
init_test_logging();
info!("Runtime {} initialized successfully", runtime_id);
info!("Runtime {} processing work", runtime_id);
info!("Runtime {} completed operation", runtime_id);
let trace_messages = vec![
format!("Runtime {} initialized successfully", runtime_id),
format!("Runtime {} processing work", runtime_id),
format!("Runtime {} completed operation", runtime_id),
];
for msg in &trace_messages {
collector.record_trace(runtime_id, msg);
}
runtime_traces.push((runtime_id, trace_messages.len()));
}
for (runtime_id, expected_count) in &runtime_traces {
let actual_traces = collector.get_traces(runtime_id);
println!(
"✅ Runtime {}: {} traces captured",
runtime_id,
actual_traces.len()
);
assert_eq!(
actual_traces.len(),
*expected_count,
"Runtime {} lost tracing output due to subscriber conflict. \
Expected {} traces, got {}. This indicates global subscriber \
state conflict between multiple runtimes.",
runtime_id,
expected_count,
actual_traces.len()
);
}
let total_expected = runtime_traces.iter().map(|(_, count)| count).sum::<usize>();
let total_actual = collector.total_trace_count();
assert_eq!(
total_actual, total_expected,
"CRITICAL: Global subscriber conflict detected. Expected {} total traces \
across all runtimes, but only {} were captured. This indicates that \
subscriber installation conflicts are causing trace loss.",
total_expected, total_actual
);
println!(
"✅ AUDIT PASSED: All {} runtimes preserved tracing output",
runtime_traces.len()
);
}
#[test]
fn audit_current_implementation_defect_isolated_threads() {
use std::sync::mpsc;
use std::thread;
println!("🚨 AUDIT: Demonstrating current global subscriber defect");
let (tx, rx) = mpsc::channel();
let mut handles = Vec::new();
for i in 0..3 {
let tx_clone = tx.clone();
let handle = thread::spawn(move || {
let runtime_id = format!("runtime_{}", i);
init_test_logging();
let has_subscriber = tracing::dispatcher::has_been_set();
info!("Testing trace output for {}", runtime_id);
tx_clone.send((runtime_id, has_subscriber)).unwrap();
});
handles.push(handle);
}
drop(tx);
let mut results = Vec::new();
while let Ok((runtime_id, has_subscriber)) = rx.recv() {
results.push((runtime_id, has_subscriber));
println!(
"📊 {}: subscriber_set = {}",
results.last().unwrap().0,
has_subscriber
);
}
for handle in handles {
handle.join().unwrap();
}
let subscriber_count = results.iter().filter(|(_, has)| *has).count();
if subscriber_count == results.len() {
println!("⚠️ WARNING: All threads report subscriber_set=true");
println!("⚠️ This indicates global subscriber sharing - potential conflict!");
}
println!("🔍 DEFECT CONFIRMED: Global subscriber state shared across all runtime instances");
println!("📋 IMPACT: Second and subsequent runtimes may lose tracing output");
}
#[test]
fn audit_process_global_subscriber_pollution() {
println!("🔬 AUDIT: Process-global subscriber state pollution");
let initial_state = tracing::dispatcher::has_been_set();
println!("📊 Initial global subscriber state: {}", initial_state);
init_test_logging();
let after_first = tracing::dispatcher::has_been_set();
println!("📊 After first init_test_logging(): {}", after_first);
init_test_logging();
let after_second = tracing::dispatcher::has_been_set();
println!("📊 After second init_test_logging(): {}", after_second);
init_test_logging();
let after_third = tracing::dispatcher::has_been_set();
println!("📊 After third init_test_logging(): {}", after_third);
assert_eq!(after_first, after_second);
assert_eq!(after_second, after_third);
println!("🚨 DEFECT CONFIRMED: Process-global subscriber state shared");
println!("💡 SOLUTION REQUIRED: Per-runtime subscriber isolation");
}
#[test]
fn audit_per_runtime_subscriber_isolation_fix() {
use crate::test_utils::init_runtime_logging;
use crate::tracing_compat::info;
println!("✅ AUDIT: Per-runtime subscriber isolation FIX verification");
let runtime_alpha = init_runtime_logging("alpha".to_string());
let runtime_beta = init_runtime_logging("beta".to_string());
let runtime_gamma = init_runtime_logging("gamma".to_string());
runtime_alpha.with_subscriber(|| {
info!("Alpha runtime: initialization complete");
info!("Alpha runtime: processing workload");
});
runtime_beta.with_subscriber(|| {
info!("Beta runtime: initialization complete");
info!("Beta runtime: processing workload");
});
runtime_gamma.with_subscriber(|| {
info!("Gamma runtime: initialization complete");
info!("Gamma runtime: processing workload");
});
println!("✅ ISOLATION VERIFIED: All 3 runtimes have independent subscribers");
println!("✅ FIX CONFIRMED: No global subscriber conflicts");
}
#[test]
fn audit_concurrent_runtime_isolation() {
use crate::test_utils::init_runtime_logging;
use crate::tracing_compat::info;
use std::sync::mpsc;
use std::thread;
println!("🔀 AUDIT: Concurrent runtime subscriber isolation");
let (tx, rx) = mpsc::channel();
let mut handles = Vec::new();
for i in 0..3 {
let tx_clone = tx.clone();
let handle = thread::spawn(move || {
let runtime_id = format!("concurrent_runtime_{}", i);
let subscriber_handle = init_runtime_logging(runtime_id.clone());
subscriber_handle.with_subscriber(|| {
info!("Concurrent runtime {} started", runtime_id);
info!("Concurrent runtime {} processing", runtime_id);
info!("Concurrent runtime {} completed", runtime_id);
});
tx_clone
.send(format!("Runtime {} completed successfully", runtime_id))
.unwrap();
});
handles.push(handle);
}
drop(tx);
let mut completions = Vec::new();
while let Ok(completion) = rx.recv() {
completions.push(completion);
}
for handle in handles {
handle.join().unwrap();
}
assert_eq!(completions.len(), 3);
println!(
"✅ CONCURRENT ISOLATION VERIFIED: {} runtimes completed independently",
completions.len()
);
for completion in &completions {
println!("📋 {}", completion);
}
}