Attempt to prevent BindExceptions in tests
Update tests that use `doWithBlockedPort` so that the port is obtained and bound early to ensure that something else doesn't grab it.
This commit is contained in:
parent
22e1a23d38
commit
fa18d2ccbf
|
@ -763,18 +763,14 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
|
||||||
public void portClashOfPrimaryConnectorResultsInPortInUseException()
|
public void portClashOfPrimaryConnectorResultsInPortInUseException()
|
||||||
throws IOException {
|
throws IOException {
|
||||||
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
||||||
|
doWithBlockedPort(new BlockedPortAction() {
|
||||||
final int port = SocketUtils.findAvailableTcpPort(40000);
|
|
||||||
|
|
||||||
factory.setPort(port);
|
|
||||||
|
|
||||||
this.container = factory.getEmbeddedServletContainer();
|
|
||||||
|
|
||||||
doWithBlockedPort(port, new Runnable() {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run(int port) {
|
||||||
try {
|
try {
|
||||||
|
factory.setPort(port);
|
||||||
|
AbstractEmbeddedServletContainerFactoryTests.this.container = factory
|
||||||
|
.getEmbeddedServletContainer();
|
||||||
AbstractEmbeddedServletContainerFactoryTests.this.container.start();
|
AbstractEmbeddedServletContainerFactoryTests.this.container.start();
|
||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
|
@ -784,25 +780,21 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void portClashOfSecondaryConnectorResultsInPortInUseException()
|
public void portClashOfSecondaryConnectorResultsInPortInUseException()
|
||||||
throws IOException {
|
throws IOException {
|
||||||
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
doWithBlockedPort(new BlockedPortAction() {
|
||||||
factory.setPort(SocketUtils.findAvailableTcpPort(40000));
|
|
||||||
|
|
||||||
final int port = SocketUtils.findAvailableTcpPort(40000);
|
|
||||||
|
|
||||||
addConnector(port, factory);
|
|
||||||
this.container = factory.getEmbeddedServletContainer();
|
|
||||||
|
|
||||||
doWithBlockedPort(port, new Runnable() {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run(int port) {
|
||||||
try {
|
try {
|
||||||
|
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
||||||
|
factory.setPort(SocketUtils.findAvailableTcpPort(40000));
|
||||||
|
addConnector(port, factory);
|
||||||
|
AbstractEmbeddedServletContainerFactoryTests.this.container = factory
|
||||||
|
.getEmbeddedServletContainer();
|
||||||
AbstractEmbeddedServletContainerFactoryTests.this.container.start();
|
AbstractEmbeddedServletContainerFactoryTests.this.container.start();
|
||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
|
@ -998,12 +990,19 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final void doWithBlockedPort(final int port, Runnable action)
|
protected final void doWithBlockedPort(BlockedPortAction action) throws IOException {
|
||||||
throws IOException {
|
int port = SocketUtils.findAvailableTcpPort(40000);
|
||||||
ServerSocket serverSocket = new ServerSocket();
|
ServerSocket serverSocket = new ServerSocket();
|
||||||
serverSocket.bind(new InetSocketAddress(port));
|
for (int i = 0; i < 10; i++) {
|
||||||
|
try {
|
||||||
|
serverSocket.bind(new InetSocketAddress(port));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
action.run();
|
action.run(port);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
|
@ -1051,4 +1050,10 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public interface BlockedPortAction {
|
||||||
|
|
||||||
|
void run(int port);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -316,12 +316,10 @@ public class TomcatEmbeddedServletContainerFactoryTests
|
||||||
@Test
|
@Test
|
||||||
public void primaryConnectorPortClashThrowsIllegalStateException()
|
public void primaryConnectorPortClashThrowsIllegalStateException()
|
||||||
throws InterruptedException, IOException {
|
throws InterruptedException, IOException {
|
||||||
final int port = SocketUtils.findAvailableTcpPort(40000);
|
doWithBlockedPort(new BlockedPortAction() {
|
||||||
|
|
||||||
doWithBlockedPort(port, new Runnable() {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run(int port) {
|
||||||
TomcatEmbeddedServletContainerFactory factory = getFactory();
|
TomcatEmbeddedServletContainerFactory factory = getFactory();
|
||||||
factory.setPort(port);
|
factory.setPort(port);
|
||||||
|
|
||||||
|
@ -337,7 +335,6 @@ public class TomcatEmbeddedServletContainerFactoryTests
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue