Skip to content

Commit

Permalink
Move the TestConnection from integration to the utils module.
Browse files Browse the repository at this point in the history
  • Loading branch information
iddm committed May 27, 2024
1 parent 758b11a commit 0868f37
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
43 changes: 2 additions & 41 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<ChildGuard>, &'static str> {
Ok(vec![start_redis_server_with_module(module_name, port)
.map_err(|_| "failed to start redis server")?])
}

struct TestConnection {
_guards: Vec<ChildGuard>,
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");
Expand Down
44 changes: 44 additions & 0 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<ChildGuard>, &'static str> {
Ok(vec![start_redis_server_with_module(module_name, port)
.map_err(|_| "failed to start redis server")?])
}

pub struct TestConnection {
_guards: Vec<ChildGuard>,
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,
Expand Down

0 comments on commit 0868f37

Please sign in to comment.