use pi_async_runtime_core::{context::record::{self, DriveScope}, CurrentRuntimeAccessError};
use std::cell::Cell;
#[test]
fn test_drive_scope_starts_inactive_without_creating_service() {
assert!(!DriveScope::is_active());
std::thread::spawn(|| { assert!(!DriveScope::is_active()); assert!(!DriveScope::is_active()); }).join().unwrap();
assert!(!DriveScope::is_active());
}
#[test]
fn test_current_borrow_without_selection_does_not_call_action() {
let calls = Cell::new(0);
let result = record::try_with_current::<usize, _>(|_| { calls.set(calls.get() + 1); });
assert!(matches!(result, Err(CurrentRuntimeAccessError::NoCurrentRuntime)));
assert_eq!(calls.get(), 0);
assert!(matches!(record::try_with_current::<String, _>(|_| ()), Err(CurrentRuntimeAccessError::NoCurrentRuntime)));
}
struct ActiveDrop<'a>(&'a Cell<usize>);
impl Drop for ActiveDrop<'_> {
fn drop(&mut self) { assert!(DriveScope::is_active()); self.0.set(self.0.get() + 1); }
}
#[test]
fn test_drive_scope_pairs_entry_exit_and_unwind_without_task_identity() {
assert!(!DriveScope::is_active());
let dropped = Cell::new(0);
{
let scope = DriveScope::enter();
assert!(DriveScope::is_active());
assert!(pi_async_task::task::try_current_task_id().is_none());
let nested = std::panic::catch_unwind(DriveScope::enter);
assert!(nested.is_err());
assert!(DriveScope::is_active());
std::thread::spawn(|| { assert!(!DriveScope::is_active()); }).join().unwrap();
drop(ActiveDrop(&dropped));
drop(scope);
}
assert_eq!(dropped.get(), 1);
assert!(!DriveScope::is_active());
let failure = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _scope = DriveScope::enter();
let _value = ActiveDrop(&dropped);
std::panic::panic_any(123_u32);
}));
let payload = failure.unwrap_err();
assert_eq!(payload.downcast_ref::<u32>(), Some(&123));
assert_eq!(dropped.get(), 2);
assert!(!DriveScope::is_active());
let scope = DriveScope::enter();
assert!(DriveScope::is_active());
drop(scope);
assert!(!DriveScope::is_active());
}
#[test]
fn test_current_owned_access_without_selection_preserves_no_target() {
assert!(matches!(record::try_current::<String>(), Err(CurrentRuntimeAccessError::NoCurrentRuntime)));
assert!(matches!(record::try_current::<usize>(), Err(CurrentRuntimeAccessError::NoCurrentRuntime)));
assert!(matches!(record::try_with_current::<String, _>(|_| ()), Err(CurrentRuntimeAccessError::NoCurrentRuntime)));
}
struct QueryHandle<const TAG: usize> {
id: pi_async_runtime_core::RuntimeId,
label: usize,
clones: std::sync::Arc<std::sync::atomic::AtomicUsize>,
drops: std::sync::Arc<std::sync::atomic::AtomicUsize>,
}
impl<const TAG: usize> Clone for QueryHandle<TAG> {
fn clone(&self) -> Self {
self.clones.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Self { id: self.id, label: self.label, clones: self.clones.clone(), drops: self.drops.clone() }
}
}
impl<const TAG: usize> Drop for QueryHandle<TAG> {
fn drop(&mut self) {
assert!(matches!(record::try_with_current::<usize, _>(|_| ()),
Err(CurrentRuntimeAccessError::NoCurrentRuntime | CurrentRuntimeAccessError::TypeMismatch)));
self.drops.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
}
impl<const TAG: usize> pi_async_runtime_core::RuntimeQuery for QueryHandle<TAG> {
fn id(&self) -> pi_async_runtime_core::RuntimeId { self.id }
fn status(&self) -> pi_async_runtime_core::RuntimeStatus { pi_async_runtime_core::RuntimeStatus::Closed }
fn is_driving(&self) -> bool { panic!("公共选择不查询推进权(selection must not query drive ownership)") }
fn last_idle_state(&self) -> pi_async_runtime_core::IdleState { panic!("不查询空闲策略(idle policy must not be queried)") }
fn close_state(&self) -> pi_async_runtime_core::CloseState { panic!("不推进关闭(close progress must not be queried)") }
fn external_task_count(&self) -> usize { panic!("不查询来源(sources must not be queried)") }
fn internal_task_count(&self) -> usize { panic!("不查询来源(sources must not be queried)") }
fn timer_registration_count(&self) -> usize { panic!("不查询计时登记(timer registrations must not be queried)") }
fn idle_task_count(&self) -> usize { panic!("不查询来源(sources must not be queried)") }
fn in_flight_task_count(&self) -> usize { panic!("不查询任务(tasks must not be queried)") }
fn waiting_for_wake_count(&self) -> usize { panic!("不查询任务(tasks must not be queried)") }
fn pending_cancel_task_count(&self) -> usize { panic!("不查询取消(cancellation must not be queried)") }
}
#[test]
fn test_selected_context_restores_identity_types_and_unwind_without_permissions() {
use pi_async_runtime_core::{RuntimeId, RuntimeQuery, RuntimeStatus};
use std::{future::Future, sync::{Arc, atomic::{AtomicUsize, Ordering}}, task::{Context, Poll, Waker}};
let id = RuntimeId::try_new().unwrap();
let different = RuntimeId::try_new().unwrap();
let clones = Arc::new(AtomicUsize::new(0));
let drops = Arc::new(AtomicUsize::new(0));
let rejected_calls = Cell::new(0);
let outer = QueryHandle::<0> { id, label: 10, clones: clones.clone(), drops: drops.clone() };
let other = QueryHandle::<0> { id: different, label: 99, clones: clones.clone(), drops: drops.clone() };
let future = record::with_selected(outer, || {
assert!(!DriveScope::is_active());
assert!(pi_async_task::task::try_current_task_id().is_none());
record::try_with_current::<QueryHandle<0>, _>(|handle| {
assert_eq!(handle.label, 10);
assert_eq!(handle.status(), RuntimeStatus::Closed);
assert_eq!(record::try_with_current::<QueryHandle<0>, _>(|same| same.id()).unwrap(), id);
}).unwrap();
assert_eq!(clones.load(Ordering::Relaxed), 0);
let owned = record::try_current::<QueryHandle<0>>().unwrap();
assert_eq!(owned.id(), id);
assert_eq!(clones.load(Ordering::Relaxed), 1);
record::try_with_current::<QueryHandle<0>, _>(|handle| {
drop(owned);
assert_eq!(handle.label, 10);
}).unwrap();
assert_eq!(drops.load(Ordering::Relaxed), 1);
assert!(matches!(record::with_selected(other, || rejected_calls.set(1)), Err(CurrentRuntimeAccessError::DifferentRuntime)));
assert_eq!(rejected_calls.get(), 0);
let nested = QueryHandle::<1> { id, label: 20, clones: clones.clone(), drops: drops.clone() };
record::with_selected(nested, || {
assert!(matches!(record::try_current::<QueryHandle<0>>(), Err(CurrentRuntimeAccessError::TypeMismatch)));
assert_eq!(record::try_with_current::<QueryHandle<1>, _>(|handle| handle.label).unwrap(), 20);
}).unwrap();
assert_eq!(record::try_with_current::<QueryHandle<0>, _>(|handle| handle.label).unwrap(), 10);
let failure = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let nested = QueryHandle::<0> { id, label: 30, clones: clones.clone(), drops: drops.clone() };
let _: Result<(), _> = record::with_selected(nested, || {
assert_eq!(record::try_with_current::<QueryHandle<0>, _>(|handle| handle.label).unwrap(), 30);
std::panic::panic_any(456_u32);
});
}));
assert_eq!(failure.unwrap_err().downcast_ref::<u32>(), Some(&456));
assert_eq!(record::try_with_current::<QueryHandle<0>, _>(|handle| handle.label).unwrap(), 10);
assert_eq!(clones.load(Ordering::Relaxed), 1);
assert_eq!(drops.load(Ordering::Relaxed), 4);
async { matches!(record::try_current::<QueryHandle<0>>(), Err(CurrentRuntimeAccessError::NoCurrentRuntime)) }
}).unwrap();
assert_eq!(drops.load(Ordering::Relaxed), 5);
assert!(matches!(record::try_current::<QueryHandle<0>>(), Err(CurrentRuntimeAccessError::NoCurrentRuntime)));
let mut future = std::pin::pin!(future);
assert_eq!(future.as_mut().poll(&mut Context::from_waker(Waker::noop())), Poll::Ready(true));
assert!(!DriveScope::is_active());
}