Skip to content

net-utils (& others): swap tests to non-overlapping port allocation#6957

Closed
puhtaytow wants to merge 27 commits intoanza-xyz:masterfrom
puhtaytow:swap-bind_to_unspecified-to-bind_to-in-tests-0
Closed

net-utils (& others): swap tests to non-overlapping port allocation#6957
puhtaytow wants to merge 27 commits intoanza-xyz:masterfrom
puhtaytow:swap-bind_to_unspecified-to-bind_to-in-tests-0

Conversation

@puhtaytow
Copy link
Copy Markdown

@puhtaytow puhtaytow commented Jul 14, 2025

Problem

A bunch of tests still relies on either hardcoded ports or bind_to_localhost and bind_to_unspecified. This creates flaky tests in cases where unique port ranges are needed.

Related to #6886

Summary of Changes

  • Swapped tests to non-overlapping port allocation.
  • Added helper function to convert Range to (u16, u16) as still some functions expect the tuple.
  • Added helper functions to allocate unique ports for localhost, range and single scenarios.
  • Reduced amount of imports and lines / less verbose overall.
  • Swapped UNSPECIFIED to LOCALHOST at many points.

@mergify mergify Bot requested a review from a team July 14, 2025 11:30
@alexpyattaev alexpyattaev added the CI Pull Request is ready to enter CI label Jul 14, 2025
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Jul 14, 2025
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jul 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.2%. Comparing base (fac7555) to head (ff43cf2).
Report is 53 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6957   +/-   ##
=======================================
  Coverage    83.2%    83.2%           
=======================================
  Files         856      856           
  Lines      376845   376862   +17     
=======================================
+ Hits       313722   313754   +32     
+ Misses      63123    63108   -15     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

alexpyattaev
alexpyattaev previously approved these changes Jul 14, 2025
Copy link
Copy Markdown

@alexpyattaev alexpyattaev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@puhtaytow
Copy link
Copy Markdown
Author

Thank you!

Thank you! 🙏

@alexpyattaev alexpyattaev requested a review from lijunwangs July 15, 2025 11:59
Comment thread core/src/repair/repair_service.rs Outdated
let mut duplicate_slot_repair_statuses = HashMap::new();
let dead_slot = 9;
let receive_socket = &bind_to_unspecified().unwrap();
let receive_socket = &bind_to(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These code are so much repeated, maybe it is worth to create a new bind_to_unspecified utility function.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that might be nice, but let us fix our CI first. Just changing semantics of bind_to_unspecified is not a great plan either as there are legit cases where you might want to call it.

@KirillLykov
Copy link
Copy Markdown

I agree with Lijun that would be nice to not repeat this code everywhere.

@alexpyattaev
Copy link
Copy Markdown

Ok, fine. @puhtaytow seems like we have to come up with a way to have a new bind_to_unspecified that works via the same mechanism as localhost_port_range_for_tests but does not allocate a full 20-port span every time.

@puhtaytow
Copy link
Copy Markdown
Author

Surely. Let me look at this 🙏

@steviez
Copy link
Copy Markdown

steviez commented Jul 17, 2025

Minor note but:

net-utils / core, rpc, streamer, turbine, validator: introduce new helper function / swap tests to non-overlapping ports

certainly exceeds the suggested character limit for commit titles

lijunwangs
lijunwangs previously approved these changes Jul 17, 2025
Copy link
Copy Markdown

@lijunwangs lijunwangs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lijunwangs lijunwangs added the CI Pull Request is ready to enter CI label Jul 17, 2025
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Jul 17, 2025
@puhtaytow puhtaytow changed the title net-utils / core, rpc, streamer, turbine, validator: introduce new helper function / swap tests to non-overlapping ports net-utils, core, rpc, streamer, turbine, validator: swap test to non-overlapping port solution Jul 17, 2025
@puhtaytow
Copy link
Copy Markdown
Author

Thank you guys! 🙏

@puhtaytow puhtaytow changed the title net-utils, core, rpc, streamer, turbine, validator: swap test to non-overlapping port solution net-utils, core, rpc, streamer, turbine, validator: swap tests to non-overlapping port solution Jul 17, 2025
@gregcusack gregcusack self-requested a review July 17, 2025 18:09
Comment thread net-utils/src/sockets.rs
Comment thread net-utils/src/sockets.rs Outdated
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have an off by one error here? If start is 10000, and i want 20 ports, i get back (10000, 10020) which is 21 ports.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good question, i wasn't confident enough so decided to check it with the test and the code below pass positively.

        let (pr_s, pr_e) = localhost_port_range_for_tests();
        assert_eq!(pr_e - pr_s, 20);
        ...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well ya the difference between 10020 and 10000 is 20. but if we return both of them, the user will use them all. but that is 21 ports returned even though the difference is 20.
in your example above, if i use every port from pr_e to pr_s, that is 21 ports. if we count it out: 0, 1, 2, 3, ..., 19, 20. that is 21.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, would you suggest to add note in the function docs or maybe return Range? instead?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switching to range would be great, yes. Since this is a new function we might as well have proper API for it.

…han one

Co-authored-by: Greg Cusack <greg.cusack@anza.xyz>
@puhtaytow puhtaytow marked this pull request as draft July 18, 2025 06:14
@alexpyattaev alexpyattaev changed the title net-utils, core, rpc, streamer, turbine, validator: swap tests to non-overlapping port solution net-utils (& others): swap tests to non-overlapping port allocation solution Jul 18, 2025
@alexpyattaev alexpyattaev changed the title net-utils (& others): swap tests to non-overlapping port allocation solution net-utils (& others): swap tests to non-overlapping port allocation Jul 18, 2025
Comment thread net-utils/src/lib.rs
url::Url,
};

