Make port delays configurable

This commit is contained in:
James Agnew 2019-03-29 17:19:22 -04:00
parent 8791ed5793
commit 3c18275b3d
2 changed files with 47 additions and 32 deletions

View File

@ -20,6 +20,7 @@ package ca.uhn.fhir.util;
* #L% * #L%
*/ */
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -27,7 +28,6 @@ import java.io.IOException;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -80,11 +80,10 @@ public class PortUtil {
private static final int SPACE_SIZE = 100; private static final int SPACE_SIZE = 100;
private static final Logger ourLog = LoggerFactory.getLogger(PortUtil.class); private static final Logger ourLog = LoggerFactory.getLogger(PortUtil.class);
private static final PortUtil INSTANCE = new PortUtil(); private static final PortUtil INSTANCE = new PortUtil();
private static int ourPortDelay = 500;
private List<ServerSocket> myControlSockets = new ArrayList<>(); private List<ServerSocket> myControlSockets = new ArrayList<>();
private Integer myCurrentControlSocketPort = null; private Integer myCurrentControlSocketPort = null;
private int myCurrentOffset = 0; private int myCurrentOffset = 0;
/** /**
* Constructor - * Constructor -
*/ */
@ -108,32 +107,6 @@ public class PortUtil {
myCurrentControlSocketPort = null; myCurrentControlSocketPort = null;
} }
private static boolean isAvailable(int port) {
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
return false;
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
/* should not be thrown */
}
}
}
}
/** /**
* Clear and release all control sockets * Clear and release all control sockets
*/ */
@ -221,7 +194,7 @@ public class PortUtil {
// } // }
// //
try { try {
Thread.sleep(500); Thread.sleep(ourPortDelay);
} catch (InterruptedException theE) { } catch (InterruptedException theE) {
// ignore // ignore
} }
@ -234,6 +207,41 @@ public class PortUtil {
} }
} }
@VisibleForTesting
public static void setPortDelay(Integer thePortDelay) {
if (thePortDelay == null) {
thePortDelay = 500;
} else {
ourPortDelay = thePortDelay;
}
}
private static boolean isAvailable(int port) {
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
return false;
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
/* should not be thrown */
}
}
}
}
/** /**
* The entire purpose here is to find an available port that can then be * The entire purpose here is to find an available port that can then be
* bound for by server in a unit test without conflicting with other tests. * bound for by server in a unit test without conflicting with other tests.

View File

@ -1,5 +1,6 @@
package ca.uhn.fhir.util; package ca.uhn.fhir.util;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -39,8 +40,14 @@ public class PortUtilTest {
} }
} }
@After
public void after() {
PortUtil.setPortDelay(null);
}
@Test @Test
public void testPortsAreNotReused() throws InterruptedException { public void testPortsAreNotReused() throws InterruptedException {
PortUtil.setPortDelay(0);
List<Integer> ports = Collections.synchronizedList(new ArrayList<>()); List<Integer> ports = Collections.synchronizedList(new ArrayList<>());
List<PortUtil> portUtils = Collections.synchronizedList(new ArrayList<>()); List<PortUtil> portUtils = Collections.synchronizedList(new ArrayList<>());