Skip to content

Commit

Permalink
Rollup merge of rust-lang#136635 - jieyouxu:base_port, r=joboet
Browse files Browse the repository at this point in the history
Remove outdated `base_port` calculation in std net test

This was never modified since `std::net` was originally introduced in 395709c, when at that time, each CI runner was running multiple jobs concurrently. This seems to have originally caused issues with jobs fighting over the same ports. This is not the case in the current CI infrastructure, so remove this relic in favor of a simple constant base port number.

I double-checked `19600` and nearby port numbers, and this isn't a well-known port number AFAICT[^1].

Closes rust-lang#136633.

[^1]: At the time of writing.
  • Loading branch information
matthiaskrgr authored Feb 7, 2025
2 parents ca56709 + 9e345fd commit 3536503
Showing 1 changed file with 3 additions and 30 deletions.
33 changes: 3 additions & 30 deletions library/std/src/net/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToS
use crate::sync::atomic::{AtomicUsize, Ordering};

static PORT: AtomicUsize = AtomicUsize::new(0);
const BASE_PORT: u16 = 19600;

pub fn next_test_ip4() -> SocketAddr {
let port = PORT.fetch_add(1, Ordering::Relaxed) as u16 + base_port();
let port = PORT.fetch_add(1, Ordering::Relaxed) as u16 + BASE_PORT;
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port))
}

pub fn next_test_ip6() -> SocketAddr {
let port = PORT.fetch_add(1, Ordering::Relaxed) as u16 + base_port();
let port = PORT.fetch_add(1, Ordering::Relaxed) as u16 + BASE_PORT;
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), port, 0, 0))
}

Expand All @@ -30,31 +31,3 @@ pub fn tsa<A: ToSocketAddrs>(a: A) -> Result<Vec<SocketAddr>, String> {
Err(e) => Err(e.to_string()),
}
}

// The bots run multiple builds at the same time, and these builds
// all want to use ports. This function figures out which workspace
// it is running in and assigns a port range based on it.
fn base_port() -> u16 {
let cwd = if cfg!(target_env = "sgx") {
String::from("sgx")
} else {
env::current_dir().unwrap().into_os_string().into_string().unwrap()
};
let dirs = [
"32-opt",
"32-nopt",
"musl-64-opt",
"cross-opt",
"64-opt",
"64-nopt",
"64-opt-vg",
"64-debug-opt",
"all-opt",
"snap3",
"dist",
"sgx",
];
dirs.iter().enumerate().find(|&(_, dir)| cwd.contains(dir)).map(|p| p.0).unwrap_or(0) as u16
* 1000
+ 19600
}

0 comments on commit 3536503

Please sign in to comment.