hoverfly-java icon indicating copy to clipboard operation
hoverfly-java copied to clipboard

Fix port conflict when running multiple unit tests in parallel

Open cretzel opened this issue 6 years ago • 0 comments

The "random" port selection can still lead to port conflicts, when running multiple unit tests in parallel.

This is because port selection and check is done within the config validation phase, but the actual port binding is done later when the hoverfly process is started. Example:

  1. Thread A selects free port, eg. 49999 (Port is not bound yet)
  2. Thread B selects same port
  3. Thread A starts hoverfly process on port 49999 (Port is bound now)
  4. Thread B starts hoverfly process on port 49999 -> IllegalStateException

Fixed by moving port selection from validation to Hoverfly.startHoverflyProcess and synchronizing on Hoverfly.class.

This required some refactoring, mainly because the HoverflyClient cannot be instantiated in constructor, because the actuall port is not known yet. HoverflyClient instantiation moved to startHoverflyProcess. Introduced HoverflyClientFactory for testability.

cretzel avatar Oct 02 '19 16:10 cretzel