GetCurrentVisitor count

This commit is contained in:
Aravinth Manivannan 2021-03-15 19:37:42 +05:30
parent 24e75f7cc9
commit a4929922bf
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 26 additions and 2 deletions

View File

@ -67,8 +67,6 @@ async fn main() -> std::io::Result<()> {
.build()
.unwrap();
p
// create and start MCaptcha actor that uses the above defense configuration
// This is what manages the difficulty factor of sites that an mCaptcha protects
let mcaptcha = MCaptchaBuilder::default()

View File

@ -229,6 +229,19 @@ impl Handler<AddVisitor> for MCaptcha {
}
}
/// Message to get the visitor count
#[derive(Message)]
#[rtype(result = "u32")]
pub struct GetCurrentVisitorCount;
impl Handler<GetCurrentVisitorCount> for MCaptcha {
type Result = MessageResult<GetCurrentVisitorCount>;
fn handle(&mut self, _: GetCurrentVisitorCount, _ctx: &mut Self::Context) -> Self::Result {
MessageResult(self.visitor_threshold)
}
}
#[cfg(test)]
pub mod tests {
use super::*;
@ -331,4 +344,17 @@ pub mod tests {
Some(CaptchaError::PleaseSetValue("duration".into()))
);
}
#[actix_rt::test]
async fn get_current_visitor_count_works() {
let addr: MyActor = get_counter().start();
addr.send(AddVisitor).await.unwrap();
addr.send(AddVisitor).await.unwrap();
addr.send(AddVisitor).await.unwrap();
addr.send(AddVisitor).await.unwrap();
let count = addr.send(GetCurrentVisitorCount).await.unwrap();
assert_eq!(count, 4);
}
}