about summary refs log tree commit diff
path: root/src/database
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2021-05-05 17:33:44 +0300
committerVika <vika@fireburn.ru>2021-05-05 17:33:44 +0300
commit48968b403b7250c9f4ccd18e5cc22e2fe3612a8e (patch)
treeccd3c27fb47786271e98665b207098d79cb43c79 /src/database
parent3c19ea0eccc25f983a9558d7120884d16b4721c4 (diff)
downloadkittybox-48968b403b7250c9f4ccd18e5cc22e2fe3612a8e.tar.zst
Refactored the Redis instance spawning in tests to automatically kill Redis
Diffstat (limited to 'src/database')
-rw-r--r--src/database/mod.rs16
-rw-r--r--src/database/redis/mod.rs25
2 files changed, 28 insertions, 13 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs
index d6b7dd6..943a1ad 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -6,7 +6,7 @@ use crate::indieauth::User;
 mod redis;
 pub use crate::database::redis::RedisStorage;
 #[cfg(test)]
-pub use redis::tests::get_redis_instance;
+pub use redis::tests::{RedisInstance, get_redis_instance};
 
 #[derive(Serialize, Deserialize, PartialEq, Debug)]
 pub struct MicropubChannel {
@@ -222,18 +222,14 @@ mod tests {
 
     #[async_std::test]
     async fn test_redis_storage_basic_operations() {
-        let (tempdir, mut redis, uri) = get_redis_instance().await;
-        let backend = super::RedisStorage::new(uri).await.unwrap();
+        let redis_instance = get_redis_instance().await;
+        let backend = super::RedisStorage::new(redis_instance.uri().to_string()).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, uri) = get_redis_instance().await;
-        let backend = super::RedisStorage::new(uri).await.unwrap();
+    async fn test_redis_storage_channel_list() {
+        let redis_instance = get_redis_instance().await;
+        let backend = super::RedisStorage::new(redis_instance.uri().to_string()).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 ccbf831..a3b4bf8 100644
--- a/src/database/redis/mod.rs
+++ b/src/database/redis/mod.rs
@@ -249,11 +249,28 @@ impl RedisStorage {
 
 #[cfg(test)]
 pub mod tests {
-    use std::{process};
+    use std::process;
     use std::time::Duration;
     use mobc_redis::redis;
 
-    pub async fn get_redis_instance() -> (tempdir::TempDir, process::Child, String) {
+    pub struct RedisInstance {
+        // We just need to hold on to it so it won't get dropped and remove the socket
+        _tempdir: tempdir::TempDir,
+        uri: String,
+        child: std::process::Child
+    }
+    impl Drop for RedisInstance {
+        fn drop(&mut self) {
+            self.child.kill().expect("Failed to kill the child!");
+        }
+    }
+    impl RedisInstance {
+        pub fn uri(&self) -> &str {
+            &self.uri
+        }
+    }
+
+    pub async fn get_redis_instance() -> RedisInstance {
         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")
@@ -282,6 +299,8 @@ pub mod tests {
             }
         }
 
-        return (tempdir, redis_child, uri)
+        return RedisInstance {
+            uri, child: redis_child, _tempdir: tempdir
+        }
     }
 }
\ No newline at end of file