#![cfg(all(feature = "async-std", not(feature = "tokio")))]
use proc_daemon::shutdown::ShutdownCoordinator;
use proc_daemon::subsystem::SubsystemManager;
use std::sync::{Arc, Mutex};
use std::time::Duration;
#[async_std::test]
#[ignore = "Task execution differs significantly between async-std and tokio runtimes"]
async fn test_subsystem_execution_in_async_std() {
let coordinator = ShutdownCoordinator::new(5000, 10000, 5000);
let manager = SubsystemManager::new(coordinator.clone());
let executed = Arc::new(Mutex::new(false));
let executed_clone = executed.clone();
let id = manager.register_fn("test_subsystem", move |mut shutdown| {
let executed = executed_clone.clone();
async move {
*executed.lock().unwrap() = true;
async_std::future::timeout(Duration::from_millis(100), shutdown.cancelled())
.await
.ok();
Ok(())
}
});
let start_result = manager.start_subsystem(id).await;
assert!(
start_result.is_ok(),
"Failed to start subsystem: {start_result:?}"
);
let mut attempts = 0;
while !*executed.lock().unwrap() && attempts < 20 {
async_std::task::sleep(Duration::from_millis(100)).await;
attempts += 1;
let metadata = manager.get_subsystem_metadata(id).unwrap();
println!(
"Attempt {}: Subsystem state: {:?}",
attempts, metadata.state
);
}
assert!(
*executed.lock().unwrap(),
"Subsystem did not execute within the expected time"
);
let _ = coordinator.initiate_shutdown(proc_daemon::shutdown::ShutdownReason::Requested);
async_std::task::sleep(Duration::from_millis(200)).await;
assert!(
coordinator.is_shutdown(),
"Coordinator should be in shutdown state"
);
}