use {
crate::sockets::UNIQUE_ALLOC_BASE_PORT,
std::{
ops::Range,
sync::atomic::{AtomicU16, Ordering},
},
};
const SLICE_PER_PROCESS: u16 = (u16::MAX - UNIQUE_ALLOC_BASE_PORT) / 64;
#[allow(clippy::arithmetic_side_effects)]
pub fn unique_port_range_for_tests_internal(size: u16) -> Range<u16> {
static SLICE: AtomicU16 = AtomicU16::new(0);
let offset = SLICE.fetch_add(size, Ordering::SeqCst);
let start = offset
+ match std::env::var("NEXTEST_TEST_GLOBAL_SLOT") {
Ok(slot) => {
let slot: u16 = slot.parse().unwrap();
assert!(
offset < SLICE_PER_PROCESS,
"Overrunning into the port range of another test! Consider using fewer ports \
per test."
);
UNIQUE_ALLOC_BASE_PORT + slot * SLICE_PER_PROCESS
}
Err(_) => UNIQUE_ALLOC_BASE_PORT,
};
assert!(start < u16::MAX - size, "Ran out of port numbers!");
start..start + size
}