diff --git a/tests/common/mod.rs b/tests/common/mod.rs index b70d2d675..0795946a5 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -215,22 +215,18 @@ pub(crate) fn random_storage_path() -> PathBuf { temp_path } -pub(crate) fn random_port() -> u16 { - let mut rng = rng(); - rng.random_range(5000..32768) -} - pub(crate) fn random_listening_addresses() -> Vec { let num_addresses = 2; - let mut listening_addresses = Vec::with_capacity(num_addresses); + let mut listening_addresses = HashSet::new(); - for _ in 0..num_addresses { - let rand_port = random_port(); + while listening_addresses.len() < num_addresses { + let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let rand_port = socket.local_addr().unwrap().port(); let address: SocketAddress = format!("127.0.0.1:{}", rand_port).parse().unwrap(); - listening_addresses.push(address); + listening_addresses.insert(address); } - listening_addresses + listening_addresses.into_iter().collect() } pub(crate) fn random_node_alias() -> Option {