Skip to content

Commit 77e1eae

Browse files
committed
Ensure that the redis port will be in a valid range.
1 parent 7137565 commit 77e1eae

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

redis/tests/support/mod.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,19 @@ impl RedisServer {
383383
/// process, so this must be used with care (since here we only use it for tests, it's
384384
/// mostly okay).
385385
pub fn get_random_available_port() -> u16 {
386-
let addr = &"127.0.0.1:0".parse::<SocketAddr>().unwrap().into();
387-
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
388-
socket.set_reuse_address(true).unwrap();
389-
socket.bind(addr).unwrap();
390-
socket.listen(1).unwrap();
391-
let listener = TcpListener::from(socket);
392-
listener.local_addr().unwrap().port()
386+
for _ in 0..100 {
387+
let addr = &"127.0.0.1:0".parse::<SocketAddr>().unwrap().into();
388+
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
389+
socket.set_reuse_address(true).unwrap();
390+
socket.bind(addr).unwrap();
391+
socket.listen(1).unwrap();
392+
let listener = TcpListener::from(socket);
393+
let port = listener.local_addr().unwrap().port();
394+
if port < 55535 {
395+
return port;
396+
}
397+
}
398+
panic!("Couldn't get a valid port");
393399
}
394400

395401
impl Drop for RedisServer {

0 commit comments

Comments
 (0)