diff --git a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/DataReceiverGroupTest.java b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/DataReceiverGroupTest.java index cc1df23972c3..fe161b6f2410 100644 --- a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/DataReceiverGroupTest.java +++ b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/DataReceiverGroupTest.java @@ -44,7 +44,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.DisposableBean; -import org.springframework.util.SocketUtils; +import org.springframework.test.util.TestSocketUtils; import java.util.Collections; import java.util.concurrent.CountDownLatch; @@ -243,11 +243,11 @@ private DataReceiverGroupProperties createMockProperties(boolean tcpEnable, bool when(properties.isTcpEnable()).thenReturn(tcpEnable); when(properties.getTcpBindIp()).thenReturn("0.0.0.0"); - when(properties.getTcpBindPort()).thenReturn(SocketUtils.findAvailableTcpPort(19099)); + when(properties.getTcpBindPort()).thenReturn(TestSocketUtils.findAvailableTcpPort()); when(properties.isUdpEnable()).thenReturn(udpEnable); when(properties.getUdpBindIp()).thenReturn("0.0.0.0"); - when(properties.getUdpBindPort()).thenReturn(SocketUtils.findAvailableTcpPort(29099)); + when(properties.getUdpBindPort()).thenReturn(TestSocketUtils.findAvailableTcpPort()); when(properties.getUdpReceiveBufferSize()).thenReturn(65535); when(properties.getWorkerThreadSize()).thenReturn(2); diff --git a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/NettyUdpReceiverTest.java b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/NettyUdpReceiverTest.java index e42af65340fb..aec4ecc5af26 100644 --- a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/NettyUdpReceiverTest.java +++ b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/NettyUdpReceiverTest.java @@ -29,7 +29,7 @@ import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.springframework.util.SocketUtils; +import org.springframework.test.util.TestSocketUtils; import java.io.IOException; import java.net.DatagramPacket; @@ -44,7 +44,7 @@ @Disabled public class NettyUdpReceiverTest { - public static final int PORT = SocketUtils.findAvailableUdpPort(30011); + public static final int PORT = TestSocketUtils.findAvailableTcpPort(); private final Logger logger = LogManager.getLogger(this.getClass()); private final CountDownLatch latch = new CountDownLatch(1); diff --git a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/UDPReceiverTest.java b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/UDPReceiverTest.java index 8bf0b9cda747..25b37c81cff4 100644 --- a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/UDPReceiverTest.java +++ b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/thrift/udp/UDPReceiverTest.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import org.springframework.util.SocketUtils; +import org.springframework.test.util.TestSocketUtils; import java.io.Closeable; import java.io.IOException; @@ -66,7 +66,7 @@ public void receive(DatagramSocket localSocket, PooledObject pac @BeforeEach public void setUp() throws Exception { - this.port = SocketUtils.findAvailableUdpPort(10999); + this.port = TestSocketUtils.findAvailableTcpPort(); } @Test diff --git a/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/ConnectionTest.java b/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/ConnectionTest.java index 691ec794c6e8..65cbb8465d0c 100644 --- a/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/ConnectionTest.java +++ b/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/ConnectionTest.java @@ -16,7 +16,6 @@ package com.navercorp.pinpoint.common.server.cluster.zookeeper; -import com.navercorp.pinpoint.testcase.util.SocketUtils; import org.apache.curator.test.TestingServer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -30,6 +29,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.springframework.test.util.TestSocketUtils; import java.util.concurrent.TimeUnit; @@ -55,7 +55,7 @@ private ConditionFactory awaitility() { @BeforeAll public static void setUp() throws Exception { - zookeeperPort = SocketUtils.findAvailableTcpPort(); + zookeeperPort = TestSocketUtils.findAvailableTcpPort(); ts = createTestingServer(); } diff --git a/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/CuratorZookeeperClientTest.java b/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/CuratorZookeeperClientTest.java index a39dc7e9e276..438714f7554e 100644 --- a/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/CuratorZookeeperClientTest.java +++ b/commons-server-cluster/src/test/java/com/navercorp/pinpoint/common/server/cluster/zookeeper/CuratorZookeeperClientTest.java @@ -18,7 +18,6 @@ import com.navercorp.pinpoint.common.server.cluster.zookeeper.exception.BadOperationException; import com.navercorp.pinpoint.common.server.cluster.zookeeper.exception.PinpointZookeeperException; -import com.navercorp.pinpoint.testcase.util.SocketUtils; import org.apache.curator.test.TestingServer; import org.apache.curator.utils.ZKPaths; import org.apache.logging.log4j.LogManager; @@ -37,6 +36,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.test.util.TestSocketUtils; import java.io.IOException; import java.util.List; @@ -68,7 +68,7 @@ private ConditionFactory awaitility() { @BeforeAll public static void setUpClass() throws Exception { - int availablePort = SocketUtils.findAvailableTcpPort(); + int availablePort = TestSocketUtils.findAvailableTcpPort(); ts = new TestingServer(availablePort); eventHoldingZookeeperEventWatcher = new EventHoldingZookeeperEventWatcher(); diff --git a/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java b/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java index 17ffd81cbafc..0a0228113fe3 100644 --- a/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java +++ b/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java @@ -17,7 +17,7 @@ package com.navercorp.pinpoint.plugin.grpc; import com.navercorp.pinpoint.common.util.CpuUtils; -import com.navercorp.pinpoint.pluginit.utils.SocketUtils; +import com.navercorp.pinpoint.testcase.util.SocketUtils; import io.grpc.Server; import io.grpc.examples.helloworld.GreeterGrpc; import io.grpc.examples.helloworld.HelloReply; diff --git a/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java b/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java index 26e8640263ec..2b7a9bc1af21 100644 --- a/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java +++ b/plugins-it/google-grpc-it/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java @@ -17,8 +17,8 @@ package com.navercorp.pinpoint.plugin.grpc; import com.navercorp.pinpoint.common.util.CpuUtils; -import com.navercorp.pinpoint.pluginit.utils.SocketUtils; +import com.navercorp.pinpoint.testcase.util.SocketUtils; import io.grpc.Server; import io.grpc.Status; import io.grpc.examples.manualflowcontrol.HelloReply; diff --git a/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java b/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java index 17ffd81cbafc..0a0228113fe3 100644 --- a/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java +++ b/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldSimpleServer.java @@ -17,7 +17,7 @@ package com.navercorp.pinpoint.plugin.grpc; import com.navercorp.pinpoint.common.util.CpuUtils; -import com.navercorp.pinpoint.pluginit.utils.SocketUtils; +import com.navercorp.pinpoint.testcase.util.SocketUtils; import io.grpc.Server; import io.grpc.examples.helloworld.GreeterGrpc; import io.grpc.examples.helloworld.HelloReply; diff --git a/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java b/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java index 70460080008a..4ec7444aaec1 100644 --- a/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java +++ b/plugins-it/google-grpc-it2/src/test/java/com/navercorp/pinpoint/plugin/grpc/HelloWorldStreamServer.java @@ -17,7 +17,7 @@ package com.navercorp.pinpoint.plugin.grpc; import com.navercorp.pinpoint.common.util.CpuUtils; -import com.navercorp.pinpoint.pluginit.utils.SocketUtils; +import com.navercorp.pinpoint.testcase.util.SocketUtils; import io.grpc.Server; import io.grpc.Status; import io.grpc.examples.manualflowcontrol.HelloReply; diff --git a/plugins-it/plugins-it-utils/pom.xml b/plugins-it/plugins-it-utils/pom.xml index 79ff1dab48ea..41c113e19064 100644 --- a/plugins-it/plugins-it-utils/pom.xml +++ b/plugins-it/plugins-it-utils/pom.xml @@ -36,6 +36,11 @@ com.navercorp.pinpoint pinpoint-commons + + com.navercorp.pinpoint + pinpoint-testcase + compile + org.nanohttpd nanohttpd diff --git a/plugins-it/plugins-it-utils/src/main/java/com/navercorp/pinpoint/pluginit/utils/SocketUtils.java b/plugins-it/plugins-it-utils/src/main/java/com/navercorp/pinpoint/pluginit/utils/SocketUtils.java deleted file mode 100644 index a30c5b743971..000000000000 --- a/plugins-it/plugins-it-utils/src/main/java/com/navercorp/pinpoint/pluginit/utils/SocketUtils.java +++ /dev/null @@ -1,311 +0,0 @@ -/* - * Copyright 2018 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.pluginit.utils; - - import java.net.DatagramSocket; - import java.net.ServerSocket; - import java.util.Random; - import java.util.SortedSet; - import java.util.TreeSet; - import javax.net.ServerSocketFactory; - -/** - * copy spring framework - * - https://github.com/spring-projects/spring-framework - * - * Simple utility methods for working with network sockets — for example, - * for finding available ports on {@code localhost}. - * - *

Within this class, a TCP port refers to a port for a {@link ServerSocket}; - * whereas, a UDP port refers to a port for a {@link DatagramSocket}. - * - * @author Sam Brannen - * @author Ben Hale - * @author Arjen Poutsma - * @author Gunnar Hillert - * @since 4.0 - */ -public class SocketUtils { - - /** - * The default minimum value for port ranges used when finding an available - * socket port. - */ - public static final int PORT_RANGE_MIN = 1024; - - /** - * The default maximum value for port ranges used when finding an available - * socket port. - */ - public static final int PORT_RANGE_MAX = 65535; - - - private static final Random random = new Random(System.currentTimeMillis()); - - - /** - * Although {@code SocketUtils} consists solely of static utility methods, - * this constructor is intentionally {@code public}. - *

Rationale

- *

Static methods from this class may be invoked from within XML - * configuration files using the Spring Expression Language (SpEL) and the - * following syntax. - *

<bean id="bean1" ... p:port="#{T(org.springframework.util.SocketUtils).findAvailableTcpPort(12000)}" />
- * If this constructor were {@code private}, you would be required to supply - * the fully qualified class name to SpEL's {@code T()} function for each usage. - * Thus, the fact that this constructor is {@code public} allows you to reduce - * boilerplate configuration with SpEL as can be seen in the following example. - *
<bean id="socketUtils" class="org.springframework.util.SocketUtils" />
-     * <bean id="bean1" ... p:port="#{socketUtils.findAvailableTcpPort(12000)}" />
-     * <bean id="bean2" ... p:port="#{socketUtils.findAvailableTcpPort(30000)}" />
- */ - public SocketUtils() { - /* no-op */ - } - - - /** - * 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 - * [{@code minPort}, {@code maxPort}]. - * @param minPort the minimum port number - * @param maxPort the maximum port number - * @return an available TCP port number - * @throws IllegalStateException if no available port could be found - */ - public static int findAvailableTcpPort(int minPort, int maxPort) { - return SocketType.TCP.findAvailablePort(minPort, maxPort); - } - - /** - * Find the requested number of available TCP ports, each randomly selected - * from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. - * @param numRequested the number of available ports to find - * @return a sorted set of available TCP port numbers - * @throws IllegalStateException if the requested number of available ports could not be found - */ - public static SortedSet findAvailableTcpPorts(int numRequested) { - return findAvailableTcpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX); - } - - /** - * Find the requested number of available TCP ports, each randomly selected - * from the range [{@code minPort}, {@code maxPort}]. - * @param numRequested the number of available ports to find - * @param minPort the minimum port number - * @param maxPort the maximum port number - * @return a sorted set of available TCP port numbers - * @throws IllegalStateException if the requested number of available ports could not be found - */ - public static SortedSet findAvailableTcpPorts(int numRequested, int minPort, int maxPort) { - return SocketType.TCP.findAvailablePorts(numRequested, minPort, maxPort); - } - - /** - * Find an available UDP port randomly selected from the range - * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. - * @return an available UDP port number - * @throws IllegalStateException if no available port could be found - */ - public static int findAvailableUdpPort() { - return findAvailableUdpPort(PORT_RANGE_MIN); - } - - /** - * Find an available UDP port randomly selected from the range - * [{@code minPort}, {@value #PORT_RANGE_MAX}]. - * @param minPort the minimum port number - * @return an available UDP port number - * @throws IllegalStateException if no available port could be found - */ - public static int findAvailableUdpPort(int minPort) { - return findAvailableUdpPort(minPort, PORT_RANGE_MAX); - } - - /** - * Find an available UDP port randomly selected from the range - * [{@code minPort}, {@code maxPort}]. - * @param minPort the minimum port number - * @param maxPort the maximum port number - * @return an available UDP port number - * @throws IllegalStateException if no available port could be found - */ - public static int findAvailableUdpPort(int minPort, int maxPort) { - return SocketType.UDP.findAvailablePort(minPort, maxPort); - } - - /** - * Find the requested number of available UDP ports, each randomly selected - * from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. - * @param numRequested the number of available ports to find - * @return a sorted set of available UDP port numbers - * @throws IllegalStateException if the requested number of available ports could not be found - */ - public static SortedSet findAvailableUdpPorts(int numRequested) { - return findAvailableUdpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX); - } - - /** - * Find the requested number of available UDP ports, each randomly selected - * from the range [{@code minPort}, {@code maxPort}]. - * @param numRequested the number of available ports to find - * @param minPort the minimum port number - * @param maxPort the maximum port number - * @return a sorted set of available UDP port numbers - * @throws IllegalStateException if the requested number of available ports could not be found - */ - public static SortedSet findAvailableUdpPorts(int numRequested, int minPort, int maxPort) { - return SocketType.UDP.findAvailablePorts(numRequested, minPort, maxPort); - } - - - private enum SocketType { - - TCP { - @Override - protected boolean isPortAvailable(int port) { - try { - ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port); - serverSocket.close(); - return true; - } - catch (Exception ex) { - return false; - } - } - }, - - UDP { - @Override - protected boolean isPortAvailable(int port) { - try { - DatagramSocket socket = new DatagramSocket(port); - socket.close(); - return true; - } - catch (Exception ex) { - return false; - } - } - }; - - /** - * Determine if the specified port for this {@code SocketType} is - * currently available on {@code localhost}. - */ - protected abstract boolean isPortAvailable(int port); - - /** - * Find a pseudo-random port number within the range - * [{@code minPort}, {@code maxPort}]. - * @param minPort the minimum port number - * @param maxPort the maximum port number - * @return a random port number within the specified range - */ - private int findRandomPort(int minPort, int maxPort) { - int portRange = maxPort - minPort; - return minPort + random.nextInt(portRange); - } - - /** - * Find an available port for this {@code SocketType}, randomly selected - * from the range [{@code minPort}, {@code maxPort}]. - * @param minPort the minimum port number - * @param maxPort the maximum port number - * @return an available port number for this socket type - * @throws IllegalStateException if no available port could be found - */ - int findAvailablePort(int minPort, int maxPort) { - isTrue(minPort > 0, "'minPort' must be greater than 0"); - isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'"); - isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX); - - int portRange = maxPort - minPort; - int candidatePort; - int searchCounter = 0; - do { - if (++searchCounter > portRange) { - throw new IllegalStateException(String.format( - "Could not find an available %s port in the range [%d, %d] after %d attempts", name(), minPort, - maxPort, searchCounter)); - } - candidatePort = findRandomPort(minPort, maxPort); - } - while (!isPortAvailable(candidatePort)); - - return candidatePort; - } - - /** - * Find the requested number of available ports for this {@code SocketType}, - * each randomly selected from the range [{@code minPort}, {@code maxPort}]. - * @param numRequested the number of available ports to find - * @param minPort the minimum port number - * @param maxPort the maximum port number - * @return a sorted set of available port numbers for this socket type - * @throws IllegalStateException if the requested number of available ports could not be found - */ - SortedSet findAvailablePorts(int numRequested, int minPort, int maxPort) { - isTrue(minPort > 0, "'minPort' must be greater than 0"); - isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'"); - isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX); - isTrue(numRequested > 0, "'numRequested' must be greater than 0"); - isTrue((maxPort - minPort) >= numRequested, - "'numRequested' must not be greater than 'maxPort' - 'minPort'"); - - final SortedSet availablePorts = new TreeSet<>(); - int attemptCount = 0; - while ((++attemptCount <= numRequested + 100) && (availablePorts.size() < numRequested)) { - availablePorts.add(findAvailablePort(minPort, maxPort)); - } - - if (availablePorts.size() != numRequested) { - throw new IllegalStateException(String.format( - "Could not find %d available %s ports in the range [%d, %d]", numRequested, name(), minPort, - maxPort)); - } - - return availablePorts; - } - } - - private static void isTrue(boolean expression, String message) { - if (!expression) { - throw new IllegalArgumentException(message); - } - } -} diff --git a/plugins-it/plugins-it-utils/src/main/java/com/navercorp/pinpoint/pluginit/utils/WebServer.java b/plugins-it/plugins-it-utils/src/main/java/com/navercorp/pinpoint/pluginit/utils/WebServer.java index 2c1530be58f2..870c8302aa6a 100644 --- a/plugins-it/plugins-it-utils/src/main/java/com/navercorp/pinpoint/pluginit/utils/WebServer.java +++ b/plugins-it/plugins-it-utils/src/main/java/com/navercorp/pinpoint/pluginit/utils/WebServer.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.pluginit.utils; +import com.navercorp.pinpoint.testcase.util.SocketUtils; import fi.iki.elonen.NanoHTTPD; import java.io.IOException; diff --git a/plugins-it/plugins-it-utils/src/test/java/com/navercorp/pinpoint/pluginit/utils/SocketUtilsTest.java b/plugins-it/plugins-it-utils/src/test/java/com/navercorp/pinpoint/pluginit/utils/SocketUtilsTest.java deleted file mode 100644 index eaf82a65c849..000000000000 --- a/plugins-it/plugins-it-utils/src/test/java/com/navercorp/pinpoint/pluginit/utils/SocketUtilsTest.java +++ /dev/null @@ -1,254 +0,0 @@ -/* - * Copyright 2020 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.pluginit.utils; - -import org.junit.Test; - -import java.util.SortedSet; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; - -/** - * @author WonChul Heo(heowc) - */ -public class SocketUtilsTest { - - @Test - public void testSuccessfulFindAvailableTcpPort() { - assertThat(SocketUtils.findAvailableTcpPort()) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - assertThat(SocketUtils.findAvailableTcpPort(SocketUtils.PORT_RANGE_MIN + 1)) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN + 1) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - assertThat(SocketUtils.findAvailableTcpPort(SocketUtils.PORT_RANGE_MIN, SocketUtils.PORT_RANGE_MAX - 1)) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX - 1); - assertThat(SocketUtils.findAvailableTcpPort(1, SocketUtils.PORT_RANGE_MAX)) - .isGreaterThan(1) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - } - - @Test - public void testFailureFindAvailableTcpPort() { - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPort(0, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'minPort' must be greater than 0"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPort(SocketUtils.PORT_RANGE_MAX, SocketUtils.PORT_RANGE_MIN); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPort(SocketUtils.PORT_RANGE_MAX, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPort(SocketUtils.PORT_RANGE_MIN, SocketUtils.PORT_RANGE_MAX + 1); - } - }, IllegalArgumentException.class, "'maxPort' must be less than or equal to " + SocketUtils.PORT_RANGE_MAX); - } - - @Test - public void testSuccessfulFindAvailableTcpPorts() { - SortedSet availableTcpPorts = SocketUtils.findAvailableTcpPorts(1); - assertThat(availableTcpPorts).hasSize(1); - for (Integer port : availableTcpPorts) { - assertThat(port) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - } - - availableTcpPorts = SocketUtils.findAvailableTcpPorts(2); - assertThat(availableTcpPorts).hasSize(2); - for (Integer port : availableTcpPorts) { - assertThat(port) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - } - - availableTcpPorts = SocketUtils.findAvailableTcpPorts(3, SocketUtils.PORT_RANGE_MIN + 1, SocketUtils.PORT_RANGE_MAX - 1); - assertThat(availableTcpPorts).hasSize(3); - for (Integer port : availableTcpPorts) { - assertThat(port) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN + 1) - .isLessThan(SocketUtils.PORT_RANGE_MAX - 1); - } - } - - @Test - public void testFailureFindAvailableTcpPorts() { - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPorts(0); - } - }, IllegalArgumentException.class, "'numRequested' must be greater than 0"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPorts(-1); - } - }, IllegalArgumentException.class, "'numRequested' must be greater than 0"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPorts(1, SocketUtils.PORT_RANGE_MAX, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPorts(1, SocketUtils.PORT_RANGE_MAX + 1, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableTcpPorts(2, SocketUtils.PORT_RANGE_MAX - 1, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'numRequested' must not be greater than 'maxPort' - 'minPort'"); - } - - @Test - public void testSuccessfulFindAvailableUdpPort() { - assertThat(SocketUtils.findAvailableUdpPort()) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - assertThat(SocketUtils.findAvailableUdpPort(SocketUtils.PORT_RANGE_MIN + 1)) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN + 1) - .isLessThan(SocketUtils.PORT_RANGE_MAX - 1); - assertThat(SocketUtils.findAvailableUdpPort(SocketUtils.PORT_RANGE_MIN, SocketUtils.PORT_RANGE_MAX - 1)) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX - 1); - assertThat(SocketUtils.findAvailableUdpPort(1, SocketUtils.PORT_RANGE_MAX)) - .isGreaterThan(1) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - } - - @Test - public void testFailureFindAvailableUdpPort() { - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPort(0, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'minPort' must be greater than 0"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPort(SocketUtils.PORT_RANGE_MAX, SocketUtils.PORT_RANGE_MIN); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPort(SocketUtils.PORT_RANGE_MAX, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPort(SocketUtils.PORT_RANGE_MIN, SocketUtils.PORT_RANGE_MAX + 1); - } - }, IllegalArgumentException.class, "'maxPort' must be less than or equal to " + SocketUtils.PORT_RANGE_MAX); - } - - @Test - public void testSuccessfulFindAvailableUdpPorts() { - SortedSet availableTcpPorts = SocketUtils.findAvailableUdpPorts(1); - assertThat(availableTcpPorts).hasSize(1); - for (Integer port : availableTcpPorts) { - assertThat(port) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - } - - availableTcpPorts = SocketUtils.findAvailableUdpPorts(2); - assertThat(availableTcpPorts).hasSize(2); - for (Integer port : availableTcpPorts) { - assertThat(port) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - } - - availableTcpPorts = SocketUtils.findAvailableUdpPorts(3, SocketUtils.PORT_RANGE_MIN + 1, SocketUtils.PORT_RANGE_MAX - 1); - assertThat(availableTcpPorts).hasSize(3); - for (Integer port : availableTcpPorts) { - assertThat(port) - .isGreaterThan(SocketUtils.PORT_RANGE_MIN) - .isLessThan(SocketUtils.PORT_RANGE_MAX); - } - } - - @Test - public void testFailureFindAvailableUdpPorts() { - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPorts(0); - } - }, IllegalArgumentException.class, "'numRequested' must be greater than 0"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPorts(-1); - } - }, IllegalArgumentException.class, "'numRequested' must be greater than 0"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPorts(1, SocketUtils.PORT_RANGE_MAX, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPorts(1, SocketUtils.PORT_RANGE_MAX + 1, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'maxPort' must be greater than 'minPort'"); - assertThrows(new Runnable() { - @Override - public void run() { - SocketUtils.findAvailableUdpPorts(2, SocketUtils.PORT_RANGE_MAX - 1, SocketUtils.PORT_RANGE_MAX); - } - }, IllegalArgumentException.class, "'numRequested' must not be greater than 'maxPort' - 'minPort'"); - } - - private static void assertThrows(Runnable runnable, Class clazz, String message) { - try { - runnable.run(); - fail(); - } catch (RuntimeException e) { - if (clazz.isInstance(e)) { - assertThat(e).isInstanceOf(clazz); - assertThat(e.getMessage()).isEqualTo(message); - } else { - throw e; - } - } - } -} diff --git a/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/RabbitMQClientITBase.java b/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/RabbitMQClientITBase.java index ebaa52bcc0f8..694f5b7cb9fc 100644 --- a/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/RabbitMQClientITBase.java +++ b/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/RabbitMQClientITBase.java @@ -19,7 +19,6 @@ import com.navercorp.pinpoint.plugin.rabbitmq.util.RabbitMQTestConstants; import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult; -import com.navercorp.pinpoint.testcase.util.SocketUtils; import com.rabbitmq.client.ConnectionFactory; import org.junit.Before; diff --git a/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/util/RabbitMQTestConstants.java b/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/util/RabbitMQTestConstants.java index 786766ebb8e3..c504ca0c6537 100644 --- a/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/util/RabbitMQTestConstants.java +++ b/plugins-it/rabbitmq-it/src/test/java/com/navercorp/pinpoint/plugin/rabbitmq/util/RabbitMQTestConstants.java @@ -16,7 +16,6 @@ package com.navercorp.pinpoint.plugin.rabbitmq.util; -import com.navercorp.pinpoint.testcase.util.SocketUtils; import com.rabbitmq.client.LongString; import com.rabbitmq.client.SaslConfig; import com.rabbitmq.client.SaslMechanism; diff --git a/web/src/test/java/com/navercorp/pinpoint/web/cluster/ClusterTest.java b/web/src/test/java/com/navercorp/pinpoint/web/cluster/ClusterTest.java index 6e8a46ce6a8c..c89074392779 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/cluster/ClusterTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/cluster/ClusterTest.java @@ -38,7 +38,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.springframework.util.SocketUtils; +import org.springframework.test.util.TestSocketUtils; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -82,7 +82,7 @@ private ConditionFactory awaitility() { @BeforeAll public static void setUp() throws Exception { - int zookeeperPort = SocketUtils.findAvailableTcpPort(28000); + int zookeeperPort = TestSocketUtils.findAvailableTcpPort(); zookeeperAddress = DEFAULT_IP + ":" + zookeeperPort; ts = createZookeeperServer(zookeeperPort); @@ -97,7 +97,7 @@ public static void setUp() throws Exception { when(properties.getCollectorZNodePath()). thenReturn(ZKPaths.makePath(ZookeeperConstants.DEFAULT_CLUSTER_ZNODE_ROOT_PATH, ZookeeperConstants.COLLECTOR_LEAF_PATH)); - acceptorPort = SocketUtils.findAvailableTcpPort(zookeeperPort); + acceptorPort = TestSocketUtils.findAvailableTcpPort(); String acceptorAddress = DEFAULT_IP + ":" + acceptorPort; when(properties.getClusterTcpPort()).thenReturn(acceptorPort);