/** * Find an available TCP port randomly selected from the range * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. * @return an available TCP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableTcpPort() { return findAvailableTcpPort(PORT_RANGE_MIN); }
/** * Find an available TCP port randomly selected from the range * [{@code minPort}, {@value #PORT_RANGE_MAX}]. * @param minPort the minimum port number * @return an available TCP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableTcpPort(int minPort) { return findAvailableTcpPort(minPort, PORT_RANGE_MAX); }
/** * Find an available TCP port randomly selected from the range * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. * @return an available TCP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableTcpPort() { return findAvailableTcpPort(PORT_RANGE_MIN); }
/** * Find an available TCP port randomly selected from the range * [{@code minPort}, {@value #PORT_RANGE_MAX}]. * @param minPort the minimum port number * @return an available TCP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableTcpPort(int minPort) { return findAvailableTcpPort(minPort, PORT_RANGE_MAX); }
/** * Construct an EmbeddedZooKeeper with a random port. */ public EmbeddedZooKeeper() { clientPort = SocketUtils.findAvailableTcpPort(); }
@Test public void findAvailableTcpPortWithMinPortEqualToMaxPort() { int minMaxPort = SocketUtils.findAvailableTcpPort(); int port = SocketUtils.findAvailableTcpPort(minMaxPort, minMaxPort); assertEquals(minMaxPort, port); }
@Test public void findAvailableTcpPort() { int port = SocketUtils.findAvailableTcpPort(); assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX); }
@Test public void findAvailableTcpPortWithZeroMinPort() { exception.expect(IllegalArgumentException.class); SocketUtils.findAvailableTcpPort(0); }
@Test public void findAvailableTcpPortWithMin() { int port = SocketUtils.findAvailableTcpPort(50000); assertPortInRange(port, 50000, PORT_RANGE_MAX); }
@Test public void findAvailableTcpPortInRange() { int minPort = 20000; int maxPort = minPort + 1000; int port = SocketUtils.findAvailableTcpPort(minPort, maxPort); assertPortInRange(port, minPort, maxPort); }
@Test public void findAvailableTcpPortWithNegativeMinPort() { exception.expect(IllegalArgumentException.class); SocketUtils.findAvailableTcpPort(-500); }
@BeforeClass public static void beforeClass() { managementPort = SocketUtils.findAvailableTcpPort(); System.setProperty("test.port", String.valueOf(managementPort)); }
@Test public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception { int port = SocketUtils.findAvailableTcpPort(); ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost")); try { exception.expect(IllegalStateException.class); exception.expectMessage(startsWith("Could not find an available TCP port")); exception.expectMessage(endsWith("after 1 attempts")); // will only look for the exact port SocketUtils.findAvailableTcpPort(port, port); } finally { socket.close(); } }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { int availableServerSocket = SocketUtils.findAvailableTcpPort(5678); final Map<String, Object> sockets = new HashMap<String, Object>(); sockets.put("availableServerSocket", availableServerSocket); if (LOGGER.isInfoEnabled()) { LOGGER.info("Available Server Socket: " + availableServerSocket); } final MapPropertySource propertySource = new MapPropertySource("sockets", sockets); context.getEnvironment().getPropertySources().addLast(propertySource); super.loadBeanDefinitions(context, mergedConfig); }
@Test public void shouldRunOnSpecifiedPort() { final ArmeriaReactiveWebServerFactory factory = factory(); final int port = SocketUtils.findAvailableTcpPort(); factory.setPort(port); runEchoServer(factory, server -> { assertThat(server.getPort()).isEqualTo(port); }); }
@Before public void setup() throws Exception { logger.debug("Setting up before '" + this.testName.getMethodName() + "'"); this.port = SocketUtils.findAvailableTcpPort(61613); this.responseChannel = new ExecutorSubscribableChannel(); this.responseHandler = new TestMessageHandler(); this.responseChannel.subscribe(this.responseHandler); this.eventPublisher = new TestEventPublisher(); startActiveMqBroker(); createAndStartRelay(); }
@Before public void startWiser() { this.wiserPort = SocketUtils.findAvailableTcpPort(2500); wiser = new Wiser(); wiser.setPort(this.wiserPort); wiser.start(); LOGGER.info("Wiser was started."); }
@Test public void simpleHessianServiceExporter() throws IOException { final int port = SocketUtils.findAvailableTcpPort(); TestBean tb = new TestBean("tb"); SimpleHessianServiceExporter exporter = new SimpleHessianServiceExporter(); exporter.setService(tb); exporter.setServiceInterface(ITestBean.class); exporter.setDebug(true); exporter.prepare(); HttpServer server = HttpServer.create(new InetSocketAddress(port), -1); server.createContext("/hessian", exporter); server.start(); try { HessianClientInterceptor client = new HessianClientInterceptor(); client.setServiceUrl("http://localhost:" + port + "/hessian"); client.setServiceInterface(ITestBean.class); //client.setHessian2(true); client.prepare(); ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, client); assertEquals("tb", proxy.getName()); proxy.setName("test"); assertEquals("test", proxy.getName()); } finally { server.stop(Integer.MAX_VALUE); } }
@Before public void setUp() throws Exception { logger.debug("Setting up before '" + this.testName.getMethodName() + "'"); int port = SocketUtils.findAvailableTcpPort(61613); this.activeMQBroker = new BrokerService(); this.activeMQBroker.addConnector("stomp://127.0.0.1:" + port); this.activeMQBroker.setStartAsync(false); this.activeMQBroker.setPersistent(false); this.activeMQBroker.setUseJmx(false); this.activeMQBroker.getSystemUsage().getMemoryUsage().setLimit(1024 * 1024 * 5); this.activeMQBroker.getSystemUsage().getTempUsage().setLimit(1024 * 1024 * 5); this.activeMQBroker.start(); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.afterPropertiesSet(); this.client = new ReactorNettyTcpStompClient("127.0.0.1", port); this.client.setMessageConverter(new StringMessageConverter()); this.client.setTaskScheduler(taskScheduler); }
Assume.group(TestGroup.JMXMP); final int port = SocketUtils.findAvailableTcpPort();