Skip to content

streamer, tests: migrate to non-overlapping ports#6895

Merged
lijunwangs merged 8 commits intoanza-xyz:masterfrom
puhtaytow:streamer-non-overlapping-socket-bind-api-0
Jul 25, 2025
Merged

streamer, tests: migrate to non-overlapping ports#6895
lijunwangs merged 8 commits intoanza-xyz:masterfrom
puhtaytow:streamer-non-overlapping-socket-bind-api-0

Conversation

@puhtaytow
Copy link
Copy Markdown

@puhtaytow puhtaytow commented Jul 9, 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 #7055

Summary of Changes

Migrated tests to non-overlapping ports.
This PR should close migration for the Streamer package.

@0xbrw
Copy link
Copy Markdown

0xbrw commented Jul 9, 2025

@alexpyattaev or @apfitzge when you get a chance, can I have you take a look?

@apfitzge
Copy link
Copy Markdown

apfitzge commented Jul 9, 2025

It looks correct to me on skim, but it's a bit verbose to do this everywhere. Can we not have a bind_to_localhost_for_tests that just accesses some static behind a mutex that is lazily initialized with this iterator? That'd make a lot of these simple function changes instead of having like 4-5 extra lines per change.

@puhtaytow
Copy link
Copy Markdown
Author

puhtaytow commented Jul 10, 2025

It looks correct to me on skim, but it's a bit verbose to do this everywhere. Can we not have a bind_to_localhost_for_tests that just accesses some static behind a mutex that is lazily initialized with this iterator? That'd make a lot of these simple function changes instead of having like 4-5 extra lines per change.

I looked at the https://github.com/anza-xyz/agave/blob/master/net-utils/src/sockets.rs#L27-L45 and to me, the function does utilize the nextest env variable to coordinate the tests globally, there is also the atomic SLICE.. it looks like we could skip mutex in this case.

Obviously, correct me if im wrong 🙏

pub fn localhost_port_range_for_tests() -> (u16, u16) {
    static SLICE: AtomicU16 = AtomicU16::new(0);
    let offset = SLICE.fetch_add(20, Ordering::Relaxed);
    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."
                );
                BASE_PORT + slot * SLICE_PER_PROCESS
            }
            Err(_) => BASE_PORT,
        };
    assert!(start < u16::MAX - 20, "ran out of port numbers!");
    (start, start + 20)
}

To address the unnecessary verbosity, i introduced the helper function in net-utils as per your suggestion. https://github.com/anza-xyz/agave/compare/master...puhtaytow:agave:streamer-non-overlapping-socket-bind-api-1?expand=1

Please tell me if that is the correct place for it and if so then how would you like to merge the changes to net-utils, as separate PR?

Thank you for the suggestion 🙏 i do appreciate a lot any constructive input.

@alexpyattaev
Copy link
Copy Markdown

Please do not fix what is not broken =) localhost_port_range_for_tests uses both nextest env variables AND local atomics to coordinate port allocation because we do not know if it will run under cargo test or cargo nextest. cargo test uses 1 process for all tests => atomics work. cargo nextest uses process-per-test model, and thus we need to use env variables. One possible improvement for that function was suggested by t-nelson that would use shared memory instead of env variables for inter-process coordination, as that would allow the same mechanism to be used under both test runners.

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.

Let us not create mutable iterators where direct access would achieve the same result=)

Comment thread streamer/src/nonblocking/quic.rs Outdated
Comment thread streamer/src/nonblocking/quic.rs Outdated
Comment thread streamer/src/nonblocking/quic.rs Outdated
Comment thread streamer/src/packet.rs Outdated
@puhtaytow
Copy link
Copy Markdown
Author

@alexpyattaev thank you for pointing these out / fixed 🙏

@puhtaytow puhtaytow requested a review from alexpyattaev July 10, 2025 12:27
@alexpyattaev alexpyattaev added the CI Pull Request is ready to enter CI label Jul 11, 2025
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Jul 11, 2025
alexpyattaev
alexpyattaev previously approved these changes Jul 11, 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.

LGTM thank you!

@puhtaytow
Copy link
Copy Markdown
Author

LGTM thank you!

Thank you! 🙏

@alexpyattaev alexpyattaev force-pushed the streamer-non-overlapping-socket-bind-api-0 branch from ecf24b2 to 08d3893 Compare July 14, 2025 07:00
@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
@alexpyattaev
Copy link
Copy Markdown

This is currently blocked by #6957

@puhtaytow puhtaytow force-pushed the streamer-non-overlapping-socket-bind-api-0 branch 2 times, most recently from da41b28 to 020d7ad Compare July 25, 2025 07:27
@puhtaytow puhtaytow changed the title streamer, tests: use the non-overlapping socket bind api streamer, tests: migrate to non-overlapping ports Jul 25, 2025
@puhtaytow puhtaytow requested a review from alexpyattaev July 25, 2025 07:34
@puhtaytow puhtaytow force-pushed the streamer-non-overlapping-socket-bind-api-0 branch 5 times, most recently from 6fd128c to 84dd696 Compare July 25, 2025 07:46
@puhtaytow puhtaytow force-pushed the streamer-non-overlapping-socket-bind-api-0 branch from 84dd696 to 11c04b2 Compare July 25, 2025 07:54
@alexpyattaev alexpyattaev added the CI Pull Request is ready to enter CI label Jul 25, 2025
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Jul 25, 2025
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.2%. Comparing base (7baf884) to head (11c04b2).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##           master    #6895     +/-   ##
=========================================
- Coverage    83.2%    83.2%   -0.1%     
=========================================
  Files         852      852             
  Lines      373846   373845      -1     
=========================================
- Hits       311408   311353     -55     
- Misses      62438    62492     +54     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

LGTM

@alexpyattaev alexpyattaev requested a review from lijunwangs July 25, 2025 18:54
@lijunwangs lijunwangs merged commit 2b89434 into anza-xyz:master Jul 25, 2025
43 checks passed
@puhtaytow
Copy link
Copy Markdown
Author

Thank you!

@puhtaytow puhtaytow deleted the streamer-non-overlapping-socket-bind-api-0 branch July 26, 2025 05:01
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.

7 participants