@@ -21,7 +21,7 @@ enum ServerType {
21
21
22
22
pub struct RedisServer {
23
23
pub process : process:: Child ,
24
- server_type : ServerType ,
24
+ addr : redis :: ConnectionAddr ,
25
25
}
26
26
27
27
impl ServerType {
@@ -41,31 +41,36 @@ impl RedisServer {
41
41
let mut cmd = process:: Command :: new ( "redis-server" ) ;
42
42
cmd
43
43
. 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 ( ) ) ;
47
45
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
+ } ;
51
60
52
61
let process = cmd. spawn ( ) . unwrap ( ) ;
53
- RedisServer { process : process, server_type : server_type }
62
+ RedisServer {
63
+ process : process,
64
+ addr : addr,
65
+ }
54
66
}
55
67
56
68
pub fn wait ( & mut self ) {
57
69
self . process . wait ( ) . unwrap ( ) ;
58
70
}
59
71
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
69
74
}
70
75
}
71
76
@@ -88,7 +93,7 @@ impl TestContext {
88
93
let server = RedisServer :: new ( ) ;
89
94
90
95
let client = redis:: Client :: open ( redis:: ConnectionInfo {
91
- addr : Box :: new ( server. get_client_addr ( ) ) ,
96
+ addr : Box :: new ( server. get_client_addr ( ) . clone ( ) ) ,
92
97
db : 0 ,
93
98
passwd : None ,
94
99
} ) . unwrap ( ) ;
@@ -127,7 +132,7 @@ impl TestContext {
127
132
128
133
#[ test]
129
134
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" ) ;
131
136
match redis:: parse_redis_url ( & redis_url) {
132
137
Ok ( _) => assert ! ( true ) ,
133
138
Err ( _) => assert ! ( false ) ,
@@ -568,14 +573,23 @@ fn test_tuple_decoding_regression() {
568
573
569
574
#[ test]
570
575
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
+
571
585
use std:: thread;
572
586
use std:: error:: Error ;
573
587
use std:: io:: Write ;
574
588
use std:: net:: TcpListener ;
575
589
use redis:: { RedisResult , Parser } ;
576
590
577
591
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 [ ..] ) ) ;
579
593
let mut stream = try!( listener. incoming ( ) . next ( ) . unwrap ( ) ) ;
580
594
// read the request and respond with garbage
581
595
let _: redis:: Value = try!( Parser :: new ( & mut stream) . parse_value ( ) ) ;
@@ -586,7 +600,7 @@ fn test_invalid_protocol() {
586
600
} ) ;
587
601
sleep ( Duration :: from_millis ( 100 ) ) ;
588
602
// 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 ( ) ;
590
604
let con = cli. get_connection ( ) . unwrap ( ) ;
591
605
592
606
let mut result: redis:: RedisResult < u8 > ;
0 commit comments