use pi_async_executor::state::ExecutorState;
#[test]
fn test_executor_state_constructs_without_runtime_resources() {
let state = ExecutorState::new();
drop(state);
}
#[test]
fn test_executor_state_starts_open() {
let state = ExecutorState::new();
assert_eq!(state.status(), pi_async_runtime_core::RuntimeStatus::Open);
assert_eq!(state.status(), pi_async_runtime_core::RuntimeStatus::Open);
}
#[test]
fn test_executor_state_starts_without_close_request() {
let state = ExecutorState::new();
assert_eq!(state.close_state(), pi_async_runtime_core::CloseState::NotRequested);
assert_eq!(state.close_state(), pi_async_runtime_core::CloseState::NotRequested);
}
#[test]
fn test_executor_state_starts_without_drive_permission() {
let state = ExecutorState::new();
assert!(!state.is_driving());
assert!(!state.is_driving());
}
#[test]
fn test_executor_state_starts_with_unknown_idle_state() {
let state = ExecutorState::new();
assert_eq!(state.last_idle_state(), pi_async_runtime_core::IdleState::Unknown);
assert_eq!(state.last_idle_state(), pi_async_runtime_core::IdleState::Unknown);
}
#[test]
fn test_executor_state_close_request_notifies_without_claiming_completion() {
use pi_async_executor::ThreadParker;
use pi_async_runtime_core::{CloseRequestStatus, CloseState, IdleState, RuntimeStatus};
let state = ExecutorState::new();
let mut parker = ThreadParker::new();
assert!(parker.take_notification());
let notifier = parker.notifier();
assert_eq!(state.request_close(¬ifier), CloseRequestStatus::Requested);
assert_eq!(state.status(), RuntimeStatus::Closing);
assert_eq!(state.close_state(), CloseState::Requested);
assert!(parker.take_notification());
assert_eq!(state.request_close(¬ifier), CloseRequestStatus::AlreadyRequested);
assert!(parker.take_notification());
assert!(!parker.take_notification());
assert_eq!(state.status(), RuntimeStatus::Closing);
assert_eq!(state.close_state(), CloseState::Requested);
assert!(!state.is_driving());
assert_eq!(state.last_idle_state(), IdleState::Unknown);
}
#[test]
fn test_executor_state_concurrent_close_requests_have_one_first_request() {
use pi_async_executor::ThreadParker;
use pi_async_runtime_core::{CloseRequestStatus, CloseState, RuntimeStatus};
let state = ExecutorState::new();
let mut parker = ThreadParker::new();
assert!(parker.take_notification());
let notifier = parker.notifier();
let (first, second) = std::thread::scope(|scope| {
let first = scope.spawn(|| state.request_close(¬ifier));
let second = scope.spawn(|| state.request_close(¬ifier));
(first.join().unwrap(), second.join().unwrap())
});
assert!(matches!((first, second),
(CloseRequestStatus::Requested, CloseRequestStatus::AlreadyRequested)
| (CloseRequestStatus::AlreadyRequested, CloseRequestStatus::Requested)));
assert!(parker.take_notification());
assert!(!parker.take_notification());
assert_eq!(state.status(), RuntimeStatus::Closing);
assert_eq!(state.close_state(), CloseState::Requested);
}
struct QueueFutureProbe { polls: std::sync::Arc<std::sync::atomic::AtomicUsize>, drops: std::sync::Arc<std::sync::atomic::AtomicUsize> }
impl std::future::Future for QueueFutureProbe {
type Output = ();
fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<()> {
self.polls.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
std::task::Poll::Pending
}
}
impl Drop for QueueFutureProbe {
fn drop(&mut self) { self.drops.fetch_add(1, std::sync::atomic::Ordering::Relaxed); }
}
struct NoQueueSchedule;
impl pi_async_task::Schedule for NoQueueSchedule {
fn schedule(&self, _: pi_async_task::Runnable) { panic!("入队切片不得调度(enqueue slice must not schedule)"); }
}
#[test]
fn test_state_local_push_preserves_order_original_values_and_rejection() {
use pi_async_task::{create, create_local, TaskOptions, TaskThread};
use pi_async_task::definitions::{CancelRequestStatus, QueueSupport};
use pi_async_task_queue::{TaskQueue, local::QueuePushErrorKind};
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
let state = ExecutorState::new();
let owner = TaskThread::new();
let mut queue = TaskQueue::new(&owner);
let polls = Arc::new(AtomicUsize::new(0)); let drops = Arc::new(AtomicUsize::new(0));
let first = create_local(&owner, QueueFutureProbe { polls: polls.clone(), drops: drops.clone() }, NoQueueSchedule, (), TaskOptions::intrusive())
.unwrap_or_else(|_| panic!("合法创建应成功(valid creation must succeed)"));
let second = create_local(&owner, QueueFutureProbe { polls: polls.clone(), drops: drops.clone() }, NoQueueSchedule, (), TaskOptions::intrusive())
.unwrap_or_else(|_| panic!("合法创建应成功(valid creation must succeed)"));
let first_id = first.runnable.id(); let second_id = second.runnable.id();
state.push_back(&mut queue, first.runnable).unwrap_or_else(|_| panic!("队尾接收应成功(back push must succeed)"));
state.push_front(&mut queue, second.runnable).unwrap_or_else(|_| panic!("队首接收应成功(front push must succeed)"));
assert_eq!(queue.len(), 2);
assert!(!first.control.snapshot().admitted); assert!(!second.control.snapshot().admitted);
assert_eq!(first.control.snapshot().cancellation, CancelRequestStatus::NotRecorded);
assert_eq!(polls.load(Ordering::Relaxed), 0); assert_eq!(drops.load(Ordering::Relaxed), 0);
owner.close();
assert_eq!(drops.load(Ordering::Relaxed), 2);
let second_value = queue.pop_front().unwrap(); let first_value = queue.pop_front().unwrap();
assert_eq!(second_value.id(), second_id); assert_eq!(first_value.id(), first_id);
drop(second_value); drop(first_value); assert!(queue.is_empty());
assert_eq!(drops.load(Ordering::Relaxed), 2);
for front in [false, true] {
let parts = create(QueueFutureProbe { polls: polls.clone(), drops: drops.clone() }, NoQueueSchedule, (), TaskOptions::plain())
.unwrap_or_else(|_| panic!("合法创建应成功(valid creation must succeed)"));
let id = parts.runnable.id();
let rejected = if front { state.push_front(&mut queue, parts.runnable) } else { state.push_back(&mut queue, parts.runnable) };
let error = rejected.err().unwrap();
assert_eq!(error.cause, QueuePushErrorKind::WrongOwner); assert_eq!(error.task.id(), id);
assert_eq!(error.task.snapshot().cancellation, CancelRequestStatus::NotRecorded);
assert!(!error.task.snapshot().admitted); assert!(queue.is_empty());
drop(error.task);
}
assert_eq!(drops.load(Ordering::Relaxed), 4);
let other_owner = TaskThread::new(); let mut other_queue = TaskQueue::new(&other_owner);
let parts = create_local(&other_owner, QueueFutureProbe { polls: polls.clone(), drops: drops.clone() }, NoQueueSchedule, (), TaskOptions::plain())
.unwrap_or_else(|_| panic!("合法创建应成功(valid creation must succeed)"));
let id = parts.runnable.id();
let error = state.push_back(&mut other_queue, parts.runnable).err().unwrap();
assert_eq!(error.cause, QueuePushErrorKind::LinkMissing); assert_eq!(error.task.id(), id);
assert_eq!(error.task.snapshot().cancellation, CancelRequestStatus::NotRecorded); assert!(other_queue.is_empty());
assert_eq!(drops.load(Ordering::Relaxed), 4);
other_owner.close(); drop(error.task);
assert_eq!(drops.load(Ordering::Relaxed), 5);
other_queue.close().unwrap();
for front in [false, true] {
let mut options = TaskOptions::plain(); options.queue = QueueSupport::IntrusiveLocalAndMpsc;
let parts = create(QueueFutureProbe { polls: polls.clone(), drops: drops.clone() }, NoQueueSchedule, (), options)
.unwrap_or_else(|_| panic!("合法创建应成功(valid creation must succeed)"));
let id = parts.runnable.id();
let rejected = if front { state.push_front(&mut other_queue, parts.runnable) } else { state.push_back(&mut other_queue, parts.runnable) };
let error = rejected.err().unwrap();
assert_eq!(error.cause, QueuePushErrorKind::Closed); assert_eq!(error.task.id(), id);
assert_eq!(error.task.snapshot().cancellation, CancelRequestStatus::NotRecorded); assert!(other_queue.is_empty());
drop(error.task);
}
assert_eq!(drops.load(Ordering::Relaxed), 7); assert_eq!(polls.load(Ordering::Relaxed), 0);
}
#[test]
fn test_state_local_push_rejects_unbound_task_without_consuming_original() {
use pi_async_task::{create, TaskOptions, TaskThread};
use pi_async_task::definitions::CancelRequestStatus;
use pi_async_task_queue::{TaskQueue, local::QueuePushErrorKind};
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
let state = ExecutorState::new(); let owner = TaskThread::new(); let mut queue = TaskQueue::new(&owner);
let polls = Arc::new(AtomicUsize::new(0)); let drops = Arc::new(AtomicUsize::new(0));
let parts = create(QueueFutureProbe { polls: polls.clone(), drops: drops.clone() }, NoQueueSchedule, (), TaskOptions::plain())
.unwrap_or_else(|_| panic!("合法创建应成功(valid creation must succeed)"));
let id = parts.runnable.id();
let error = state.push_back(&mut queue, parts.runnable).err().unwrap();
assert_eq!(error.cause, QueuePushErrorKind::WrongOwner); assert_eq!(error.task.id(), id);
assert_eq!(error.task.snapshot().cancellation, CancelRequestStatus::NotRecorded);
assert!(!error.task.snapshot().admitted); assert!(queue.is_empty());
assert_eq!(polls.load(Ordering::Relaxed), 0); assert_eq!(drops.load(Ordering::Relaxed), 0);
drop(error.task);
assert_eq!(drops.load(Ordering::Relaxed), 1); assert_eq!(polls.load(Ordering::Relaxed), 0);
}