From 0d8de9092b68fcdec74c7bee9e037aeda29c7a87 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 19 Jul 2021 13:07:46 +0530 Subject: [PATCH] embedded master: rename --- src/master/embedded/master.rs | 55 ++++++++++++++++++++++++++++++----- src/master/mod.rs | 7 +++++ 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/master/embedded/master.rs b/src/master/embedded/master.rs index d5c23b1..a6c23ca 100644 --- a/src/master/embedded/master.rs +++ b/src/master/embedded/master.rs @@ -27,7 +27,7 @@ use tokio::sync::oneshot::channel; use super::counter::Counter; use crate::errors::*; -use crate::master::messages::{AddSite, AddVisitor}; +use crate::master::messages::{AddSite, AddVisitor, Rename}; use crate::master::Master as MasterTrait; /// This Actor manages the [Counter] actors. @@ -71,6 +71,15 @@ impl Master { pub fn rm_site(&mut self, id: &str) { self.sites.remove(id); } + + /// renames [Counter] actor + pub fn rename(&mut self, msg: Rename) { + // If actor isn't present, it's okay to not throw an error + // since actors are lazyily initialized and are cleaned up when inactive + if let Some((_, counter)) = self.sites.remove(&msg.name) { + self.add_site(counter, msg.rename_to); + } + } } impl Actor for Master { @@ -115,6 +124,21 @@ impl Handler for Master { } } +impl Handler for Master { + type Result = MessageResult; + + fn handle(&mut self, m: Rename, ctx: &mut Self::Context) -> Self::Result { + self.rename(m); + let (tx, rx) = channel(); + let fut = async move { + let _ = tx.send(Ok(())); + } + .into_actor(self); + ctx.spawn(fut); + MessageResult(rx) + } +} + /// Message to get an [Counter] actor from master #[derive(Message)] #[rtype(result = "Option>")] @@ -198,24 +222,39 @@ mod tests { use super::*; use crate::master::embedded::counter::tests::*; use crate::master::messages::AddSiteBuilder; + use crate::master::messages::RenameBuilder; + use crate::MCaptcha; #[actix_rt::test] async fn master_actor_works() { let addr = Master::new(1).start(); + let get_add_site_msg = |id: String, mcaptcha: MCaptcha| { + AddSiteBuilder::default() + .id(id) + .mcaptcha(mcaptcha) + .build() + .unwrap() + }; + let id = "yo"; - let mcaptcha = get_mcaptcha(); - let msg = AddSiteBuilder::default() - .id(id.into()) - .mcaptcha(mcaptcha.clone()) - .build() - .unwrap(); + let msg = get_add_site_msg(id.into(), get_mcaptcha()); addr.send(msg).await.unwrap(); let mcaptcha_addr = addr.send(GetSite(id.into())).await.unwrap(); assert!(mcaptcha_addr.is_some()); + let new_id = "yoyo"; + let rename = RenameBuilder::default() + .name(id.into()) + .rename_to(new_id.into()) + .build() + .unwrap(); + addr.send(rename).await.unwrap(); + let mcaptcha_addr = addr.send(GetSite(new_id.into())).await.unwrap(); + assert!(mcaptcha_addr.is_some()); + let addr_doesnt_exist = addr.send(GetSite("a".into())).await.unwrap(); assert!(addr_doesnt_exist.is_none()); @@ -225,7 +264,7 @@ mod tests { // delay_for(timer_expire).await; // delay_for(timer_expire).await; - let mcaptcha_addr = addr.send(GetSite(id.into())).await.unwrap(); + let mcaptcha_addr = addr.send(GetSite(new_id.into())).await.unwrap(); assert_eq!(mcaptcha_addr, None); } } diff --git a/src/master/mod.rs b/src/master/mod.rs index b3d26f4..d4f1815 100644 --- a/src/master/mod.rs +++ b/src/master/mod.rs @@ -83,4 +83,11 @@ pub mod messages { pub id: String, pub mcaptcha: MCaptcha, } + + #[derive(Message, Builder)] + #[rtype(result = "Receiver>")] + pub struct Rename { + pub name: String, + pub rename_to: String, + } }