#[cfg(test)]
pub mod tests {
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use crate::{Actor, ActorRef};
use crate::control::Control;
pub static COUNTER: AtomicU64 = AtomicU64::new(0);
#[derive(Debug, PartialEq, Clone)]
pub enum DelayingSends {
Ping,
}
#[derive(Debug, PartialEq, Clone)]
pub enum DelayingCalls {
DoPong,
Pong,
}
pub struct DelayingActor {
waited: bool, }
impl DelayingActor {
pub fn new() -> Self {
Self {
waited: false
}
}
}
impl Actor for DelayingActor {
type SendMessage = DelayingSends;
type CallMessage = DelayingCalls;
type ErrorType = ();
async fn handle_sends(&mut self, _msg: Self::SendMessage) -> Control {
if !self.waited {
tokio::time::sleep(Duration::new(0, 100)).await;
}
let _j = COUNTER.fetch_add(1, Ordering::Relaxed);
Control::Ok
}
async fn handle_calls(&mut self, _msg: Self::CallMessage) -> (Control, Result<Self::CallMessage, Self::ErrorType>) {
(Control::Ok, Ok(DelayingCalls::Pong))
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum CounterSends {
Count,
}
#[derive(Debug, PartialEq, Clone)]
pub enum CounterCalls {
GetCount,
Reply(u64),
}
pub struct SimpleCounter {
count: u64,
immediate_quit: bool, }
impl SimpleCounter {
pub fn new(immediate_quit: bool) -> Self {
Self {
count: 0,
immediate_quit,
}
}
}
impl Actor for SimpleCounter {
type SendMessage = CounterSends;
type CallMessage = CounterCalls;
type ErrorType = ();
async fn on_initialization(&mut self, _self_ref: ActorRef<Self>) -> Control {
if self.immediate_quit {
Control::Shutdown
} else {
Control::Ok
}
}
async fn handle_sends(&mut self, _msg: Self::SendMessage) -> Control {
self.count += 1;
Control::Ok
}
async fn handle_calls(&mut self, _msg: Self::CallMessage) -> (Control, Result<Self::CallMessage, Self::ErrorType>) {
(Control::Ok, Ok(CounterCalls::Reply(self.count)))
}
}
}