Skip to content

Commit 0868f37

Browse files
committed
Move the TestConnection from integration to the utils module.
1 parent 758b11a commit 0868f37

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

tests/integration.rs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,14 @@
1-
use std::sync::atomic::AtomicU16;
21
use std::thread;
32
use std::time::Duration;
43

5-
use crate::utils::{get_redis_connection, start_redis_server_with_module};
4+
use crate::utils::{get_redis_connection, start_redis_server_with_module, TestConnection};
65
use anyhow::Context;
76
use anyhow::Result;
8-
use redis::{Connection, RedisError, RedisResult, Value};
7+
use redis::{RedisError, RedisResult, Value};
98
use redis_module::RedisValue;
10-
use utils::ChildGuard;
119

1210
mod utils;
1311

14-
fn start_redis(module_name: &str, port: u16) -> Result<Vec<ChildGuard>, &'static str> {
15-
Ok(vec![start_redis_server_with_module(module_name, port)
16-
.map_err(|_| "failed to start redis server")?])
17-
}
18-
19-
struct TestConnection {
20-
_guards: Vec<ChildGuard>,
21-
connection: Connection,
22-
}
23-
24-
static TEST_PORT: AtomicU16 = AtomicU16::new(6479);
25-
26-
impl TestConnection {
27-
fn new(module_name: &str) -> Self {
28-
let port = TEST_PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
29-
30-
Self {
31-
_guards: start_redis(module_name, port).expect("Redis instance started."),
32-
connection: get_redis_connection(port).expect("Established connection to server."),
33-
}
34-
}
35-
}
36-
37-
impl std::ops::Deref for TestConnection {
38-
type Target = Connection;
39-
40-
fn deref(&self) -> &Self::Target {
41-
&self.connection
42-
}
43-
}
44-
45-
impl std::ops::DerefMut for TestConnection {
46-
fn deref_mut(&mut self) -> &mut Self::Target {
47-
&mut self.connection
48-
}
49-
}
50-
5112
#[test]
5213
fn test_hello() -> Result<()> {
5314
let mut con = TestConnection::new("hello");

tests/utils.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,53 @@ use redis::Connection;
44
use std::fs;
55
use std::path::PathBuf;
66
use std::process::Command;
7+
use std::sync::atomic::AtomicU16;
78
use std::time::Duration;
89

10+
/// Starts a redis instance with the module provided as a module name
11+
/// and a port, returns the connection guards (`ChildGuard`) through
12+
/// which the redis instance can be interacted with.
13+
pub fn start_redis(module_name: &str, port: u16) -> Result<Vec<ChildGuard>, &'static str> {
14+
Ok(vec![start_redis_server_with_module(module_name, port)
15+
.map_err(|_| "failed to start redis server")?])
16+
}
17+
18+
pub struct TestConnection {
19+
_guards: Vec<ChildGuard>,
20+
connection: Connection,
21+
}
22+
23+
static TEST_PORT: AtomicU16 = AtomicU16::new(6479);
24+
25+
impl TestConnection {
26+
/// Creates a new connection to a Redis server with the module
27+
/// provided as a module name.
28+
pub fn new(module_name: &str) -> Self {
29+
let port = TEST_PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
30+
31+
Self {
32+
_guards: start_redis(module_name, port).expect("Redis instance started."),
33+
connection: get_redis_connection(port).expect("Established connection to server."),
34+
}
35+
}
36+
}
37+
38+
impl std::ops::Deref for TestConnection {
39+
type Target = Connection;
40+
41+
fn deref(&self) -> &Self::Target {
42+
&self.connection
43+
}
44+
}
45+
46+
impl std::ops::DerefMut for TestConnection {
47+
fn deref_mut(&mut self) -> &mut Self::Target {
48+
&mut self.connection
49+
}
50+
}
51+
952
/// Ensure child process is killed both on normal exit and when panicking due to a failed test.
53+
#[derive(Debug)]
1054
pub struct ChildGuard {
1155
name: &'static str,
1256
child: std::process::Child,

0 commit comments

Comments
 (0)