#![cfg(feature = "async")]
use std::time::Duration;
use actor_lite::{ActorContext, ActorCore, AsyncActor, CancelToken};
struct Counter {
count: u32,
}
enum Cmd {
Inc,
}
impl ActorCore for Counter {
type Cmd = Cmd;
type Event = u32;
type State = u32;
type Progress = ();
fn init_state_and_progress(&self) -> (Self::State, Self::Progress) {
(self.count, ())
}
}
impl AsyncActor for Counter {
async fn run(self, mut ctx: ActorContext<Self>) {
let cancel = ctx.cancel_clone();
let mut count = self.count;
loop {
tokio::select! {
_ = cancel.cancelled() => break,
cmd = ctx.wait_for_cmd() => match cmd {
Some(Cmd::Inc) => {
count += 1;
ctx.update_state(count);
ctx.send_event(count);
}
None => break, },
}
}
}
}
#[tokio::test]
async fn state_listener_sees_update() {
let handle = Counter { count: 0 }.spawn();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let _listener = handle.state_listen_async(move |state| {
let tx = tx.clone();
async move {
let _ = tx.send(state);
}
});
handle.send_cmd(Cmd::Inc).unwrap();
assert_eq!(rx.recv().await, Some(1));
handle.send_cmd(Cmd::Inc).unwrap();
assert_eq!(rx.recv().await, Some(2));
handle.stop();
}
#[tokio::test]
async fn send_cmd_fails_after_stop() {
let handle = Counter { count: 0 }.spawn();
handle.stop();
assert!(handle.send_cmd(Cmd::Inc).is_err());
}
#[tokio::test]
async fn state_keeps_latest_value() {
let handle = Counter { count: 0 }.spawn();
for _ in 0..5 {
handle.send_cmd(Cmd::Inc).unwrap();
}
let deadline = tokio::time::Instant::now() + Duration::from_secs(1);
loop {
if handle.get_state() == 5 {
break;
}
assert!(
tokio::time::Instant::now() < deadline,
"actor 没在 1s 内处理完命令"
);
tokio::time::sleep(Duration::from_millis(5)).await;
}
handle.stop();
}
#[tokio::test]
async fn cancel_token_wakes_waiter() {
let token = CancelToken::new();
let waiter = {
let token = token.clone();
tokio::spawn(async move { token.cancelled().await })
};
token.cancel();
tokio::time::timeout(Duration::from_secs(1), waiter)
.await
.expect("cancelled() 没被唤醒 —— 丢唤醒回归了")
.unwrap();
}
#[tokio::test]
async fn cancel_is_idempotent_and_sticky() {
let token = CancelToken::new();
token.cancel();
token.cancel();
tokio::time::timeout(Duration::from_millis(100), token.cancelled())
.await
.expect("已取消的 token 不该再等待");
assert!(token.is_cancelled());
assert!(token.clone().is_cancelled()); }