mirror of
https://github.com/hapifhir/hapi-fhir.git
synced 2025-02-17 02:15:22 +00:00
PortUtil fixes from my working branch to avoid test failures
This commit is contained in:
parent
0848fdf1eb
commit
cff79e6aef
@ -217,28 +217,20 @@ public class PortUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isAvailable(int port) {
|
private static boolean isAvailable(int port) {
|
||||||
ServerSocket ss = null;
|
ourLog.info("Testing a bind on port {}", port);
|
||||||
DatagramSocket ds = null;
|
try (ServerSocket ss = new ServerSocket(port)) {
|
||||||
try {
|
|
||||||
ss = new ServerSocket(port);
|
|
||||||
ss.setReuseAddress(true);
|
ss.setReuseAddress(true);
|
||||||
ds = new DatagramSocket(port);
|
try (DatagramSocket ds = new DatagramSocket(port)) {
|
||||||
ds.setReuseAddress(true);
|
ds.setReuseAddress(true);
|
||||||
return true;
|
ourLog.info("Successfully bound port {}", port);
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
ourLog.info("Failed to bind port {}: {}", port, e.toString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
ourLog.info("Failed to bind port {}: {}", port, e.toString());
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
|
||||||
if (ds != null) {
|
|
||||||
ds.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ss != null) {
|
|
||||||
try {
|
|
||||||
ss.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
/* should not be thrown */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,13 +63,23 @@ public class PortUtilTest {
|
|||||||
for (int j = 0; j < portsPerTaskCount; j++) {
|
for (int j = 0; j < portsPerTaskCount; j++) {
|
||||||
int nextFreePort = portUtil.getNextFreePort();
|
int nextFreePort = portUtil.getNextFreePort();
|
||||||
|
|
||||||
|
boolean bound;
|
||||||
try (ServerSocket ss = new ServerSocket()) {
|
try (ServerSocket ss = new ServerSocket()) {
|
||||||
ss.bind(new InetSocketAddress("localhost", nextFreePort));
|
ss.bind(new InetSocketAddress("localhost", nextFreePort));
|
||||||
|
bound = true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
String msg = "Failure binding new port " + nextFreePort + ": " + e.toString();
|
bound = false;
|
||||||
ourLog.error(msg, e);
|
}
|
||||||
errors.add(msg);
|
|
||||||
|
|
||||||
|
if (!bound) {
|
||||||
|
try (ServerSocket ss = new ServerSocket()) {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
ss.bind(new InetSocketAddress("localhost", nextFreePort));
|
||||||
|
} catch (Exception e) {
|
||||||
|
String msg = "Failure binding new port (second attempt) " + nextFreePort + ": " + e.toString();
|
||||||
|
ourLog.error(msg, e);
|
||||||
|
errors.add(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ports.add(nextFreePort);
|
ports.add(nextFreePort);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user