about summary refs log tree commit diff
path: root/src/database/redis/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/database/redis/mod.rs')
-rw-r--r--src/database/redis/mod.rs39
1 files changed, 39 insertions, 0 deletions
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