Skip to content
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

Fixes #37413 - Let the kernel assign a TCP port in tests #893

Merged
merged 1 commit into from
May 10, 2024

Conversation

ekohl
Copy link
Member

@ekohl ekohl commented May 8, 2024

This binds on port 0, which lets the kernel decide which port to use. The port is then read back from the listener. Because the kernel knows which ports it's allowed to bind to, it removes the risk of a failure due to collisions or policies.

@ekohl ekohl force-pushed the 37413-let-kernel-allocate-port branch from 367c40d to 83c33ed Compare May 8, 2024 13:32
@ekohl ekohl changed the title Fixes #37413 - Let the kernel assign a TCP port Fixes #37413 - Let the kernel assign a TCP port in tests May 8, 2024
Copy link

@dosas dosas left a comment

Choose a reason for hiding this comment

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

Very nice, thanks. Fixes the issue with docker.

test/test_helper.rb Outdated Show resolved Hide resolved
end
Timeout.timeout(2) do
sleep(0.1) until can_connect?('localhost', @settings.send("#{protocol}_port"))
sleep(0.1) until can_connect?('localhost', @settings["#{protocol}_port"])
Copy link
Member Author

Choose a reason for hiding this comment

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

Technically this change isn't needed, but it feels more consistent if we're also setting it as a hash on line 49.

end
end

def can_connect?(host, port)
return false if port == 0
Copy link
Member Author

Choose a reason for hiding this comment

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

This took me a while to figure out, but the server is started in a thread, so the port isn't immediately set.

This binds on port 0, which lets the kernel decide which port to use.
The port is then read back from the listener. Because the kernel knows
which ports it's allowed to bind to, it removes the risk of a failure
due to collisions or policies.
@ekohl ekohl force-pushed the 37413-let-kernel-allocate-port branch from 83c33ed to bf8b44f Compare May 8, 2024 15:10
@ekohl ekohl merged commit bf8b44f into theforeman:develop May 10, 2024
4 checks passed
@ekohl ekohl deleted the 37413-let-kernel-allocate-port branch May 10, 2024 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants