redis master tests

This commit is contained in:
Aravinth Manivannan 2021-06-08 20:43:47 +05:30
parent 20786b84a9
commit a50a21d33b
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 50 additions and 7 deletions

View File

@ -66,3 +66,5 @@ jobs:
uses: codecov/codecov-action@v1 uses: codecov/codecov-action@v1
with: with:
file: cobertura.xml file: cobertura.xml
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

View File

@ -114,6 +114,7 @@ pub enum CaptchaError {
} }
#[cfg(feature = "full")] #[cfg(feature = "full")]
#[cfg(not(tarpaulin_include))]
impl From<RedisError> for CaptchaError { impl From<RedisError> for CaptchaError {
fn from(e: RedisError) -> Self { fn from(e: RedisError) -> Self {
Self::RedisError(e) Self::RedisError(e)
@ -121,6 +122,7 @@ impl From<RedisError> for CaptchaError {
} }
#[cfg(feature = "full")] #[cfg(feature = "full")]
#[cfg(not(tarpaulin_include))]
impl From<actix::MailboxError> for CaptchaError { impl From<actix::MailboxError> for CaptchaError {
fn from(_: actix::MailboxError) -> Self { fn from(_: actix::MailboxError) -> Self {
Self::MailboxError Self::MailboxError

View File

@ -132,13 +132,13 @@ impl RedisConnection {
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub mod tests {
use super::*; use super::*;
use crate::defense::{Level, LevelBuilder}; use crate::defense::{Level, LevelBuilder};
use crate::master::embedded::counter::tests::get_mcaptcha; use crate::master::embedded::counter::tests::get_mcaptcha;
use crate::master::redis::master::{Master, Redis}; use crate::master::redis::master::{Master, Redis};
async fn connect(redis: &Redis) -> RedisConnection { pub async fn connect(redis: &Redis) -> RedisConnection {
match &redis { match &redis {
Redis::Single(c) => { Redis::Single(c) => {
let con = c.get_async_connection().await.unwrap(); let con = c.get_async_connection().await.unwrap();

View File

@ -50,12 +50,12 @@ pub struct Master {
} }
impl Master { impl Master {
pub async fn new(redis: Redis) -> Self { pub async fn new(redis: Redis) -> CaptchaResult<Self> {
let con = Self::connect(&redis).await; let con = Self::connect(&redis).await;
con.is_module_loaded().await.unwrap(); con.is_module_loaded().await?;
let con = Rc::new(con); let con = Rc::new(con);
let master = Self { redis, con }; let master = Self { redis, con };
master Ok(master)
} }
async fn connect(redis: &Redis) -> RedisConnection { async fn connect(redis: &Redis) -> RedisConnection {
@ -99,14 +99,53 @@ impl Handler<AddSite> for Master {
type Result = (); type Result = ();
fn handle(&mut self, m: AddSite, ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, m: AddSite, ctx: &mut Self::Context) -> Self::Result {
let (tx, rx) = mpsc::channel(); //let (tx, rx) = mpsc::channel();
let con = Rc::clone(&self.con); let con = Rc::clone(&self.con);
let fut = async move { let fut = async move {
let res = con.add_mcaptcha(m).await; let res = con.add_mcaptcha(m).await;
tx.send(res).unwrap(); //tx.send(res).unwrap();
} }
.into_actor(self); .into_actor(self);
ctx.wait(fut); ctx.wait(fut);
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::defense::{Level, LevelBuilder};
use crate::master::embedded::counter::tests::get_mcaptcha;
use crate::master::redis::connection::tests::connect;
use crate::master::redis::master::{Master, Redis};
const CAPTCHA_NAME: &str = "REDIS_MASTER_CAPTCHA_TEST";
const DURATION: usize = 10;
#[actix_rt::test]
async fn redis_master_works() {
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let master = Master::new(Redis::Single(client)).await;
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let r = connect(&Redis::Single(client)).await;
assert!(master.is_ok());
let master = master.unwrap();
{
let _ = r.delete_captcha(CAPTCHA_NAME).await;
}
let addr = master.start();
let add_mcaptcha_msg = AddSite {
id: CAPTCHA_NAME.into(),
mcaptcha: get_mcaptcha(),
};
addr.send(add_mcaptcha_msg).await.unwrap();
let add_visitor_msg = AddVisitor(CAPTCHA_NAME.into());
addr.send(add_visitor_msg).await.unwrap();
let visitors = r.get_visitors(CAPTCHA_NAME).await.unwrap();
assert_eq!(visitors, 1);
}
}