Skip to content

Commit 291426e

Browse files
committed
Wait for free port in legal range.
This issue happens often when I ran the tests locally on my MacOS machine.
1 parent aee538b commit 291426e

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
@@ -390,13 +390,19 @@ impl RedisServer {
390390
/// process, so this must be used with care (since here we only use it for tests, it's
391391
/// mostly okay).
392392
pub fn get_random_available_port() -> u16 {
393-
let addr = &"127.0.0.1:0".parse::<SocketAddr>().unwrap().into();
394-
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
395-
socket.set_reuse_address(true).unwrap();
396-
socket.bind(addr).unwrap();
397-
socket.listen(1).unwrap();
398-
let listener = TcpListener::from(socket);
399-
listener.local_addr().unwrap().port()
393+
for _ in 0..10000 {
394+
let addr = &"127.0.0.1:0".parse::<SocketAddr>().unwrap().into();
395+
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
396+
socket.set_reuse_address(true).unwrap();
397+
socket.bind(addr).unwrap();
398+
socket.listen(1).unwrap();
399+
let listener = TcpListener::from(socket);
400+
let port = listener.local_addr().unwrap().port();
401+
if port < 55535 {
402+
return port;
403+
}
404+
}
405+
panic!("Couldn't get a valid port");
400406
}
401407

402408
impl Drop for RedisServer {

0 commit comments

Comments
 (0)