From 78cbdd2c93e02472a0b703ec1756948dfe47275a Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 1 Nov 2012 11:14:50 -0500 Subject: [PATCH] Reserve Server Ports in integrationTests Previously the build would look up a server port dynamically, but since it closed the port immediately it may not be reserved by the time jetty started up. We now reserve the port and do not close it till just before Jetty starts. While there is still a race condition, it is much smaller window of time than it was previously. --- samples/cas/sample/cassample.gradle | 30 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/samples/cas/sample/cassample.gradle b/samples/cas/sample/cassample.gradle index 8c46fd491f..79d7fc5e1e 100644 --- a/samples/cas/sample/cassample.gradle +++ b/samples/cas/sample/cassample.gradle @@ -104,22 +104,26 @@ gradle.taskGraph.whenReady {graph -> } if(graph.hasTask(integrationTest)) { tasks.getByPath(':spring-security-samples-casserver:casServerOverlay').logLevel = 'ERROR' - jettyRunWar.additionalRuntimeJars += file("src/integration-test/resources") + jettyRunWar { + additionalRuntimeJars += file("src/integration-test/resources") + daemon = true + } - jettyRunWar.daemon = true - jettyRunWar.httpConnector.port = availablePort() - jettyRunWar.httpsConnector.port = jettyRunWar.httpConnector.confidentialPort = availablePort() - casServer.httpsConnector.port = availablePort() + [jettyRunWar.httpConnector,jettyRunWar.httpsConnector,casServer.httpsConnector]*.metaClass*.reservePort { taskToCloseSocket -> + def serverSocket = new ServerSocket(0) + delegate.metaClass.serverSocket = serverSocket + delegate.port = serverSocket.localPort + taskToCloseSocket.doFirst { + serverSocket.close() + } + } + + [jettyRunWar.httpConnector,jettyRunWar.httpsConnector]*.reservePort(jettyRunWar) + jettyRunWar.httpConnector.confidentialPort = jettyRunWar.httpsConnector.port + casServer.httpsConnector.reservePort(casServer) } } def casServer() { tasks.getByPath(':spring-security-samples-casserver:casServer') -} - -def availablePort() { - ServerSocket server = new ServerSocket(0) - int port = server.localPort - server.close() - port -} +} \ No newline at end of file