diff --git a/tests/support/src/proc.rs b/tests/support/src/proc.rs index 59e177b..ece8f58 100644 --- a/tests/support/src/proc.rs +++ b/tests/support/src/proc.rs @@ -10,13 +10,22 @@ use std::process::Child; use std::sync::atomic::{AtomicU32, Ordering}; use std::time::{Duration, Instant}; -/// A port on 127.0.0.1 that was free a moment ago. +/// A port on 127.0.0.1 that was free a moment ago, and never handed out before by this +/// process (parallel tests otherwise can get the same port and talk to each other's servers). pub fn free_port() -> u16 { - std::net::TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port() + static GIVEN: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + loop { + let port = std::net::TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port(); + let mut given = GIVEN.lock().unwrap(); + if !given.contains(&port) { + given.push(port); + return port; + } + } } pub fn local(port: u16) -> SocketAddr {