From 3db009977ae4144b0a3f71dbb310f62f11c31893 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 12 Jun 2021 13:25:18 +0530 Subject: [PATCH] ping redis --- src/redis/mcaptcha_redis.rs | 7 +++++++ src/redis/mod.rs | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/redis/mcaptcha_redis.rs b/src/redis/mcaptcha_redis.rs index faff5d4..a2e1642 100644 --- a/src/redis/mcaptcha_redis.rs +++ b/src/redis/mcaptcha_redis.rs @@ -110,6 +110,11 @@ impl MCaptchaRedisConnection { Ok(()) } + /// Ping redis + pub async fn ping(&self) -> bool { + self.0.ping().await + } + /// Add visitor pub async fn add_visitor(&self, msg: AddVisitor) -> CaptchaResult> { let res: String = self.0.exec(redis::cmd(ADD_VISITOR).arg(&[msg.0])).await?; @@ -328,6 +333,8 @@ pub mod tests { r.add_token(&add_challenge_msg).await.unwrap(); assert!(r.delete_token(&challenge_msg).await.is_ok()); + assert!(r.ping().await); + assert!(r.delete_captcha(CAPTCHA_NAME).await.is_ok()); } } diff --git a/src/redis/mod.rs b/src/redis/mod.rs index 745ebb5..a1d35ec 100644 --- a/src/redis/mod.rs +++ b/src/redis/mod.rs @@ -76,6 +76,18 @@ impl RedisConnection { RedisConnection::Cluster(con) => cmd.query(&mut *con.borrow_mut()), } } + + pub async fn ping(&self) -> bool { + if let Ok(redis::Value::Status(v)) = self.exec(&mut redis::cmd("PING")).await { + if v == "PONG" { + true + } else { + false + } + } else { + false + } + } } #[derive(Clone)] @@ -126,3 +138,16 @@ impl Redis { (redis, client) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[actix_rt::test] + async fn ping_works() { + let r = Redis::new(RedisConfig::Single("redis://127.0.0.1".into())) + .await + .unwrap(); + assert!(r.get_client().ping().await); + } +}