Skip to content

Commit 28bceac

Browse files
committed
Cleaned up test code for tcp/unix usage
1 parent 7ecf0e7 commit 28bceac

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

runtests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test_run() {
1111
echo "======================================================================"
1212

1313
if [ x$REDISRS_UNIFIED_TARGET != x1 ]; then
14-
export CARGO_TARGET_DIR=target/test-$2
14+
export CARGO_TARGET_DIR="target/test-$2"
1515
fi
1616

1717
RUST_TEST_THREADS=1 REDISRS_SERVER_TYPE=$1 cargo test --features="with-rustc-json $3"

src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn parse_redis_url(input: &str) -> Result<url::Url, ()> {
4141
/// Not all connection addresses are supported on all platforms. For instance
4242
/// to connect to a unix socket you need to run this on an operating system
4343
/// that supports them.
44-
#[derive(Clone, Debug)]
44+
#[derive(Clone, Debug, PartialEq)]
4545
pub enum ConnectionAddr {
4646
/// Format for this is `(host, port)`.
4747
Tcp(String, u16),

tests/test_basic.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum ServerType {
2121

2222
pub struct RedisServer {
2323
pub process: process::Child,
24-
server_type: ServerType,
24+
addr: redis::ConnectionAddr,
2525
}
2626

2727
impl ServerType {
@@ -41,31 +41,36 @@ impl RedisServer {
4141
let mut cmd = process::Command::new("redis-server");
4242
cmd
4343
.stdout(process::Stdio::null())
44-
.stderr(process::Stdio::null())
45-
.arg("--port").arg(SERVER_PORT.to_string())
46-
.arg("--bind").arg("127.0.0.1");
44+
.stderr(process::Stdio::null());
4745

48-
if server_type == ServerType::Unix {
49-
cmd.arg("--unixsocket").arg(SERVER_UNIX_PATH);
50-
}
46+
let addr = match server_type {
47+
ServerType::Tcp => {
48+
cmd
49+
.arg("--port").arg(SERVER_PORT.to_string())
50+
.arg("--bind").arg("127.0.0.1");
51+
redis::ConnectionAddr::Tcp("127.0.0.1".to_string(), SERVER_PORT)
52+
},
53+
ServerType::Unix => {
54+
cmd
55+
.arg("--port").arg("0")
56+
.arg("--unixsocket").arg(SERVER_UNIX_PATH);
57+
redis::ConnectionAddr::Unix(PathBuf::from(SERVER_UNIX_PATH))
58+
}
59+
};
5160

5261
let process = cmd.spawn().unwrap();
53-
RedisServer { process: process, server_type: server_type }
62+
RedisServer {
63+
process: process,
64+
addr: addr,
65+
}
5466
}
5567

5668
pub fn wait(&mut self) {
5769
self.process.wait().unwrap();
5870
}
5971

60-
pub fn get_client_addr(&self) -> redis::ConnectionAddr {
61-
match self.server_type {
62-
ServerType::Tcp => {
63-
redis::ConnectionAddr::Tcp("127.0.0.1".to_string(), SERVER_PORT)
64-
},
65-
ServerType::Unix => {
66-
redis::ConnectionAddr::Unix(PathBuf::from(SERVER_UNIX_PATH))
67-
}
68-
}
72+
pub fn get_client_addr(&self) -> &redis::ConnectionAddr {
73+
&self.addr
6974
}
7075
}
7176

@@ -88,7 +93,7 @@ impl TestContext {
8893
let server = RedisServer::new();
8994

9095
let client = redis::Client::open(redis::ConnectionInfo {
91-
addr: Box::new(server.get_client_addr()),
96+
addr: Box::new(server.get_client_addr().clone()),
9297
db: 0,
9398
passwd: None,
9499
}).unwrap();
@@ -127,7 +132,7 @@ impl TestContext {
127132

128133
#[test]
129134
fn test_parse_redis_url() {
130-
let redis_url = format!("redis://127.0.0.1:{}/0", SERVER_PORT);
135+
let redis_url = format!("redis://127.0.0.1:1234/0");
131136
match redis::parse_redis_url(&redis_url) {
132137
Ok(_) => assert!(true),
133138
Err(_) => assert!(false),
@@ -568,14 +573,23 @@ fn test_tuple_decoding_regression() {
568573

569574
#[test]
570575
fn test_invalid_protocol() {
576+
let ctx = TestContext::new();
577+
let (addr, url) = match *ctx.server.get_client_addr() {
578+
redis::ConnectionAddr::Tcp(ref host, port) => {
579+
(format!("{}:{}", host, port),
580+
format!("redis://{}:{}", host, port))
581+
},
582+
_ => { return; }
583+
};
584+
571585
use std::thread;
572586
use std::error::Error;
573587
use std::io::Write;
574588
use std::net::TcpListener;
575589
use redis::{RedisResult, Parser};
576590

577591
let child = thread::spawn(move || -> Result<(), Box<Error + Send + Sync>> {
578-
let listener = try!(TcpListener::bind(&format!("127.0.0.1:{}", SERVER_PORT)[..]));
592+
let listener = try!(TcpListener::bind(&addr[..]));
579593
let mut stream = try!(listener.incoming().next().unwrap());
580594
// read the request and respond with garbage
581595
let _: redis::Value = try!(Parser::new(&mut stream).parse_value());
@@ -586,7 +600,7 @@ fn test_invalid_protocol() {
586600
});
587601
sleep(Duration::from_millis(100));
588602
// some work here
589-
let cli = redis::Client::open(&format!("redis://127.0.0.1:{}/", SERVER_PORT)[..]).unwrap();
603+
let cli = redis::Client::open(&url[..]).unwrap();
590604
let con = cli.get_connection().unwrap();
591605

592606
let mut result: redis::RedisResult<u8>;

0 commit comments

Comments
 (0)