|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.util; |
| 18 | + |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.net.ServerSocket; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +import javax.net.ServerSocketFactory; |
| 24 | + |
| 25 | +/** |
| 26 | + * Simple utility methods for finding available ports on {@code localhost} for |
| 27 | + * use in integration testing scenarios. |
| 28 | + * |
| 29 | + * <p>This is a limited form of {@code SocketUtils} which is deprecated in Spring |
| 30 | + * Framework 5.3 and removed in Spring Framework 6.0. |
| 31 | + * |
| 32 | + * <p>{@code SocketUtils} was introduced in Spring Framework 4.0, primarily to |
| 33 | + * assist in writing integration tests which start an external server on an |
| 34 | + * available random port. However, these utilities make no guarantee about the |
| 35 | + * subsequent availability of a given port and are therefore unreliable (the reason |
| 36 | + * for deprecation and removal). |
| 37 | + * |
| 38 | + * <p>Instead of using {@code TestSocketUtils} to find an available local port for a server, |
| 39 | + * it is recommended that you rely on a server's ability to start on a random port |
| 40 | + * that it selects or is assigned by the operating system. To interact with that |
| 41 | + * server, you should query the server for the port it is currently using. |
| 42 | + * |
| 43 | + * @author Sam Brannen |
| 44 | + * @author Ben Hale |
| 45 | + * @author Arjen Poutsma |
| 46 | + * @author Gunnar Hillert |
| 47 | + * @author Gary Russell |
| 48 | + * @author Chris Bono |
| 49 | + * @since 5.3 |
| 50 | + */ |
| 51 | +public final class TestSocketUtils { |
| 52 | + |
| 53 | + /** |
| 54 | + * The minimum value for port ranges used when finding an available TCP port. |
| 55 | + */ |
| 56 | + private static final int PORT_RANGE_MIN = 1024; |
| 57 | + |
| 58 | + /** |
| 59 | + * The maximum value for port ranges used when finding an available TCP port. |
| 60 | + */ |
| 61 | + private static final int PORT_RANGE_MAX = 65535; |
| 62 | + |
| 63 | + private static final int PORT_RANGE = PORT_RANGE_MAX - PORT_RANGE_MIN; |
| 64 | + |
| 65 | + private static final int MAX_ATTEMPTS = 1_000; |
| 66 | + |
| 67 | + private static final Random random = new Random(System.nanoTime()); |
| 68 | + |
| 69 | + private TestSocketUtils() { |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Find an available TCP port randomly selected from the range [1024, 65535]. |
| 74 | + * @return an available TCP port number |
| 75 | + * @throws IllegalStateException if no available port could be found within max attempts |
| 76 | + */ |
| 77 | + public static int findAvailableTcpPort() { |
| 78 | + int candidatePort; |
| 79 | + int searchCounter = 0; |
| 80 | + do { |
| 81 | + if (searchCounter > MAX_ATTEMPTS) { |
| 82 | + throw new IllegalStateException(String.format( |
| 83 | + "Could not find an available TCP port in the range [%d, %d] after %d attempts", |
| 84 | + PORT_RANGE_MIN, PORT_RANGE_MAX, MAX_ATTEMPTS)); |
| 85 | + } |
| 86 | + candidatePort = PORT_RANGE_MIN + random.nextInt(PORT_RANGE + 1); |
| 87 | + searchCounter++; |
| 88 | + } |
| 89 | + while (!isPortAvailable(candidatePort)); |
| 90 | + |
| 91 | + return candidatePort; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Determine if the specified TCP port is currently available on {@code localhost}. |
| 96 | + */ |
| 97 | + private static boolean isPortAvailable(int port) { |
| 98 | + try { |
| 99 | + ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket( |
| 100 | + port, 1, InetAddress.getByName("localhost")); |
| 101 | + serverSocket.close(); |
| 102 | + return true; |
| 103 | + } |
| 104 | + catch (Exception ex) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments