Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run tests on a different port #869

Merged
merged 2 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import org.jboss.shamrock.deployment.builditem.ExtensionClassLoaderBuildItem;
import org.jboss.shamrock.deployment.builditem.GeneratedClassBuildItem;
import org.jboss.shamrock.deployment.builditem.GeneratedResourceBuildItem;
import org.jboss.shamrock.deployment.builditem.LaunchModeBuildItem;
import org.jboss.shamrock.deployment.builditem.ShutdownContextBuildItem;
import org.jboss.shamrock.deployment.builditem.substrate.SubstrateResourceBuildItem;
import org.jboss.shamrock.runtime.LaunchMode;

public class ShamrockAugmentor {

Expand All @@ -45,13 +47,15 @@ public class ShamrockAugmentor {
private final Path root;
private final Set<Class<? extends BuildItem>> finalResults;
private final List<Consumer<BuildChainBuilder>> buildChainCustomizers;
private final LaunchMode launchMode;

ShamrockAugmentor(Builder builder) {
this.output = builder.output;
this.classLoader = builder.classLoader;
this.root = builder.root;
this.finalResults = new HashSet<>(builder.finalResults);
this.buildChainCustomizers = new ArrayList<>(builder.buildChainCustomizers);
this.launchMode = builder.launchMode;
}

public BuildResult run() throws Exception {
Expand All @@ -72,6 +76,7 @@ public BuildResult run() throws Exception {
.addInitial(ArchiveRootBuildItem.class)
.addInitial(ShutdownContextBuildItem.class)
.addInitial(ClassOutputBuildItem.class)
.addInitial(LaunchModeBuildItem.class)
.addInitial(ExtensionClassLoaderBuildItem.class);
for (Class<? extends BuildItem> i : finalResults) {
chainBuilder.addFinal(i);
Expand All @@ -91,6 +96,7 @@ public BuildResult run() throws Exception {
.produce(new ArchiveRootBuildItem(root))
.produce(new ClassOutputBuildItem(output))
.produce(new ShutdownContextBuildItem())
.produce(new LaunchModeBuildItem(launchMode))
.produce(new ExtensionClassLoaderBuildItem(classLoader))
.execute();

Expand Down Expand Up @@ -121,6 +127,7 @@ public static final class Builder {
Path root;
Set<Class<? extends BuildItem>> finalResults = new HashSet<>();
private final List<Consumer<BuildChainBuilder>> buildChainCustomizers = new ArrayList<>();
LaunchMode launchMode = LaunchMode.NORMAL;

public Builder addBuildChainCustomizer(Consumer<BuildChainBuilder> customizer) {
this.buildChainCustomizers.add(customizer);
Expand All @@ -145,6 +152,15 @@ public Builder setOutput(ClassOutput output) {
return this;
}

public LaunchMode getLaunchMode() {
return launchMode;
}

public Builder setLaunchMode(LaunchMode launchMode) {
this.launchMode = launchMode;
return this;
}

public ClassLoader getClassLoader() {
return classLoader;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jboss.shamrock.deployment.builditem;

import org.jboss.builder.item.SimpleBuildItem;
import org.jboss.shamrock.runtime.LaunchMode;

/**
* indicates the type of launch
*/
public final class LaunchModeBuildItem extends SimpleBuildItem {

private final LaunchMode launchMode;

public LaunchModeBuildItem(LaunchMode launchMode) {
this.launchMode = launchMode;
}

public LaunchMode getLaunchMode() {
return launchMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -33,6 +34,7 @@
import org.jboss.shamrock.deployment.builditem.ApplicationClassNameBuildItem;
import org.jboss.shamrock.deployment.builditem.BytecodeTransformerBuildItem;
import org.jboss.shamrock.runtime.Application;
import org.jboss.shamrock.runtime.LaunchMode;
import org.objectweb.asm.ClassVisitor;

/**
Expand All @@ -45,19 +47,15 @@ public class RuntimeRunner implements Runnable, Closeable {
private final RuntimeClassLoader loader;
private Closeable closeTask;
private final List<Path> additionalArchives;
private final List<Consumer<BuildChainBuilder>> chainCustomizers = new ArrayList<>();


public RuntimeRunner(ClassLoader classLoader, Path target, Path frameworkClassesPath, Path transformerCache, List<Path> additionalArchives) {
this(classLoader, target, frameworkClassesPath, transformerCache, additionalArchives, Collections.emptyList());
}

public RuntimeRunner(ClassLoader classLoader, Path target, Path frameworkClassesPath, Path transformerCache, List<Path> additionalArchives, List<Consumer<BuildChainBuilder>> chainCustomizers) {
this.target = target;
this.additionalArchives = additionalArchives;
this.chainCustomizers.addAll(chainCustomizers);
RuntimeClassLoader rcl = new RuntimeClassLoader(classLoader, target, frameworkClassesPath, transformerCache);
this.loader = rcl;
private final List<Consumer<BuildChainBuilder>> chainCustomizers;
private final LaunchMode launchMode;

public RuntimeRunner(Builder builder) {
this.target = builder.target;
this.additionalArchives = new ArrayList<>(builder.additionalArchives);
this.chainCustomizers = new ArrayList<>(builder.chainCustomizers);
this.launchMode = builder.launchMode;
this.loader = new RuntimeClassLoader(builder.classLoader, target, builder.frameworkClassesPath, builder.transformerCache);
}

@Override
Expand All @@ -75,6 +73,7 @@ public void run() {
builder.setRoot(target);
builder.setClassLoader(loader);
builder.setOutput(loader);
builder.setLaunchMode(launchMode);
for (Path i : additionalArchives) {
builder.addAdditionalApplicationArchive(i);
}
Expand Down Expand Up @@ -120,4 +119,69 @@ public void close() {
throw new RuntimeException(e);
}
}

public static Builder builder() {
return new Builder();
}

public static class Builder {
private ClassLoader classLoader;
private Path target;
private Path frameworkClassesPath;
private Path transformerCache;
private LaunchMode launchMode = LaunchMode.NORMAL;
private final List<Path> additionalArchives = new ArrayList<>();
private final List<Consumer<BuildChainBuilder>> chainCustomizers = new ArrayList<>();

public Builder setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
return this;
}

public Builder setTarget(Path target) {
this.target = target;
return this;
}

public Builder setFrameworkClassesPath(Path frameworkClassesPath) {
this.frameworkClassesPath = frameworkClassesPath;
return this;
}

public Builder setTransformerCache(Path transformerCache) {
this.transformerCache = transformerCache;
return this;
}

public Builder addAdditionalArchive(Path additionalArchive) {
this.additionalArchives.add(additionalArchive);
return this;
}
public Builder addAdditionalArchives(Collection<Path> additionalArchive) {
this.additionalArchives.addAll(additionalArchives);
return this;
}

public Builder addChainCustomizer(Consumer<BuildChainBuilder> chainCustomizer) {
this.chainCustomizers.add(chainCustomizer);
return this;
}
public Builder addChainCustomizers(Collection<Consumer<BuildChainBuilder>> chainCustomizer) {
this.chainCustomizers.addAll(chainCustomizer);
return this;
}

public LaunchMode getLaunchMode() {
return launchMode;
}

public Builder setLaunchMode(LaunchMode launchMode) {
this.launchMode = launchMode;
return this;
}

public RuntimeRunner build() {
return new RuntimeRunner(this);
}
}
}
20 changes: 11 additions & 9 deletions core/devmode/src/main/java/org/jboss/shamrock/dev/DevModeMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.microprofile.config.Config;
import org.jboss.logging.Logger;
import org.jboss.shamrock.runner.RuntimeRunner;
import org.jboss.shamrock.runtime.LaunchMode;
import org.jboss.shamrock.runtime.Timing;

import io.smallrye.config.PropertiesConfigSource;
Expand Down Expand Up @@ -114,11 +112,15 @@ private static synchronized void doStart() {
//we can potentially throw away this class loader, and reload the app
try {
Thread.currentThread().setContextClassLoader(runtimeCl);
Class<?> runnerClass = runtimeCl.loadClass("org.jboss.shamrock.runner.RuntimeRunner");
Constructor ctor = runnerClass.getDeclaredConstructor(ClassLoader.class, Path.class, Path.class, Path.class, List.class);
Object runner = ctor.newInstance(runtimeCl, classesRoot.toPath(), wiringDir.toPath(), cacheDir.toPath(), new ArrayList<>());
((Runnable) runner).run();
closeable = ((Closeable) runner);
RuntimeRunner runner = RuntimeRunner.builder()
.setLaunchMode(LaunchMode.DEVELOPMENT)
.setClassLoader(runtimeCl)
.setTarget(classesRoot.toPath())
.setFrameworkClassesPath(wiringDir.toPath())
.setTransformerCache(cacheDir.toPath())
.build();
runner.run();
closeable = runner;
deploymentProblem = null;
} finally {
Thread.currentThread().setContextClassLoader(old);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jboss.shamrock.runtime;

public enum LaunchMode {
/**
* A normal production build. At the moment this can be both native image or
* JVM mode, but eventually these will likely be split
*/
NORMAL,
/**
* shamrock:dev or an IDE launch (when we support IDE launch)
*/
DEVELOPMENT,
/**
* a test run
*/
TEST
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,21 @@

package org.jboss.shamrock.runtime;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* The main entry point class, calling main allows you to bootstrap shamrock
*
* <p>
* Note that at native image generation time this is replaced by {@link org.jboss.shamrock.runtime.graal.ShamrockReplacement}
* which will avoid the need for reflection.
*
* <p>
* TODO: how do we deal with static init
*
*/
public class Shamrock {

public static void main(String... args) throws Exception {
try {
//check if the main class is on the classpath
//if so then we have a wiring jar on the classpath, which means
//we should not be using runtime mode
Class main = Class.forName("org.jboss.shamrock.runner.GeneratedMain");
Method mainMethod = main.getDeclaredMethod("main", String[].class);
mainMethod.invoke(null, (Object) args);
} catch (Exception e) {
Logger.getLogger("shamrock").log(Level.WARNING, "Could not find wiring classes, using development mode");
Runnable runnable;
try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = Shamrock.class.getClassLoader();
}
//TODO: massive hack
//but I don't know a better way to do this
//basically we want to find the classes directory, so we can process it
Path path = null;
Enumeration<URL> resources = cl.getResources("");
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
path = Paths.get(url.toURI());
break;
}
Class<?> runtimeRunner = Class.forName("org.jboss.shamrock.runtime.RuntimeRunner");
Constructor<?> ctor = runtimeRunner.getConstructor(Path.class);
runnable = (Runnable) ctor.newInstance(path);

} catch (Throwable t) {

throw new RuntimeException("Could not create Runtime runner. Are the deployment artifacts on the classpath?", t);
}
runnable.run();
}
Class main = Class.forName("org.jboss.shamrock.runner.GeneratedMain");
Method mainMethod = main.getDeclaredMethod("main", String[].class);
mainMethod.invoke(null, (Object) args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,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.ObjectSubstitution;
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 @@ -147,10 +148,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
Loading