simple-actors/tests/ping.rs
Blake Rain bb7bb6b712
Some checks failed
Check / check (push) Failing after 41s
feat: add some new features
2024-04-06 12:10:25 +01:00

42 lines
837 B
Rust

use async_trait::async_trait;
use simple_actors::{Actor, Context, Handler, Message};
use test_log::test;
#[derive(Default)]
struct PingActor {
count: usize,
}
impl Actor for PingActor {
type Error = String;
}
struct Ping;
impl Message for Ping {
type Reply = usize;
}
#[async_trait]
impl Handler<Ping> for PingActor {
async fn handle(&mut self, _: Ping) -> Result<usize, Self::Error> {
self.count += 1;
Ok(self.count)
}
}
#[test(tokio::test)]
async fn test_actor_ping() -> Result<(), Box<dyn std::error::Error>> {
let ctx = Context::default();
let hdl = ctx.spawn_default::<PingActor>().await;
let res = hdl.send(Ping).await?;
assert_eq!(res, 1);
// We have to drop the handle here, or 'ctx.wait_all' will not return.
drop(hdl);
ctx.wait_all().await;
Ok(())
}