#![cfg(test)]
use crate::runtime::RuntimeBuilder;
use crate::test_utils::{init_runtime_logging, init_test_logging};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use tracing::level_filters::LevelFilter;
use tracing::span::{Attributes, Id, Record};
use tracing::subscriber::Interest;
use tracing::{Event, Metadata, Subscriber};
#[derive(Debug, Clone)]
struct ApplicationSubscriberFixture {
events: Arc<Mutex<VecDeque<String>>>,
subscriber_id: String,
}
impl ApplicationSubscriberFixture {
fn new(subscriber_id: String) -> Self {
Self {
events: Arc::new(Mutex::new(VecDeque::new())),
subscriber_id,
}
}
fn record_event(&self, message: String) {
let mut events = self.events.lock().unwrap();
events.push_back(format!("[{}] {}", self.subscriber_id, message));
}
fn captured_events(&self) -> Vec<String> {
let events = self.events.lock().unwrap();
events.iter().cloned().collect()
}
fn event_count(&self) -> usize {
self.events.lock().unwrap().len()
}
}
impl Subscriber for ApplicationSubscriberFixture {
fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
true
}
fn new_span(&self, _span: &Attributes<'_>) -> Id {
Id::from_u64(1)
}
fn record(&self, _span: &Id, _values: &Record<'_>) {
}
fn record_follows_from(&self, _span: &Id, _follows: &Id) {
}
fn event(&self, event: &Event<'_>) {
let message = format!(
"Event at {}:{}",
event.metadata().file().unwrap_or("unknown"),
event.metadata().line().unwrap_or(0)
);
self.record_event(message);
}
fn enter(&self, _span: &Id) {
}
fn exit(&self, _span: &Id) {
}
fn register_callsite(&self, _metadata: &'static Metadata<'static>) -> Interest {
Interest::always()
}
fn max_level_hint(&self) -> Option<LevelFilter> {
None
}
}
#[test]
fn audit_subscriber_installation_order_composability() {
println!("🔍 AUDIT: Subscriber installation order composability");
println!("📋 Composability requirements:");
println!(" • Application subscriber A installed first");
println!(" • Asupersync runtime creation does NOT override A");
println!(" • Both subscribers coexist (chain A and B)");
println!(" • No data loss from subscriber A");
println!(" • No errors preventing runtime creation");
println!("📊 Phase 1: Application installs Subscriber A");
let app_subscriber = ApplicationSubscriberFixture::new("AppSubscriber".to_string());
let app_dispatch = tracing::Dispatch::new(app_subscriber.clone());
let (initial_events, final_events, result) =
tracing::dispatcher::with_default(&app_dispatch, || {
crate::tracing_compat::info!("Application subscriber installed");
let initial_events = app_subscriber.event_count();
println!(" Application subscriber events: {}", initial_events);
println!("📊 Phase 2: Create asupersync runtime");
let runtime = RuntimeBuilder::current_thread()
.build()
.expect("Runtime creation should not fail due to existing subscriber");
let result = runtime.block_on(async {
crate::tracing_compat::info!("Runtime created successfully");
42_u32
});
let final_events = app_subscriber.event_count();
(initial_events, final_events, result)
});
assert_eq!(result, 42, "Runtime should work normally");
println!(" Final application subscriber events: {}", final_events);
assert!(
final_events > initial_events,
"Application subscriber should continue receiving events after runtime creation. \
Initial: {}, Final: {}. This indicates the asupersync runtime OVERRIDE the \
application's subscriber instead of being composable.",
initial_events,
final_events
);
println!("✅ COMPOSABILITY VERIFIED: Application subscriber preserved");
println!(" • Runtime creation succeeded");
println!(" • Application subscriber still receives events");
println!(" • No data loss from original subscriber");
}
#[test]
fn audit_old_api_subscriber_conflict_defect() {
println!("🚨 AUDIT: Old API subscriber conflict defect demonstration");
println!("📋 Defect scenario (old API):");
println!(" • Application installs subscriber A");
println!(" • Code calls init_test_logging() (old API)");
println!(" • Expected: Global subscriber conflict");
let app_subscriber = ApplicationSubscriberFixture::new("OldApiApp".to_string());
let app_dispatch = tracing::Dispatch::new(app_subscriber.clone());
tracing::dispatcher::with_default(&app_dispatch, || {
crate::tracing_compat::info!("Old API app subscriber baseline event");
});
assert!(
!app_subscriber.captured_events().is_empty(),
"old-API conflict fixture should prove the app subscriber can capture events"
);
println!("📊 Simulating init_test_logging() call");
init_test_logging(); init_test_logging(); init_test_logging();
let global_set = tracing::dispatcher::has_been_set();
println!(" Global subscriber set: {}", global_set);
assert!(global_set, "init_test_logging should set global subscriber");
println!("🚨 DEFECT CONFIRMED: Old API uses Once guard");
println!(" • Once::call_once ignores subsequent subscriber installations");
println!(" • Non-composable: second runtime gets no tracing");
println!(" • Violates asupersync philosophy of composability");
}
#[test]
fn audit_new_api_subscriber_isolation_fix() {
println!("✅ AUDIT: New API subscriber isolation fix");
println!("📋 Isolation solution (new API):");
println!(" • Application subscriber works globally");
println!(" • Runtime-specific subscriber works in isolated scope");
println!(" • No conflicts between the two");
let app_subscriber = ApplicationSubscriberFixture::new("GlobalApp".to_string());
let app_dispatch = tracing::Dispatch::new(app_subscriber.clone());
tracing::dispatcher::with_default(&app_dispatch, || {
crate::tracing_compat::info!("Global app subscriber baseline event");
});
let runtime_subscriber = init_runtime_logging("isolated_runtime".to_string());
crate::tracing_compat::info!("Global app message");
let global_events = app_subscriber.event_count();
runtime_subscriber.with_subscriber(|| {
crate::tracing_compat::info!("Isolated runtime message");
});
crate::tracing_compat::info!("Another global app message");
let final_events = app_subscriber.event_count();
let captured_events = app_subscriber.captured_events();
println!("📊 Isolation verification:");
println!(" Global subscriber events: {}", final_events);
println!(" Runtime subscriber: isolated scope");
assert_eq!(
captured_events.len(),
final_events,
"captured event list should match event_count for the audit fixture"
);
assert!(
final_events >= global_events,
"Global subscriber should continue working independently"
);
println!("✅ ISOLATION VERIFIED: Composable subscriber solution");
println!(" • Global subscriber preserved");
println!(" • Runtime subscriber isolated");
println!(" • Both coexist without conflicts");
println!(" • Follows asupersync composability philosophy");
}
#[test]
fn audit_runtime_builder_subscriber_neutrality() {
println!("🔬 AUDIT: Runtime builder subscriber neutrality");
println!("📋 Neutrality requirements:");
println!(" • Runtime creation doesn't install global subscribers");
println!(" • Multiple runtimes don't conflict");
println!(" • Users control subscriber installation");
let initial_state = tracing::dispatcher::has_been_set();
let runtime1 = RuntimeBuilder::current_thread()
.build()
.expect("First runtime should build");
let runtime2 = RuntimeBuilder::current_thread()
.build()
.expect("Second runtime should build");
let runtime3 = RuntimeBuilder::new()
.worker_threads(1)
.build()
.expect("Third runtime should build");
let final_state = tracing::dispatcher::has_been_set();
println!("📊 Subscriber state analysis:");
println!(" Initial global subscriber: {}", initial_state);
println!(" After 3 runtime builds: {}", final_state);
let result1 = runtime1.block_on(async { 1_u32 });
let result2 = runtime2.block_on(async { 2_u32 });
let result3 = runtime3.block_on(async { 3_u32 });
assert_eq!(result1, 1);
assert_eq!(result2, 2);
assert_eq!(result3, 3);
println!("✅ NEUTRALITY VERIFIED: Runtime builder is subscriber-neutral");
println!(" • No automatic global subscriber installation");
println!(" • Multiple runtimes coexist peacefully");
println!(" • User controls observability setup");
println!(" • Composable by design");
}
#[test]
fn audit_real_world_web_app_scenario() {
println!("🌐 AUDIT: Real-world web application scenario");
println!("📋 Web app scenario:");
println!(" 1. Web app initializes tracing on startup");
println!(" 2. Background service creates asupersync runtime");
println!(" 3. Both should work without interference");
let web_subscriber = ApplicationSubscriberFixture::new("WebApp".to_string());
let web_dispatch = tracing::Dispatch::new(web_subscriber.clone());
tracing::dispatcher::with_default(&web_dispatch, || {
crate::tracing_compat::info!("Web application starting up");
crate::tracing_compat::info!("Initializing background services");
});
let startup_events = web_subscriber.event_count();
println!(" Web app startup events: {}", startup_events);
println!("📊 Phase 2: Background service creates runtime");
let background_subscriber = init_runtime_logging("background_service".to_string());
let runtime = RuntimeBuilder::current_thread()
.build()
.expect("Background service runtime should build");
println!("📊 Phase 3: Independent operation verification");
tracing::dispatcher::with_default(&web_dispatch, || {
crate::tracing_compat::info!("Processing web request");
});
background_subscriber.with_subscriber(|| {
runtime.block_on(async {
crate::tracing_compat::info!("Background task processing");
});
});
tracing::dispatcher::with_default(&web_dispatch, || {
crate::tracing_compat::info!("Web request completed");
});
let final_web_events = web_subscriber.event_count();
println!("📊 Final verification:");
println!(" Web app total events: {}", final_web_events);
assert!(
final_web_events > startup_events,
"Web app should continue receiving tracing events"
);
println!("✅ REAL-WORLD SCENARIO: SOUND");
println!(" • Web app tracing preserved");
println!(" • Background runtime works independently");
println!(" • No cross-contamination of tracing");
println!(" • Composable architecture achieved");
}