use super::TimerEntry;
use crate::time::registration::{RegistrationEnd, TimerRegistration, TimerRegistrationKind};
use std::sync::Arc;
use std::task::{Context, Poll, Waker};
fn prepared() -> Arc<TimerRegistration<()>> {
let registration = Arc::new(TimerRegistration::<()>::new(TimerRegistrationKind::Wait));
assert!(registration.begin());
assert!(!registration.publish_key(()));
registration
}
fn poll_now(registration: &TimerRegistration<()>) -> Poll<RegistrationEnd> {
registration.poll(&mut Context::from_waker(Waker::noop()))
}
#[test]
fn test_signal_entry_delivery_finishes_wait_as_elapsed() {
let registration = prepared();
let entry = TimerEntry::signal(registration.clone());
assert!(entry.into_due().is_none(), "信号不交出业务原值");
assert!(matches!(poll_now(®istration), Poll::Ready(RegistrationEnd::Elapsed)));
registration.finish(RegistrationEnd::Elapsed);
assert!(matches!(poll_now(®istration), Poll::Ready(RegistrationEnd::Elapsed)), "重复结束不改原结果");
}
#[test]
fn test_undelivered_entry_reports_removed_once() {
let registration = prepared();
drop(TimerEntry::signal(registration.clone()));
assert!(matches!(poll_now(®istration), Poll::Ready(RegistrationEnd::Removed)));
registration.finish(RegistrationEnd::Removed);
assert!(matches!(poll_now(®istration), Poll::Ready(RegistrationEnd::Removed)));
}
#[test]
fn test_undelivered_entry_with_recorded_cancel_reports_cancelled() {
let registration = prepared();
assert!(registration.request_wait_cancel());
drop(TimerEntry::signal(registration.clone()));
assert!(matches!(poll_now(®istration), Poll::Ready(RegistrationEnd::Cancelled)));
}
#[test]
fn test_recovered_entry_keeps_registration_unsettled() {
let registration = prepared();
assert!(TimerEntry::signal(registration.clone()).recover().is_none(), "信号不恢复业务原值");
assert!(poll_now(®istration).is_pending(), "恢复不结束登记");
assert!(registration.claim_forwarding(false).is_none(), "未请求取消时不存在转交资格");
}