/// Helper for code that still expects port ranges as `(u16, u16)`
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be here or somewhere else?

@puhtaytow puhtaytow marked this pull request as ready for review July 18, 2025 16:27
@puhtaytow
Copy link
Copy Markdown
Author

The Range was fixed in as suggested. I used this occasion to fix more stuff that didn't look like it makes much sense.

Please tell me WDYT 🙏

@gregcusack gregcusack self-requested a review July 18, 2025 17:05
@alexpyattaev
Copy link
Copy Markdown

The Range was fixed in as suggested. I used this occasion to fix more stuff that didn't look like it makes much sense.

Generally it is not advised to do "drive-by" fixes for things unrelated to the subject of the PR. Not a huge deal here, but please avoid when possible.

Copy link
Copy Markdown

@alexpyattaev alexpyattaev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it is going in the right direction, but I feel like we should split this into 2 PRs: one to introduce new functions in net-utils, another to patch them into the codebase. Also left a few nits and bikeshedding suggestions.

Comment thread net-utils/src/sockets.rs
///
/// 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_single_for_tests() -> u16 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function redundant? If not, maybe it should be called unique_port_for_tests ?

Comment thread net-utils/src/sockets.rs
bind_to_async(IpAddr::V4(Ipv4Addr::LOCALHOST), 0).await
bind_to_async(
IpAddr::V4(Ipv4Addr::LOCALHOST),
localhost_port_range_for_tests().start,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we eating an entire port range for this one?

Comment thread streamer/src/sendmmsg.rs
#[test]
pub fn test_multicast_msg() {
let reader = bind_to_localhost().expect("bind");
let reader = bind_to_localhost_unique().expect("should bind");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are changing the messages, maybe giving them numbers would be nice?

@puhtaytow puhtaytow marked this pull request as draft July 21, 2025 05:26
@puhtaytow
Copy link
Copy Markdown
Author

Turned this PR into draft with no intentions to ever merge it. As suggested by @alexpyattaev it would be splitted into multiple PRs starting from this one #7055

Thank you and sorry for the chaos 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants