test/connect: fix sporadic "Address already in use" failures - #1617
Merged
Conversation
The connect.t test previously generated a pseudo-random port number using rand(). This approach occasionally resulted in port collisions when the randomly selected port was already in use by another process or lingering in a TIME_WAIT state, causing the test suite to sporadically fail with "bind(): Address already in use". Update the tests in connect.t to bind sockets using sin_port=0, allowing the operating system to assign an available ephemeral port. Afterwards, getsockname() is used to retrieve the assigned port so that the client sockets can be configured to connect to it. For test_connect_with_no_peer(), we introduce a new helper block_port(). This test explicitly requires connecting to a port where no server is listening to verify that an ECONNREFUSED error is returned. block_port() binds a temporary socket to port 0 to acquire an unused port from the OS, and keeps the file descriptor open to prevent other processes from claiming the port before and during the test run. Because listen() is never called on this socket, connecting to it is guaranteed to be refused. After test_connect_with_no_peer() completed the socket is closed. Remove the port randomization code in test() as it is now unused. Signed-off-by: Florian Schmaus <flo@geekplace.eu>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The connect.t test previously generated a pseudo-random port number using rand(). This approach occasionally resulted in port collisions when the randomly selected port was already in use by another process or lingering in a TIME_WAIT state, causing the test suite to sporadically fail with "bind(): Address already in use".
Update the tests in connect.t to bind sockets using sin_port=0, allowing the operating system to assign an available ephemeral port. Afterwards, getsockname() is used to retrieve the assigned port so that the client sockets can be configured to connect to it.
For test_connect_with_no_peer(), we introduce a new helper get_free_port(). This test explicitly requires connecting to a port where no server is listening to verify that an ECONNREFUSED error is returned. get_free_port() binds a temporary socket to port 0 to acquire an unused port from the OS, then immediately closes it. This increases the chances of the test to connect to an empty port.
Remove the port randomization code in test() as it is now unused.