From 8dbcd29c8d6ed79969cf753490c76b83aedd9e1b Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 8 Nov 2017 17:12:06 -0500 Subject: [PATCH] Dont reuse ports in tests --- .../src/main/java/ca/uhn/fhir/util/PortUtil.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java index 620e36dbbc4..0ec0fe86848 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java @@ -21,12 +21,14 @@ package ca.uhn.fhir.util; */ import java.net.ServerSocket; +import java.util.LinkedHashSet; /** * Provides server ports */ @CoverageIgnore public class PortUtil { + private static LinkedHashSet ourPorts = new LinkedHashSet<>(); /* * Non instantiable @@ -41,9 +43,13 @@ public class PortUtil { public static int findFreePort() { ServerSocket server; try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - server.close(); + int port; + do { + server = new ServerSocket(0); + port = server.getLocalPort(); + server.close(); + } while (!ourPorts.add(port)); + Thread.sleep(500); return port; } catch (Exception e) {