From 0868f37692640e7add1a3b7797321115cd23962f Mon Sep 17 00:00:00 2001 From: Victor Polevoy Date: Mon, 27 May 2024 16:44:13 +0200 Subject: [PATCH] Move the TestConnection from integration to the utils module. --- tests/integration.rs | 43 ++----------------------------------------- tests/utils.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index e84cc613..1cc5716e 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,53 +1,14 @@ -use std::sync::atomic::AtomicU16; use std::thread; use std::time::Duration; -use crate::utils::{get_redis_connection, start_redis_server_with_module}; +use crate::utils::{get_redis_connection, start_redis_server_with_module, TestConnection}; use anyhow::Context; use anyhow::Result; -use redis::{Connection, RedisError, RedisResult, Value}; +use redis::{RedisError, RedisResult, Value}; use redis_module::RedisValue; -use utils::ChildGuard; mod utils; -fn start_redis(module_name: &str, port: u16) -> Result, &'static str> { - Ok(vec![start_redis_server_with_module(module_name, port) - .map_err(|_| "failed to start redis server")?]) -} - -struct TestConnection { - _guards: Vec, - connection: Connection, -} - -static TEST_PORT: AtomicU16 = AtomicU16::new(6479); - -impl TestConnection { - fn new(module_name: &str) -> Self { - let port = TEST_PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - - Self { - _guards: start_redis(module_name, port).expect("Redis instance started."), - connection: get_redis_connection(port).expect("Established connection to server."), - } - } -} - -impl std::ops::Deref for TestConnection { - type Target = Connection; - - fn deref(&self) -> &Self::Target { - &self.connection - } -} - -impl std::ops::DerefMut for TestConnection { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.connection - } -} - #[test] fn test_hello() -> Result<()> { let mut con = TestConnection::new("hello"); diff --git a/tests/utils.rs b/tests/utils.rs index 76e2e859..c8aac648 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -4,9 +4,53 @@ use redis::Connection; use std::fs; use std::path::PathBuf; use std::process::Command; +use std::sync::atomic::AtomicU16; use std::time::Duration; +/// Starts a redis instance with the module provided as a module name +/// and a port, returns the connection guards (`ChildGuard`) through +/// which the redis instance can be interacted with. +pub fn start_redis(module_name: &str, port: u16) -> Result, &'static str> { + Ok(vec![start_redis_server_with_module(module_name, port) + .map_err(|_| "failed to start redis server")?]) +} + +pub struct TestConnection { + _guards: Vec, + connection: Connection, +} + +static TEST_PORT: AtomicU16 = AtomicU16::new(6479); + +impl TestConnection { + /// Creates a new connection to a Redis server with the module + /// provided as a module name. + pub fn new(module_name: &str) -> Self { + let port = TEST_PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + + Self { + _guards: start_redis(module_name, port).expect("Redis instance started."), + connection: get_redis_connection(port).expect("Established connection to server."), + } + } +} + +impl std::ops::Deref for TestConnection { + type Target = Connection; + + fn deref(&self) -> &Self::Target { + &self.connection + } +} + +impl std::ops::DerefMut for TestConnection { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.connection + } +} + /// Ensure child process is killed both on normal exit and when panicking due to a failed test. +#[derive(Debug)] pub struct ChildGuard { name: &'static str, child: std::process::Child,