Mercurial > hg > thermostat-ng > web-gateway
changeset 169:917366c00614
This patch alters the integration tests to run in the smae process as JUnit
Doing this, instead of spawning a subprocess, allows the test to pass on
Windows and Linux platforms.
Reviewed-by: sgehwolf, jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023462.html
author | Simon Tooke <stooke@redhat.com> |
---|---|
date | Thu, 01 Jun 2017 09:12:15 -0400 |
parents | 3193c04a9932 |
children | 218875206054 |
files | server/src/main/java/com/redhat/thermostat/gateway/server/Start.java tests/integration-tests/pom.xml tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java |
diffstat | 3 files changed, 97 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/server/src/main/java/com/redhat/thermostat/gateway/server/Start.java Wed Jun 07 10:24:01 2017 -0400 +++ b/server/src/main/java/com/redhat/thermostat/gateway/server/Start.java Thu Jun 01 09:12:15 2017 -0400 @@ -44,8 +44,19 @@ import com.redhat.thermostat.gateway.server.services.CoreServiceBuilder; import com.redhat.thermostat.gateway.server.services.CoreServiceBuilderFactory; import com.redhat.thermostat.gateway.server.services.CoreServiceBuilderFactory.CoreServiceType; +import org.eclipse.jetty.util.component.AbstractLifeCycle; -public class Start { +public class Start implements Runnable { + + private Server server = null; + private AbstractLifeCycle.AbstractLifeCycleListener listener = null; + + public Start() { + } + + public Start(AbstractLifeCycle.AbstractLifeCycleListener listener) { + this.listener = listener; + } public void run() { String gatewayHome = System.getProperty(GlobalConstants.GATEWAY_HOME_ENV, System.getenv(GlobalConstants.GATEWAY_HOME_ENV)); @@ -58,7 +69,10 @@ setServerConfig(serverBuilder, factory); setServiceBuilder(serverBuilder, factory); - Server server = serverBuilder.build(); + server = serverBuilder.build(); + if (listener != null) { + server.addLifeCycleListener(listener); + } try { server.start(); @@ -66,7 +80,9 @@ } catch (Exception e) { e.printStackTrace(); } + } + public void stopServer() { try { server.stop(); } catch (Exception e) {
--- a/tests/integration-tests/pom.xml Wed Jun 07 10:24:01 2017 -0400 +++ b/tests/integration-tests/pom.xml Thu Jun 01 09:12:15 2017 -0400 @@ -70,6 +70,15 @@ </executions> </plugin> <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <environmentVariables> + <THERMOSTAT_GATEWAY_HOME>${project.build.directory}/../../../distribution/target/image</THERMOSTAT_GATEWAY_HOME> + </environmentVariables> + </configuration> + </plugin> + <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration>
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java Wed Jun 07 10:24:01 2017 -0400 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java Thu Jun 01 09:12:15 2017 -0400 @@ -36,38 +36,36 @@ package com.redhat.thermostat.gateway.tests.integration; -import static org.junit.Assert.fail; - -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import java.util.concurrent.CountDownLatch; -import com.redhat.thermostat.gateway.common.util.OS; +import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; +import com.redhat.thermostat.gateway.server.Start; + import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.util.component.AbstractLifeCycle; +import org.eclipse.jetty.util.component.LifeCycle; import org.junit.AfterClass; import org.junit.BeforeClass; import com.redhat.thermostat.gateway.tests.utils.MongodTestUtil; -import com.redhat.thermostat.gateway.tests.utils.ProcessTestUtil; public class IntegrationTest { protected static HttpClient client; protected static String baseUrl = "http://127.0.0.1:30000"; protected static final MongodTestUtil mongodTestUtil = new MongodTestUtil(); - private static final Path distributionImage = Paths.get("../../distribution/target/image"); - private static final String POSIX_WEB_GATEWAY_SCRIPT = "thermostat-web-gateway.sh"; - private static final String WINDOWS_WEB_GATEWAY_SCRIPT = "thermostat-web-gateway.cmd"; + private static final Path distributionImage; - private static Process serverProcess; + static { + final String distDir = System.getProperty(GlobalConstants.GATEWAY_HOME_ENV, System.getenv(GlobalConstants.GATEWAY_HOME_ENV)); + distributionImage = distDir != null ? Paths.get(distDir) : null; + if (distributionImage == null) { + throw new RuntimeException("Environment variable THERMOSTAT_GATEWAY_HOME not defined!"); + } + } protected String resourceUrl; @@ -94,43 +92,70 @@ mongoProcess.waitFor(); } - private static void startServer() throws IOException, InterruptedException { - String commandStr = OS.IS_UNIX ? distributionImage.resolve("bin").resolve(POSIX_WEB_GATEWAY_SCRIPT).toAbsolutePath().toString() - : distributionImage.resolve("bin").resolve(WINDOWS_WEB_GATEWAY_SCRIPT).toAbsolutePath().toString(); + private static Thread serverThread = null; + private static Start serverObject = null; + + private static class StartListener extends AbstractLifeCycle.AbstractLifeCycleListener { + + private final CountDownLatch latch; + private Throwable cause = null; + private boolean failed = false; + + StartListener(CountDownLatch cdl) { + this.latch = cdl; + } - ProcessBuilder processBuilder = OS.IS_UNIX ? new ProcessBuilder().command(commandStr).inheritIO().redirectError(ProcessBuilder.Redirect.PIPE) - : new ProcessBuilder().command("cmd", "/c", commandStr).inheritIO().redirectError(ProcessBuilder.Redirect.PIPE); + @Override + public void lifeCycleStarted(LifeCycle event) { + super.lifeCycleStarted(event); + latch.countDown(); + } - serverProcess = processBuilder.start(); - final BufferedReader reader = new BufferedReader(new InputStreamReader(serverProcess.getErrorStream())); + @Override + public void lifeCycleFailure(LifeCycle event, Throwable cause) { + this.failed = true; + this.cause = cause; + this.latch.countDown(); + } - Future<Boolean> f = Executors.newFixedThreadPool(1).submit(new Callable<Boolean>() { - @Override - public Boolean call() throws Exception { - String line; - while ((line = reader.readLine()) != null && !line.contains("Server:main: Started")) { - System.out.println(line); - } - return true; + boolean hasFailed() { + return failed; + } + + Throwable getCause() { + return this.cause; + } + } + + private static void startServer() throws IOException, InterruptedException { + + if (serverThread == null) { + + final CountDownLatch contextStartedLatch = new CountDownLatch(1); + + final StartListener listener = new StartListener(contextStartedLatch); + + serverObject = new Start(listener); + serverThread = new Thread(serverObject); + serverThread.start(); + + // wait for Jetty is up and running? + contextStartedLatch.await(); + if (listener.hasFailed()) { + throw new IllegalStateException(listener.getCause()); } - }); - - try { - f.get(20L, TimeUnit.SECONDS); - } catch (ExecutionException e) { - e.printStackTrace(); - fail(); - } catch (TimeoutException e) { - fail("20 seconds elapsed: Integration test timed out waiting for Server to start"); } } private static void stopServer() throws Exception { - if (OS.IS_UNIX) { - ProcessTestUtil.killRecursively(serverProcess); - } else { - // TODO: kill children on Windows - serverProcess.destroy(); + if (serverThread != null) { + Thread st = serverThread; + synchronized (serverThread) { + serverThread = null; + serverObject.stopServer(); + st.join(); + serverObject = null; + } } }