-
Notifications
You must be signed in to change notification settings - Fork 1k
net-utils: introduce new helper functions #7055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,21 @@ | ||
| #[cfg(feature = "dev-context-only-utils")] | ||
| use tokio::net::UdpSocket as TokioUdpSocket; | ||
| use { | ||
| crate::PortRange, | ||
| log::warn, | ||
| socket2::{Domain, SockAddr, Socket, Type}, | ||
| std::{ | ||
| io, | ||
| net::{IpAddr, SocketAddr, TcpListener, UdpSocket}, | ||
| net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, UdpSocket}, | ||
| ops::Range, | ||
| sync::atomic::{AtomicU16, Ordering}, | ||
| }, | ||
| }; | ||
| #[cfg(feature = "dev-context-only-utils")] | ||
| use {std::net::Ipv4Addr, tokio::net::UdpSocket as TokioUdpSocket}; | ||
| // base port for deconflicted allocations | ||
| const BASE_PORT: u16 = 5000; | ||
| // how much to allocate per individual process. | ||
| // we expect to have at most 64 concurrent tests in CI at any moment on a given host. | ||
| const SLICE_PER_PROCESS: u16 = (u16::MAX - BASE_PORT) / 64; | ||
| /// Retrieve a free 20-port slice for unit tests | ||
| /// | ||
| /// When running under nextest, this will try to provide | ||
| /// a unique slice of port numbers (assuming no other nextest processes | ||
| /// are running on the same host) based on NEXTEST_TEST_GLOBAL_SLOT variable | ||
|
|
@@ -25,9 +24,9 @@ const SLICE_PER_PROCESS: u16 = (u16::MAX - BASE_PORT) / 64; | |
| /// When running without nextest, this will only bump an atomic and eventually | ||
| /// panic when it runs out of port numbers to assign. | ||
| #[allow(clippy::arithmetic_side_effects)] | ||
| pub fn localhost_port_range_for_tests() -> (u16, u16) { | ||
| pub fn unique_port_range_for_tests(size: u16) -> Range<u16> { | ||
|
KirillLykov marked this conversation as resolved.
|
||
| static SLICE: AtomicU16 = AtomicU16::new(0); | ||
| let offset = SLICE.fetch_add(20, Ordering::Relaxed); | ||
| let offset = SLICE.fetch_add(size, Ordering::Relaxed); | ||
| let start = offset | ||
| + match std::env::var("NEXTEST_TEST_GLOBAL_SLOT") { | ||
| Ok(slot) => { | ||
|
|
@@ -40,8 +39,30 @@ pub fn localhost_port_range_for_tests() -> (u16, u16) { | |
| } | ||
| Err(_) => BASE_PORT, | ||
| }; | ||
| assert!(start < u16::MAX - 20, "ran out of port numbers!"); | ||
| (start, start + 20) | ||
| assert!(start < u16::MAX - size, "Ran out of port numbers!"); | ||
| start..start + size | ||
| } | ||
|
|
||
| /// Retrieve a free 20-port slice for unit tests | ||
| /// | ||
| /// When running under nextest, this will try to provide | ||
| /// a unique slice of port numbers (assuming no other nextest processes | ||
| /// are running on the same host) based on NEXTEST_TEST_GLOBAL_SLOT variable | ||
| /// The port ranges will be reused following nextest logic. | ||
| /// | ||
| /// When running without nextest, this will only bump an atomic and eventually | ||
| /// panic when it runs out of port numbers to assign. | ||
| pub fn localhost_port_range_for_tests() -> (u16, u16) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this also return a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a whole bunch of code that expects this port range format. Legacy:) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh ok sad! we do have the off by one error here still.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are absolutely right. I was convinced that the fact, that the Since in the tests that base on the I scanned thru the repo and there is 35 occurrences of the Thank you 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I understand it, the code as proposed will fix the localhost_port_range_for_tests, so it will work perfectly fine returning 1 less port. No need to change its users. I'd remove the note there, it does nothing but add confusion.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Thank you 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry i'm late on this. if we call unique_port_range_for_tests(20), we are allocating/reserving 20 ports, say port 10000 -> 10019. But the range returned is |
||
| let pr = unique_port_range_for_tests(20); | ||
| (pr.start, pr.end) | ||
| } | ||
|
|
||
| /// Bind a `UdpSocket` to a unique port. | ||
| pub fn bind_to_localhost_unique() -> io::Result<UdpSocket> { | ||
| bind_to( | ||
| IpAddr::V4(Ipv4Addr::LOCALHOST), | ||
| unique_port_range_for_tests(1).start, | ||
| ) | ||
| } | ||
|
|
||
| pub fn bind_gossip_port_in_range( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its really odd that this one got migrated up...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auto-formatting it is. Do you want me to bring it back to the previous location?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, it does not matter just odd.