Author: andygumbrecht Date: Wed Jul 23 09:15:58 2014 New Revision: 1612805 URL: http://svn.apache.org/r1612805 Log: Fix TCK server port - The server is extracted to target and the generated port written to the server.xml On the next iteration the generated port is NOT always the same as the one if the server.xml - We just get lucky sometimes. The ServerLocal util/ServerLocal.java now used in containers checks for the server.xml, so the TCK then uses this port if found.
The RemoteServer now attempts a process waitfor and fails fast if the waitfor times out. There is no more danger of a process leak or hang until the buildbot times out. Added: tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/util/ServerLocal.java - copied unchanged from r1612803, tomee/tomee/branches/tomee-1.7.x/tck/tck-common/src/main/java/org/apache/openejb/tck/util/ServerLocal.java Modified: tomee/tomee/trunk/ (props changed) tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java tomee/tomee/trunk/tck/cdi-tomee/pom.xml tomee/tomee/trunk/tck/tck-common/pom.xml tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java Propchange: tomee/tomee/trunk/ ------------------------------------------------------------------------------ Merged /tomee/tomee/trunk:r1612616,1612637 Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java?rev=1612805&r1=1612804&r2=1612805&view=diff ============================================================================== --- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java (original) +++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java Wed Jul 23 09:15:58 2014 @@ -34,6 +34,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; /** @@ -127,18 +129,18 @@ public class RemoteServer { public void destroy() { - stop(); + final boolean stopSent = stop(); final Process p = server.get(); if (p != null) { - try { - p.waitFor(); - } catch (final Throwable t) { - t.printStackTrace(System.err); - } + if (stopSent) { + waitFor(p); + } else { + p.destroy(); } } + } public void start() { start(Collections.<String>emptyList(), START, true); @@ -338,28 +340,31 @@ public class RemoteServer { if (START.equals(cmd)) { server.set(p); } else if (STOP.equals(cmd)) { - p.waitFor(); + waitFor(p); p = server.get(); if (p != null) { - p.waitFor(); + waitFor(p); } } + System.out.println("Started server process on port: " + port); + } catch (final Exception e) { throw (RuntimeException) new OpenEJBRuntimeException("Cannot start the server. Exception: " + e.getClass().getName() + ": " + e.getMessage()).initCause(e); } - if (checkPortAvailable) { - if (debug) { + if (debug) { if (!connect(port, Integer.MAX_VALUE)) { + destroy(); throw new OpenEJBRuntimeException("Could not connect to server"); } } else { if (!connect(port, tries)) { + destroy(); throw new OpenEJBRuntimeException("Could not connect to server"); } } - } + } else { if (verbose) { System.out.println("[] FOUND STARTED SERVER"); @@ -367,6 +372,33 @@ public class RemoteServer { } } + private void waitFor(final Process p) { + final CountDownLatch latch = new CountDownLatch(1); + final Thread t = new Thread(new Runnable() { + @Override + public void run() { + try { + p.waitFor(); + } catch (final InterruptedException e) { + //Ignore + } + + latch.countDown(); + } + }, "process-waitFor"); + + t.start(); + + try { + if (!latch.await(10, TimeUnit.SECONDS)) { + killOnExit(p); + throw new RuntimeException("Timeout waiting for process"); + } + } catch (final InterruptedException e) { + killOnExit(p); + } + } + public void kill3UNIX() { // debug purpose only if (System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows")) { return; @@ -445,18 +477,21 @@ public class RemoteServer { return home; } - public void stop() { + public boolean stop() { try { - shutdown(); + shutdown(5); + return true; } catch (final Exception e) { if (verbose && !serverHasAlreadyBeenStarted) { e.printStackTrace(System.err); } } + + return false; } public void forceStop() throws Exception { - shutdown(); + shutdown(5); // check tomcat was effectively shutted down // we can have some concurrent shutdown commands (catalina shutdown hook for instance) @@ -480,7 +515,7 @@ public class RemoteServer { } // same as catalina.sh stop {@see org.apache.catalina.startup.Catalina#stopServer} - private void shutdown() throws Exception { + private void shutdown(int attempts) throws Exception { Socket socket = null; OutputStream stream = null; try { @@ -491,6 +526,13 @@ public class RemoteServer { stream.write(shutdown.charAt(i)); } stream.flush(); + } catch (final Exception e) { + if (attempts > 0) { + Thread.sleep(1000); + shutdown(--attempts); + } else { + throw e; + } } finally { IO.close(stream); if (socket != null) { @@ -505,7 +547,7 @@ public class RemoteServer { private boolean connect(final int port, int tries) { if (verbose) { - System.out.println("[] CONNECT ATTEMPT " + (this.tries - tries)); + System.out.println("[] CONNECT ATTEMPT " + (this.tries - tries) + " on port: " + port); } Socket s = null; @@ -524,7 +566,7 @@ public class RemoteServer { return false; } else { try { - Thread.sleep(2000); + Thread.sleep(1000); } catch (final Exception e2) { e.printStackTrace(); } @@ -548,14 +590,20 @@ public class RemoteServer { } public void killOnExit() { - if (!serverHasAlreadyBeenStarted && kill.contains(this.server.get())) { + final Process p = this.server.get(); + if (!serverHasAlreadyBeenStarted && kill.contains(p)) { return; } - kill.add(this.server.get()); + + killOnExit(p); + } + + private static void killOnExit(final Process p) { + kill.add(p); } - // Shutdown hook for recursive delete on tmp directories - static final List<Process> kill = new ArrayList<Process>(); + // Shutdown hook for processes + private static final List<Process> kill = new ArrayList<Process>(); static { Runtime.getRuntime().addShutdownHook(new CleanUpThread()); Modified: tomee/tomee/trunk/tck/cdi-tomee/pom.xml URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tck/cdi-tomee/pom.xml?rev=1612805&r1=1612804&r2=1612805&view=diff ============================================================================== --- tomee/tomee/trunk/tck/cdi-tomee/pom.xml (original) +++ tomee/tomee/trunk/tck/cdi-tomee/pom.xml Wed Jul 23 09:15:58 2014 @@ -15,7 +15,8 @@ See the License for the specific language governing permissions and limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>tck</artifactId> <groupId>org.apache.openejb</groupId> @@ -27,9 +28,11 @@ <name>OpenEJB :: TCK :: CDI TomEE</name> <properties> - <openejb.home>${project.build.directory}${file.separator}apache-tomee-${tomee.classifier}-${tomee.version}</openejb.home> + <openejb.home>${project.build.directory}${file.separator}apache-tomee-${tomee.classifier}-${tomee.version} + </openejb.home> <openejb.deployer.jndiname>openejb/DeployerBusinessRemote</openejb.deployer.jndiname> - <org.jboss.testharness.spi.Containers>org.apache.openejb.tck.impl.ContainersImplTomEE</org.jboss.testharness.spi.Containers> + <org.jboss.testharness.spi.Containers>org.apache.openejb.tck.impl.ContainersImplTomEE + </org.jboss.testharness.spi.Containers> <suiteXmlFile>src/test/resources/passing.xml</suiteXmlFile> </properties> @@ -240,7 +243,8 @@ <org.jboss.testharness.standalone>false</org.jboss.testharness.standalone> <org.jboss.testharness.runIntegrationTests>true</org.jboss.testharness.runIntegrationTests> <org.jboss.testharness.container.forceRestart>true</org.jboss.testharness.container.forceRestart> - <org.jboss.testharness.libraryDirectory>${project.build.directory}/dependency/lib</org.jboss.testharness.libraryDirectory> + <org.jboss.testharness.libraryDirectory>${project.build.directory}/dependency/lib + </org.jboss.testharness.libraryDirectory> <org.jboss.testharness.host>127.0.0.1:${tomee.http.port}</org.jboss.testharness.host> <!--<org.jboss.testharness.outputDirectory>target</org.jboss.testharness.outputDirectory>--> <deleteArtifacts>true</deleteArtifacts> @@ -254,7 +258,8 @@ <java.opts>-Xmx768m -XX:PermSize=64m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=64m -Xss2048k</java.opts> <openejb.home>${openejb.home}</openejb.home> <openejb.deployer.jndiname>${openejb.deployer.jndiname}</openejb.deployer.jndiname> - <org.jboss.testharness.spi.Containers>${org.jboss.testharness.spi.Containers}</org.jboss.testharness.spi.Containers> + <org.jboss.testharness.spi.Containers>${org.jboss.testharness.spi.Containers} + </org.jboss.testharness.spi.Containers> </systemPropertyVariables> </configuration> <executions> @@ -282,7 +287,8 @@ <profile> <id>webapp-deployer</id> <properties> - <org.jboss.testharness.spi.Containers>org.apache.openejb.tck.impl.FullRestartContainer</org.jboss.testharness.spi.Containers> + <org.jboss.testharness.spi.Containers>org.apache.openejb.tck.impl.FullRestartContainer + </org.jboss.testharness.spi.Containers> <!--suiteXmlFile>src/test/resources/webapps-passing.xml</suiteXmlFile--> </properties> </profile> Modified: tomee/tomee/trunk/tck/tck-common/pom.xml URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tck/tck-common/pom.xml?rev=1612805&r1=1612804&r2=1612805&view=diff ============================================================================== --- tomee/tomee/trunk/tck/tck-common/pom.xml (original) +++ tomee/tomee/trunk/tck/tck-common/pom.xml Wed Jul 23 09:15:58 2014 @@ -61,6 +61,11 @@ <artifactId>webdeployer</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.openejb</groupId> + <artifactId>tomee-util</artifactId> + <version>${tomee.version}</version> + </dependency> </dependencies> <build> <plugins> Modified: tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java?rev=1612805&r1=1612804&r2=1612805&view=diff ============================================================================== --- tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java (original) +++ tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java Wed Jul 23 09:15:58 2014 @@ -23,6 +23,7 @@ import org.apache.openejb.config.RemoteS import org.apache.openejb.config.ValidationException; import org.apache.openejb.loader.Options; import org.apache.openejb.tck.OpenEJBTCKRuntimeException; +import org.apache.openejb.tck.util.ServerLocal; import org.jboss.testharness.api.DeploymentException; import org.jboss.testharness.spi.Containers; @@ -44,7 +45,7 @@ public class ContainersImplTomEE extends private Exception exception; private AppInfo appInfo; private File currentFile = null; - private final int port = Integer.getInteger("server.http.port", 8080); + private final int port = ServerLocal.getPort(8080); private Deployer lookup() { final Options options = new Options(System.getProperties()); Modified: tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java?rev=1612805&r1=1612804&r2=1612805&view=diff ============================================================================== --- tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java (original) +++ tomee/tomee/trunk/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java Wed Jul 23 09:15:58 2014 @@ -21,6 +21,7 @@ import org.apache.openejb.client.RemoteI import org.apache.openejb.config.RemoteServer; import org.apache.openejb.loader.Options; import org.apache.openejb.tck.OpenEJBTCKRuntimeException; +import org.apache.openejb.tck.util.ServerLocal; import org.apache.tomee.catalina.facade.ExceptionManagerFacade; import org.jboss.testharness.api.DeploymentException; import org.jboss.testharness.spi.Containers; @@ -61,7 +62,7 @@ public class FullRestartContainer extend } @Override - public boolean deploy(InputStream archive, String name) throws IOException { + public boolean deploy(final InputStream archive, final String name) throws IOException { if (name.endsWith("war")) { currentFile = new File(WEBAPP_DIR, name); } else { @@ -71,17 +72,17 @@ public class FullRestartContainer extend System.out.println(currentFile); writeToFile(currentFile, archive); - String port = System.getProperty("server.http.port"); - if (port != null) { + final int port = ServerLocal.getPort(-1); + if (port > 0) { server = new RemoteServer(100, true); - server.setPortStartup(Integer.parseInt(port)); + server.setPortStartup(port); } else { throw new OpenEJBTCKRuntimeException("Please set the tomee port using the system property 'server.http.port'"); } try { server.start(); - } catch (RuntimeException e) { + } catch (final RuntimeException e) { e.printStackTrace(); throw e; } @@ -90,13 +91,13 @@ public class FullRestartContainer extend } @Override - public void undeploy(String name) throws IOException { + public void undeploy(final String name) throws IOException { if (null != server) { server.destroy(); } - File folder = new File(currentFile.getParentFile(), currentFile.getName().substring(0, currentFile.getName().length() - 4)); + final File folder = new File(currentFile.getParentFile(), currentFile.getName().substring(0, currentFile.getName().length() - 4)); if (folder.exists()) { delete(folder); } @@ -117,7 +118,7 @@ public class FullRestartContainer extend final Options options = new Options(System.getProperties()); final Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName()); - String port = System.getProperty("server.http.port"); + final String port = System.getProperty("server.http.port"); if (port != null) { System.out.println("provider url = " + "http://localhost:" + port + "/tomee/ejb"); props.put(Context.PROVIDER_URL, options.get(Context.PROVIDER_URL, "http://localhost:" + port + "/tomee/ejb")); @@ -126,9 +127,9 @@ public class FullRestartContainer extend } try { - InitialContext context = new InitialContext(props); + final InitialContext context = new InitialContext(props); return (ExceptionManagerFacade) context.lookup("openejb/ExceptionManagerFacadeBusinessRemote"); - } catch (Exception e) { + } catch (final Exception e) { throw new OpenEJBTCKRuntimeException(e); } }