Compare commits

...

2 Commits

Author SHA1 Message Date
Valeryi Savich 8dadf99c2b Moved Definition in the systems module 2021-07-17 21:23:08 +02:00
Valeryi Savich 872d26c46f Added tests with actors for definitions 2021-07-17 21:17:26 +02:00
4 changed files with 43 additions and 17 deletions

View File

@ -1,6 +1,5 @@
pub mod actor_ref;
pub mod context;
pub mod definition;
pub mod local_state;
pub mod state;
pub mod traits;

View File

@ -1,19 +1,18 @@
use std::fmt::Debug;
use async_trait::async_trait;
use crate::actor::context::Context;
use crate::error::Result;
#[async_trait]
pub trait Actor: Sync {
fn new() -> Self
where
Self: Sized;
async fn on_init(&self, _ctx: &mut Context) {}
async fn on_sync(&self, _ctx: &mut Context) {}
async fn on_stopped(&self, _ctx: &mut Context) {}
async fn on_terminated(&self, _ctx: &mut Context) {}
async fn on_failed(&self, _ctx: &mut Context) {}
async fn on_finished(&self, _ctx: &mut Context) {}
async fn on_deinit(&self, _ctx: &mut Context) {}
async fn handler(&self, ctx: &mut Context) -> Result<()>;
pub trait Actor: 'static {
async fn on_init(&mut self, _ctx: &mut Context) {}
async fn on_sync(&mut self, _ctx: &mut Context) {}
async fn on_stopped(&mut self, _ctx: &mut Context) {}
async fn on_terminated(&mut self, _ctx: &mut Context) {}
async fn on_failed(&mut self, _ctx: &mut Context) {}
async fn on_finished(&mut self, _ctx: &mut Context) {}
async fn on_deinit(&mut self, _ctx: &mut Context) {}
async fn handler(&mut self, ctx: &mut Context) -> Result<()>;
}

View File

@ -117,8 +117,25 @@ impl Hash for Definition {
#[cfg(test)]
mod tests {
use crate::actor::definition::Definition;
use crate::actor::context::Context;
use crate::actor::traits::Actor;
use crate::error::Result;
use crate::routing::path::Scope;
use crate::system::definition::Definition;
use async_trait::async_trait;
#[derive(Debug)]
struct FakeActor {
counter: u32,
}
#[async_trait]
impl Actor for FakeActor {
async fn handler(&mut self, ctx: &mut Context) -> Result<()> {
Ok(())
}
}
fn fake_actor_name() -> String {
let index = 1;
@ -134,6 +151,16 @@ mod tests {
assert_eq!(instance.redundancy, 1);
}
#[test]
fn test_definition_with_custom_actor() {
let instance = Definition::new().actor(FakeActor { counter: 0 });
assert_eq!(instance.actor.is_some(), true);
assert_eq!(instance.scope, Scope::User);
assert_eq!(instance.actor_name_fn.is_none(), true);
assert_eq!(instance.redundancy, 1);
}
#[test]
fn test_definition_with_custom_name() {
let instance = Definition::new().name("test_name");

View File

@ -1,8 +1,9 @@
mod global_state;
mod node;
use once_cell::sync::Lazy;
use crate::system::node::Node;
pub mod definition;
mod global_state;
mod node;
static SYSTEM: Lazy<Node> = Lazy::new(|| Node::new());