RemoteInterpreterUtils { public static TServerSocket createTServerSocket(String portRange) throws IOException { TServerSocket tSocket = null; if (StringUtils.isBlank(portRange) || portRange.equals(":")) { try { tSocket = new TServerSocket(0); return tSocket; } catch (TTransportException e) { throw new IOException("Fail to create TServerSocket", e); } } int start = 1024; int end = 65535; String[] ports = portRange.split(":", -1); if (!ports[0].isEmpty()) { start = Integer.parseInt(ports[0]); } if (!ports[1].isEmpty()) { end = Integer.parseInt(ports[1]); } for (int i = start; i <= end; ++i) { try { tSocket = new TServerSocket(i); return tSocket; } catch (Exception e) { } } throw new IOException("No available port in the portRange: " + portRange); } private  RemoteInterpreterUtils(); static int findRandomAvailablePortOnAllLocalInterfaces(); static TServerSocket createTServerSocket(String portRange); static String findAvailableHostAddress(); static boolean checkIfRemoteEndpointAccessible(String host, int port); static String getInterpreterSettingId(String intpGrpId); static boolean isEnvString(String key);  }
@Test public void testCreateTServerSocket() throws IOException { assertTrue(RemoteInterpreterUtils.createTServerSocket(":") .getServerSocket().getLocalPort() > 0); String portRange = ":30000"; assertTrue(RemoteInterpreterUtils.createTServerSocket(portRange) .getServerSocket().getLocalPort() <= 30000); portRange = "30000:"; assertTrue(RemoteInterpreterUtils.createTServerSocket(portRange) .getServerSocket().getLocalPort() >= 30000); portRange = "30000:40000"; int port = RemoteInterpreterUtils.createTServerSocket(portRange) .getServerSocket().getLocalPort(); assertTrue(port >= 30000 && port <= 40000); }