[tests] Ports: never hand out the same free port twice in one process, so parallel tests cannot reach each other's servers
This commit is contained in:
@@ -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<Vec<u16>> = 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 {
|
||||
|
||||
Reference in New Issue
Block a user