Skip to content

Commit

Permalink
Fixes #569 #508 run integration tests on a different port to 8080
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Feb 13, 2019
1 parent 3f5b347 commit 45e5cc3
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@
import org.jboss.shamrock.deployment.builditem.HotDeploymentConfigFileBuildItem;
import org.jboss.shamrock.deployment.builditem.HttpServerBuiltItem;
import org.jboss.shamrock.deployment.builditem.InjectionFactoryBuildItem;
import org.jboss.shamrock.deployment.builditem.LaunchModeBuildItem;
import org.jboss.shamrock.deployment.builditem.ObjectSubstitutionBuildItem;
import org.jboss.shamrock.deployment.builditem.ServiceStartBuildItem;
import org.jboss.shamrock.deployment.builditem.ShutdownContextBuildItem;
import org.jboss.shamrock.deployment.builditem.substrate.ReflectiveClassBuildItem;
import org.jboss.shamrock.deployment.builditem.substrate.SubstrateConfigBuildItem;
import org.jboss.shamrock.deployment.builditem.substrate.SubstrateResourceBuildItem;
import org.jboss.shamrock.deployment.recording.RecorderContext;
import org.jboss.shamrock.runtime.LaunchMode;
import org.jboss.shamrock.runtime.RuntimeValue;
import org.jboss.shamrock.runtime.annotations.ConfigItem;
import org.jboss.shamrock.undertow.runtime.HttpConfig;
Expand Down Expand Up @@ -144,10 +146,13 @@ public ServiceStartBuildItem boot(UndertowDeploymentTemplate template,
List<HttpHandlerWrapperBuildItem> wrappers,
ShutdownContextBuildItem shutdown,
Consumer<UndertowBuildItem> undertowProducer,
Consumer<HttpServerBuiltItem> serverProducer) throws Exception {
RuntimeValue<Undertow> ut = template.startUndertow(shutdown, servletDeploymentBuildItem.getDeployment(), config, wrappers.stream().map(HttpHandlerWrapperBuildItem::getValue).collect(Collectors.toList()));
Consumer<HttpServerBuiltItem> serverProducer,
LaunchModeBuildItem launchMode) throws Exception {
RuntimeValue<Undertow> ut = template.startUndertow(shutdown, servletDeploymentBuildItem.getDeployment(),
config, wrappers.stream().map(HttpHandlerWrapperBuildItem::getValue).collect(Collectors.toList()),
launchMode.getLaunchMode());
undertowProducer.accept(new UndertowBuildItem(ut));
serverProducer.accept(new HttpServerBuiltItem(config.host, config.port));
serverProducer.accept(new HttpServerBuiltItem(config.host, launchMode.getLaunchMode() == LaunchMode.TEST ? config.testPort : config.port));
return new ServiceStartBuildItem("undertow");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jboss.shamrock.deployment.ShamrockConfig;
import org.jboss.shamrock.deployment.devmode.HotReplacementContext;
import org.jboss.shamrock.deployment.devmode.HotReplacementSetup;
import org.jboss.shamrock.runtime.LaunchMode;
import org.jboss.shamrock.undertow.runtime.HttpConfig;
import org.jboss.shamrock.undertow.runtime.UndertowDeploymentTemplate;

Expand All @@ -33,7 +34,7 @@ public void setupHotDeployment(HotReplacementContext context) {
config.workerThreads = OptionalInt.empty();

try {
UndertowDeploymentTemplate.startUndertowEagerly(config, wrapper);
UndertowDeploymentTemplate.startUndertowEagerly(config, wrapper, LaunchMode.DEVELOPMENT);
} catch (ServletException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class HttpConfig {
@ConfigItem(defaultValue = "8080")
public int port;

/**
* The HTTP port used to run tests
*/
@ConfigItem(defaultValue = "8081")
public int testPort;

/**
* The HTTP host
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jboss.shamrock.arc.runtime.BeanContainer;
import org.jboss.shamrock.runtime.InjectionFactory;
import org.jboss.shamrock.runtime.InjectionInstance;
import org.jboss.shamrock.runtime.LaunchMode;
import org.jboss.shamrock.runtime.RuntimeValue;
import org.jboss.shamrock.runtime.ShutdownContext;
import org.jboss.shamrock.runtime.annotations.Template;
Expand Down Expand Up @@ -203,9 +204,9 @@ public void addServltInitParameter(RuntimeValue<DeploymentInfo> info, String nam
info.getValue().addInitParameter(name, value);
}

public RuntimeValue<Undertow> startUndertow(ShutdownContext shutdown, Deployment deployment, HttpConfig config, List<HandlerWrapper> wrappers) throws ServletException {
public RuntimeValue<Undertow> startUndertow(ShutdownContext shutdown, Deployment deployment, HttpConfig config, List<HandlerWrapper> wrappers, LaunchMode launchMode) throws ServletException {
if (undertow == null) {
startUndertowEagerly(config, null);
startUndertowEagerly(config, null, launchMode);

//in development mode undertow is started eagerly
shutdown.addShutdownTask(new Runnable() {
Expand All @@ -232,16 +233,17 @@ public void run() {
* be no chance to use hot deployment to fix the error. In development mode we start Undertow early, so any error
* on boot can be corrected via the hot deployment handler
*/
public static void startUndertowEagerly(HttpConfig config, HandlerWrapper hotDeploymentWrapper) throws ServletException {
public static void startUndertowEagerly(HttpConfig config, HandlerWrapper hotDeploymentWrapper, LaunchMode launchMode) throws ServletException {
if (undertow == null) {
log.debugf("Starting Undertow on port %d", config.port);
int port = launchMode == LaunchMode.TEST ? config.testPort : config.port;
log.debugf("Starting Undertow on port %d", port);
HttpHandler rootHandler = new CanonicalPathHandler(ROOT_HANDLER);
if (hotDeploymentWrapper != null) {
rootHandler = hotDeploymentWrapper.wrap(rootHandler);
}

Undertow.Builder builder = Undertow.builder()
.addHttpListener(config.port, config.host)
.addHttpListener(port, config.host)
.setHandler(rootHandler);
if (config.ioThreads.isPresent()) {
builder.setIoThreads(config.ioThreads.getAsInt());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8080/rest
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8081/rest
shamrock.datasource.url=jdbc:h2:mem:nonstrict;TRACE_LEVEL_FILE=4
shamrock.datasource.driver=org.h2.Driver
shamrock.datasource.maxSize=8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8080/rest
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8081/rest
shamrock.datasource.url=jdbc:h2:mem:readwrite;TRACE_LEVEL_FILE=4
shamrock.datasource.driver=org.h2.Driver
shamrock.datasource.maxSize=8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8080/rest
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8081/rest
shamrock.datasource.url=jdbc:h2:tcp://localhost/mem:test
shamrock.datasource.driver=org.h2.Driver
shamrock.datasource.maxSize=8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8080/rest
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8081/rest
shamrock.datasource.url=jdbc:h2:tcp://localhost/mem:test
shamrock.datasource.driver=org.h2.Driver
shamrock.datasource.max-size=8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#

org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8080/rest
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8081/rest
shamrock.datasource.url=${mariadb.url}
shamrock.datasource.driver=org.mariadb.jdbc.Driver
shamrock.datasource.username=hibernate_orm_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#

org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8080/rest
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8081/rest
shamrock.datasource.url=${postgres.url}
shamrock.datasource.driver=org.postgresql.Driver
shamrock.datasource.username=hibernate_orm_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ClientResource {
@Path("/manual")
public String manual() throws Exception {
RestInterface iface = RestClientBuilder.newBuilder()
.baseUrl(new URL("http", "localhost", 8080, "/"))
.baseUrl(new URL("http", "localhost", 8081, "/"))
.build(RestInterface.class);
return iface.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#

org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8080/
org.jboss.shamrock.example.rest.RestInterface/mp-rest/url=http://localhost:8081/
# Disabled by default as it establishes external connections.
# Uncomment when you want to test SSL support.
#org.jboss.shamrock.example.rest.SslRestInterface/mp-rest/url=https://www.example.com/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class FaultToleranceTestCase {

@Test
public void testRetry() throws Exception {
URL uri = new URL("http://localhost:8080/ft");
URL uri = new URL("http://localhost:8081/ft");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class OpenApiTestCase {

@Test
public void testOpenAPIJSON() throws Exception {
URL uri = new URL("http://localhost:8080/openapi");
URL uri = new URL("http://localhost:8081/openapi");
URLConnection connection = uri.openConnection();
connection.setRequestProperty("Accept", "application/json");
InputStream in = connection.getInputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ValidatorTestCase {

@Test
public void testManualValidationFailed() throws Exception {
URL uri = new URL("http://localhost:8080/validator/manual");
URL uri = new URL("http://localhost:8081/validator/manual");
URLConnection connection = uri.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
Expand All @@ -60,7 +60,7 @@ public void testManualValidationFailed() throws Exception {

@Test
public void testManualValidationPassed() throws Exception {
URL uri = new URL("http://localhost:8080/validator/manual");
URL uri = new URL("http://localhost:8081/validator/manual");
URLConnection connection = uri.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class WebsocketTestCase {
@Test
public void websocketTest() throws Exception {

final URI uri = new URI("http://localhost:8080/echo");
final URI uri = new URI("http://localhost:8081/echo");

LinkedBlockingDeque<String> message = new LinkedBlockingDeque<>();
Session session = ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,28 @@
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.microprofile.config.ConfigProvider;

public class NativeImageLauncher implements Closeable {

private static final long IMAGE_WAIT_TIME = 60000;

private final Class<?> testClass;
private Process shamrockProcess;
private final int port;

public NativeImageLauncher(Class<?> testClass) {
this(testClass, ConfigProvider.getConfig().getOptionalValue("shamrock.http.test-port", Integer.class).orElse(8081));
}

public NativeImageLauncher(Class<?> testClass, int port) {
this.testClass = testClass;
this.port = port;
}

public void start() throws Exception {
Expand All @@ -42,10 +54,13 @@ public void start() throws Exception {
if (path == null) {
path = guessPath(testClass);
}
List<String> args = new ArrayList<>();
args.add(path);
args.add("-Dshamrock.http.port=" + port);

System.out.println("Executing " + path);
System.out.println("Executing " + args);

shamrockProcess = Runtime.getRuntime().exec(path);
shamrockProcess = Runtime.getRuntime().exec(args.toArray(new String[args.size()]));
new Thread(new ProcessReader(shamrockProcess.getInputStream())).start();
new Thread(new ProcessReader(shamrockProcess.getErrorStream())).start();

Expand Down Expand Up @@ -89,8 +104,7 @@ private static void logGuessedPath(String guessedPath) {
System.err.println();
}

private static void waitForShamrock() {
int port = Integer.getInteger("http.port", 8080);
private void waitForShamrock() {
long bailout = System.currentTimeMillis() + IMAGE_WAIT_TIME;

while (System.currentTimeMillis() < bailout) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jboss.shamrock.test.common;

import java.lang.reflect.Field;

import org.eclipse.microprofile.config.ConfigProvider;

/**
* Utility class that sets the rest assured port to the default test port.
* <p>
* This uses reflection so as to not introduce a dependency on rest-assured
* <p>
* TODO: do we actually want this here, or should it be in a different module?
*/
public class RestAssuredPortManager {

private static final Field portField;
private static int oldPort;

static {
Field p;
try {
Class<?> restAssured = Class.forName("io.restassured.RestAssured");
p = restAssured.getField("port");
p.setAccessible(true);
} catch (Exception e) {
p = null;
}
portField = p;
}

public static void setPort() {
if (portField != null) {
try {
oldPort = (Integer) portField.get(null);
int port = ConfigProvider.getConfig().getOptionalValue("shamrock.http.test-port", Integer.class).orElse(8081);
portField.set(null, port);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

public static void clearPort() {
if (portField != null) {
try {
portField.set(null, oldPort);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
* These resources are started before the first test is run, and are closed
* at the end of the test suite. They are configured via the {@link ShamrockTestResource}
* annotation, which can be placed on any class in the test suite.
*
* These can also be loaded via a service loader mechanism, however if a service
* loader is used it should not also be annotated as this will result in it being executed
* twice
*/
public interface ShamrockTestResourceLifecycleManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.LinkedHashSet;
import java.util.ServiceLoader;
import java.util.Set;

import org.jboss.jandex.AnnotationInstance;
Expand Down Expand Up @@ -90,6 +91,10 @@ private Set<ShamrockTestResourceLifecycleManager> getTestResources(Class<?> test
}
}

for(ShamrockTestResourceLifecycleManager i : ServiceLoader.load(ShamrockTestResourceLifecycleManager.class)) {
testResourceRunners.add(i);
}

return testResourceRunners;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import org.jboss.shamrock.test.common.RestAssuredPortManager;
import org.jboss.shamrock.test.common.TestResourceManager;
import org.junit.runner.Description;
import org.junit.runner.Result;
Expand All @@ -45,6 +46,7 @@ protected AbstractShamrockRunListener(Class<?> testClass, RunNotifier runNotifie

@Override
public void testStarted(Description description) throws Exception {
RestAssuredPortManager.setPort();
if (!started) {
List<RunListener> stopListeners = new ArrayList<>();

Expand Down Expand Up @@ -87,6 +89,12 @@ public void testRunFinished(Result result) throws Exception {
}
}

@Override
public void testFinished(Description description) throws Exception {
super.testFinished(description);
RestAssuredPortManager.clearPort();
}

protected abstract void startShamrock() throws Exception;

protected abstract void stopShamrock() throws Exception;
Expand Down
Loading

0 comments on commit 45e5cc3

Please sign in to comment.