GEODE-8404: Make tests use AvailablePortHelper, not AvailablePort #5861
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.
Make nearly all tests use AvailablePortHelper instead of AvailablePort
to obtain ports. See the rationale below.
Also: Remove unused methods from AvailablePort.
Rationale:
AvailablePort is inherently risky as a source of ports for tests.
Each "get available port" method obtains candidate port numbers from the
desired range by randomly sampling with replacement. This means that
multiple calls can return the same port number if the port is not put
into use between calls.
Some tests failed intermittently because they made multiple calls to
AvailablePort, received the same port on multiple calls, and unknowingly
attempted bind multiple sockets to the same port number, resulting in a
BindException. See GEODE-6622 for examples.
AvailablePortHelper does not have this problem. It obtains candidate
port numbers round robin. After returning an available port,
AvailablePortHelper will not return that port again in that JVM until it
has tested every other port in the range for availability.
To reduce the chance of different JVMs selecting each other's ports,
AvailablePortHelper selects a random starting point for its round robin
search in each JVM.
For distributed tests, DUnit further arranges for the
AvailablePortHelper in each JVM to start its round robin search in a
distinct place, maximally distant from the starting points of all other
JVMs. Because AvailablePort selects randomly from the full port range,
it cannot benefit from this techique.
The problems caused by AvailablePort are rare, but inevitable, with a
frequency determined by the total size of the port range. An upcoming
change will make the available port range much smaller (~400 ports
instead of the current ~10000 ports), which will greatly increase the
frequency of this problem. But the problem exists now, and results in
intermittent BindExceptions.