Added custom name for a definition

This commit is contained in:
Valeryi Savich 2021-07-07 23:59:41 +02:00
parent 3b7bc3e7cf
commit d365ce0f7e
2 changed files with 23 additions and 1 deletions

View File

@ -1,6 +1,8 @@
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
use uuid::Uuid;
use crate::actor::traits::Actor;
use crate::routing::path::{ActorPath, Scope};
@ -8,6 +10,8 @@ type CustomActorNameFn = dyn Fn() -> String + Send + 'static;
/// A structure that holds configuration of the Bastion actor.
pub struct Definition {
/// A unique definition name;
name: String,
/// A struct that implements actor's behaviour
actor: Option<Arc<dyn Actor>>,
/// Defines a used scope for instantiating actors.
@ -21,12 +25,14 @@ pub struct Definition {
impl Definition {
/// Returns a new instance of the actor's definition.
pub fn new() -> Self {
let name = Uuid::new_v4().to_string();
let scope = Scope::User;
let actor_name_fn = None;
let redundancy = 1;
let actor = None;
Definition {
name,
actor,
scope,
actor_name_fn,
@ -34,6 +40,12 @@ impl Definition {
}
}
/// Overrides the definition's name. The passed value must be unique.
pub fn name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
/// Sets the actor to schedule.
pub fn actor<T: 'static>(mut self, actor: T) -> Self
where
@ -108,6 +120,16 @@ mod tests {
assert_eq!(instance.redundancy, 1);
}
#[test]
fn test_definition_with_custom_name() {
let instance = Definition::new().name("test_name");
assert_eq!(instance.name, "test_name");
assert_eq!(instance.scope, Scope::User);
assert_eq!(instance.actor_name_fn.is_some(), false);
assert_eq!(instance.redundancy, 1);
}
#[test]
fn test_definition_with_custom_actor_name() {
let instance = Definition::new().custom_actor_name(fake_actor_name);

View File

@ -72,7 +72,7 @@ where
}
#[cfg(test)]
mod message_target_tests {
mod envelope_tests {
use crate::mailbox::envelope::Envelope;
use crate::mailbox::message::{Message, MessageType};