Class TestSocketUtils
java.lang.Object
org.springframework.cloud.test.TestSocketUtils
Simple utility methods for working with network sockets — for example, for
finding available ports on
localhost.
Within this class, a TCP port refers to a port for a ServerSocket; whereas, a
UDP port refers to a port for a DatagramSocket.
SocketUtils was introduced in Spring Framework 4.0, primarily to assist in
writing integration tests which start an external server on an available random port.
However, these utilities make no guarantee about the subsequent availability of a given
port and are therefore unreliable. Instead of using SocketUtils to find an
available local port for a server, it is recommended that you rely on a server's
ability to start on a random port that it selects or is assigned by the operating
system. To interact with that server, you should query the server for the port it is
currently using.
- Since:
- 4.0
- Author:
- Sam Brannen, Ben Hale, Arjen Poutsma, Gunnar Hillert, Gary Russell
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum value for port ranges used when finding an available socket port.static final intThe default minimum value for port ranges used when finding an available socket port. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intstatic intfindAvailableTcpPort(int minPort) Find an available TCP port randomly selected from the range [minPort, 65535].static intfindAvailableTcpPort(int minPort, int maxPort) Find an available TCP port randomly selected from the range [minPort,maxPort].findAvailableTcpPorts(int numRequested) findAvailableTcpPorts(int numRequested, int minPort, int maxPort) Find the requested number of available TCP ports, each randomly selected from the range [minPort,maxPort].static intstatic intfindAvailableUdpPort(int minPort) Find an available UDP port randomly selected from the range [minPort, 65535].static intfindAvailableUdpPort(int minPort, int maxPort) Find an available UDP port randomly selected from the range [minPort,maxPort].findAvailableUdpPorts(int numRequested) findAvailableUdpPorts(int numRequested, int minPort, int maxPort) Find the requested number of available UDP ports, each randomly selected from the range [minPort,maxPort].
-
Field Details
-
PORT_RANGE_MIN
public static final int PORT_RANGE_MINThe default minimum value for port ranges used when finding an available socket port.- See Also:
-
PORT_RANGE_MAX
public static final int PORT_RANGE_MAXThe default maximum value for port ranges used when finding an available socket port.- See Also:
-
-
Constructor Details
-
TestSocketUtils
public TestSocketUtils()
-
-
Method Details
-
findAvailableTcpPort
public static int findAvailableTcpPort()- Returns:
- an available TCP port number
- Throws:
IllegalStateException- if no available port could be found
-
findAvailableTcpPort
public static int findAvailableTcpPort(int minPort) Find an available TCP port randomly selected from the range [minPort, 65535].- Parameters:
minPort- the minimum port number- Returns:
- an available TCP port number
- Throws:
IllegalStateException- if no available port could be found
-
findAvailableTcpPort
public static int findAvailableTcpPort(int minPort, int maxPort) Find an available TCP port randomly selected from the range [minPort,maxPort].- Parameters:
minPort- the minimum port numbermaxPort- the maximum port number- Returns:
- an available TCP port number
- Throws:
IllegalStateException- if no available port could be found
-
findAvailableTcpPorts
Find the requested number of available TCP ports, each randomly selected from the range [1024, 65535].- Parameters:
numRequested- the number of available ports to find- Returns:
- a sorted set of available TCP port numbers
- Throws:
IllegalStateException- if the requested number of available ports could not be found
-
findAvailableTcpPorts
Find the requested number of available TCP ports, each randomly selected from the range [minPort,maxPort].- Parameters:
numRequested- the number of available ports to findminPort- the minimum port numbermaxPort- the maximum port number- Returns:
- a sorted set of available TCP port numbers
- Throws:
IllegalStateException- if the requested number of available ports could not be found
-
findAvailableUdpPort
public static int findAvailableUdpPort()- Returns:
- an available UDP port number
- Throws:
IllegalStateException- if no available port could be found
-
findAvailableUdpPort
public static int findAvailableUdpPort(int minPort) Find an available UDP port randomly selected from the range [minPort, 65535].- Parameters:
minPort- the minimum port number- Returns:
- an available UDP port number
- Throws:
IllegalStateException- if no available port could be found
-
findAvailableUdpPort
public static int findAvailableUdpPort(int minPort, int maxPort) Find an available UDP port randomly selected from the range [minPort,maxPort].- Parameters:
minPort- the minimum port numbermaxPort- the maximum port number- Returns:
- an available UDP port number
- Throws:
IllegalStateException- if no available port could be found
-
findAvailableUdpPorts
Find the requested number of available UDP ports, each randomly selected from the range [1024, 65535].- Parameters:
numRequested- the number of available ports to find- Returns:
- a sorted set of available UDP port numbers
- Throws:
IllegalStateException- if the requested number of available ports could not be found
-
findAvailableUdpPorts
Find the requested number of available UDP ports, each randomly selected from the range [minPort,maxPort].- Parameters:
numRequested- the number of available ports to findminPort- the minimum port numbermaxPort- the maximum port number- Returns:
- a sorted set of available UDP port numbers
- Throws:
IllegalStateException- if the requested number of available ports could not be found
-