Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
None
-
None
Description
We have many tests throughout the codebase that use NetworkUtils.getAvailablePort / getAvailableTcpPort / getAvailableUdpPort.
These util methods need to be removed and not used. They are a very bad practice that result in intermittent failures. These methods work by creating a ServerSocket using Port 0, starting it, then determining the port that it's listening on. Then the ServerSocket is closed and the port it was listening on was returned so that it can be used for another server to be started.
This is problematic for two reasons. Firstly, there's no reason to think that the port will be available when the next server is started - there are all sorts of OS settings for how long the sockets are kept alive, etc.
Secondly, another process, or another unit test using the same mechanism may well start as soon as the ServerSocket is closed and before the second server is started.
Either of these conditions will cause a BindException, which we see frequently in failed unit tests.
The proper mechanism to handle this is to start the desired server using port 0 to begin with, and then asking the server which port it's listening on once it's started.
Attachments
Issue Links
- links to