From 4a1085b7e9a6231f3e21334af1ea43b71ee8d918 Mon Sep 17 00:00:00 2001 From: Vika Date: Wed, 5 May 2021 16:31:52 +0300 Subject: Moved the Redis spawner to the Redis module where it belongs, refactored tests to use the Redis database instead of a fake one --- src/database/mod.rs | 52 +++++++++-------------------------------------- src/database/redis/mod.rs | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 42 deletions(-) (limited to 'src/database') diff --git a/src/database/mod.rs b/src/database/mod.rs index cdd5c43..1a6aa84 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,17 +1,17 @@ #![warn(missing_docs)] use async_trait::async_trait; use serde::{Serialize,Deserialize}; +use crate::indieauth::User; +mod redis; +pub use crate::database::redis::RedisStorage; +#[cfg(test)] +pub use redis::tests::get_redis_instance; #[cfg(test)] mod memory; #[cfg(test)] pub(crate) use crate::database::memory::MemoryStorage; -use crate::indieauth::User; - -mod redis; -pub use crate::database::redis::RedisStorage; - #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct MicropubChannel { pub uid: String, @@ -169,9 +169,8 @@ pub trait Storage: Clone + Send + Sync { #[cfg(test)] mod tests { use super::{Storage, MicropubChannel}; - use std::{process}; - use std::time::Duration; use serde_json::json; + use super::redis::tests::get_redis_instance; async fn test_backend_basic_operations(backend: Backend) { let post: serde_json::Value = json!({ @@ -236,49 +235,18 @@ mod tests { test_backend_get_channel_list(backend).await } - async fn get_redis_backend() -> (tempdir::TempDir, process::Child, super::RedisStorage) { - let tempdir = tempdir::TempDir::new("redis").expect("failed to create tempdir"); - let socket = tempdir.path().join("redis.sock"); - let redis_child = process::Command::new("redis-server") - .current_dir(&tempdir) - .arg("--port").arg("0") - .arg("--unixsocket").arg(&socket) - .stdout(process::Stdio::null()) - .stderr(process::Stdio::null()) - .spawn().expect("Failed to spawn Redis"); - println!("redis+unix:///{}", socket.to_str().unwrap()); - let uri = format!("redis+unix:///{}", socket.to_str().unwrap()); - // There should be a slight delay, we need to wait for Redis to spin up - let client = redis::Client::open(uri.clone()).unwrap(); - let millisecond = Duration::from_millis(1); - let mut retries: usize = 0; - const MAX_RETRIES: usize = 60 * 1000/*ms*/; - while let Err(err) = client.get_connection() { - if err.is_connection_refusal() { - async_std::task::sleep(millisecond).await; - retries += 1; - if retries > MAX_RETRIES { - panic!("Timeout waiting for Redis, last error: {}", err); - } - } else { - panic!("Could not connect: {}", err); - } - } - let backend = super::RedisStorage::new(uri).await.unwrap(); - - return (tempdir, redis_child, backend) - } - #[async_std::test] async fn test_redis_storage_basic_operations() { - let (tempdir, mut redis, backend) = get_redis_backend().await; + let (tempdir, mut redis, uri) = get_redis_instance().await; + let backend = super::RedisStorage::new(uri).await.unwrap(); test_backend_basic_operations(backend).await; redis.kill().expect("Redis wasn't running"); drop(tempdir); } #[async_std::test] async fn test_redis_storage_channel_support() { - let (tempdir, mut redis, backend) = get_redis_backend().await; + let (tempdir, mut redis, uri) = get_redis_instance().await; + let backend = super::RedisStorage::new(uri).await.unwrap(); test_backend_get_channel_list(backend).await; redis.kill().expect("Redis wasn't running"); drop(tempdir); diff --git a/src/database/redis/mod.rs b/src/database/redis/mod.rs index eded583..ccbf831 100644 --- a/src/database/redis/mod.rs +++ b/src/database/redis/mod.rs @@ -245,4 +245,43 @@ impl RedisStorage { Err(e) => Err(e.into()) } } +} + +#[cfg(test)] +pub mod tests { + use std::{process}; + use std::time::Duration; + use mobc_redis::redis; + + pub async fn get_redis_instance() -> (tempdir::TempDir, process::Child, String) { + let tempdir = tempdir::TempDir::new("redis").expect("failed to create tempdir"); + let socket = tempdir.path().join("redis.sock"); + let redis_child = process::Command::new("redis-server") + .current_dir(&tempdir) + .arg("--port").arg("0") + .arg("--unixsocket").arg(&socket) + .stdout(process::Stdio::null()) + .stderr(process::Stdio::null()) + .spawn().expect("Failed to spawn Redis"); + println!("redis+unix:///{}", socket.to_str().unwrap()); + let uri = format!("redis+unix:///{}", socket.to_str().unwrap()); + // There should be a slight delay, we need to wait for Redis to spin up + let client = redis::Client::open(uri.clone()).unwrap(); + let millisecond = Duration::from_millis(1); + let mut retries: usize = 0; + const MAX_RETRIES: usize = 60 * 1000/*ms*/; + while let Err(err) = client.get_connection() { + if err.is_connection_refusal() { + async_std::task::sleep(millisecond).await; + retries += 1; + if retries > MAX_RETRIES { + panic!("Timeout waiting for Redis, last error: {}", err); + } + } else { + panic!("Could not connect: {}", err); + } + } + + return (tempdir, redis_child, uri) + } } \ No newline at end of file -- cgit 1.4.1