Test failing CI tests due to port conflicts (#4134)
## Issue Addressed #4127. PR to test port conflicts in CI tests . ## Proposed Changes See issue for more details, potential solution could be adding a cache bound by time to the `unused_port` function.
This commit is contained in:
parent
036b797b2c
commit
d351cc8d8d
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -8546,6 +8546,11 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "unused_port"
|
name = "unused_port"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"lru_cache",
|
||||||
|
"parking_lot 0.12.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "url"
|
name = "url"
|
||||||
|
@ -6,3 +6,6 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
lru_cache = { path = "../lru_cache" }
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
parking_lot = "0.12.0"
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
use std::net::{TcpListener, UdpSocket};
|
use lazy_static::lazy_static;
|
||||||
|
use lru_cache::LRUTimeCache;
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use std::net::{SocketAddr, TcpListener, UdpSocket};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub enum Transport {
|
pub enum Transport {
|
||||||
@ -12,6 +16,13 @@ pub enum IpVersion {
|
|||||||
Ipv6,
|
Ipv6,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const CACHED_PORTS_TTL: Duration = Duration::from_secs(300);
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref FOUND_PORTS_CACHE: Mutex<LRUTimeCache<u16>> =
|
||||||
|
Mutex::new(LRUTimeCache::new(CACHED_PORTS_TTL));
|
||||||
|
}
|
||||||
|
|
||||||
/// A convenience wrapper over [`zero_port`].
|
/// A convenience wrapper over [`zero_port`].
|
||||||
pub fn unused_tcp4_port() -> Result<u16, String> {
|
pub fn unused_tcp4_port() -> Result<u16, String> {
|
||||||
zero_port(Transport::Tcp, IpVersion::Ipv4)
|
zero_port(Transport::Tcp, IpVersion::Ipv4)
|
||||||
@ -48,6 +59,20 @@ pub fn zero_port(transport: Transport, ipv: IpVersion) -> Result<u16, String> {
|
|||||||
IpVersion::Ipv6 => std::net::Ipv6Addr::LOCALHOST.into(),
|
IpVersion::Ipv6 => std::net::Ipv6Addr::LOCALHOST.into(),
|
||||||
};
|
};
|
||||||
let socket_addr = std::net::SocketAddr::new(localhost, 0);
|
let socket_addr = std::net::SocketAddr::new(localhost, 0);
|
||||||
|
let mut unused_port: u16;
|
||||||
|
loop {
|
||||||
|
unused_port = find_unused_port(transport, socket_addr)?;
|
||||||
|
let mut cache_lock = FOUND_PORTS_CACHE.lock();
|
||||||
|
if !cache_lock.contains(&unused_port) {
|
||||||
|
cache_lock.insert(unused_port);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(unused_port)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_unused_port(transport: Transport, socket_addr: SocketAddr) -> Result<u16, String> {
|
||||||
let local_addr = match transport {
|
let local_addr = match transport {
|
||||||
Transport::Tcp => {
|
Transport::Tcp => {
|
||||||
let listener = TcpListener::bind(socket_addr).map_err(|e| {
|
let listener = TcpListener::bind(socket_addr).map_err(|e| {
|
||||||
|
Loading…
Reference in New Issue
Block a user