Skip to content

Commit

Permalink
MEECROWAVE-342 fix junit5 test setup
Browse files Browse the repository at this point in the history
Now tests will use a 'fresh' ClassLoader for each non-mono test setup.
That way it's possible to run them in parallel.
I've also added a few tests and did split the surefire runs into 2 parts.
One for running the junit4 related tests, the other one for the junit 5 tests.
  • Loading branch information
struberg committed Oct 21, 2024
1 parent 0d24db9 commit 7eb9fee
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 43 deletions.
4 changes: 4 additions & 0 deletions meecrowave-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,10 @@
<shadedClassifierName>runner</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>false</createDependencyReducedPom>
<!--
<dependencyReducedPomLocation>${project.build.directory}/reduced-pom-bundle.xml
</dependencyReducedPomLocation>
-->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.apache.meecrowave.runner.Cli</mainClass>
Expand Down Expand Up @@ -487,6 +489,7 @@
<configuration>
<shadedClassifierName>runner-light</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>false</createDependencyReducedPom>
<dependencyReducedPomLocation>${project.build.directory}/reduced-pom-bundle-light.xml
</dependencyReducedPomLocation>
<transformers>
Expand Down Expand Up @@ -553,6 +556,7 @@
<configuration>
<shadedClassifierName>servlet</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>false</createDependencyReducedPom>
<dependencyReducedPomLocation>${project.build.directory}/reduced-pom-bundle-servlet.xml
</dependencyReducedPomLocation>
<transformers>
Expand Down
38 changes: 38 additions & 0 deletions meecrowave-junit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>all</parallel>
<threadCount>4</threadCount>
<threadCountClasses>3</threadCountClasses>
</configuration>

<executions>
<execution>
<!-- disable default execution -->
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>

<execution>
<id>mwjunit4</id>
<phase>test</phase>
<goals><goal>test</goal></goals>
<configuration>
<includes>
<inlude>org/apache/meecrowave/junit/*Test.class</inlude>
</includes>
</configuration>
</execution>

<execution>
<id>mwjunit5</id>
<phase>test</phase>
<goals><goal>test</goal></goals>
<configuration>
<includes>
<inlude>org/apache/meecrowave/junit5/*Test.class</inlude>
</includes>
</configuration>
</execution>
</executions>

<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import java.lang.reflect.InvocationTargetException;
import java.util.stream.Stream;

import org.apache.meecrowave.internal.ClassLoaderLock;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtensionContext;

abstract class BaseLifecycle {

boolean isPerClass(final ExtensionContext context) {
return context.getTestInstanceLifecycle()
.map(it -> it.equals(TestInstance.Lifecycle.PER_CLASS))
Expand All @@ -41,7 +43,7 @@ LifecyleState onInjection(final ExtensionContext context, final LifecyleState st
.orElse(state);
}

private static LifecyleState invoke(final Object test, final Class<? extends Annotation> marker) {
private LifecyleState invoke(final Object test, final Class<? extends Annotation> marker) {
Class<?> type = test.getClass();
while (type != Object.class) {
Stream.of(type.getDeclaredMethods())
Expand All @@ -63,7 +65,19 @@ private static LifecyleState invoke(final Object test, final Class<? extends Ann
return new LifecyleState(true, test);
}

static class LifecyleState {

protected void doUnlockContext(final boolean unlocked) {
if (!unlocked) {
ClassLoaderLock.LOCK.unlock();
}
}

protected void doLockContext() {
ClassLoaderLock.LOCK.lock();
}


class LifecyleState {
private final boolean injected;
private final Object instance;

Expand All @@ -76,4 +90,5 @@ void afterLastTest(final ExtensionContext context) {
invoke(instance, AfterLastTest.class);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.meecrowave.Meecrowave;
import org.apache.meecrowave.configuration.Configuration;
import org.apache.meecrowave.internal.ClassLoaderLock;
import org.apache.meecrowave.testing.Injector;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
Expand All @@ -42,6 +43,10 @@ public class MeecrowaveExtension extends BaseLifecycle

private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(MeecrowaveExtension.class.getName());

private ClassLoader meecrowaveCL;
private ClassLoader oldCl;


private final ScopesExtension scopes = new ScopesExtension() {
@Override
protected Optional<Class<? extends Annotation>[]> getScopes(final ExtensionContext context) {
Expand All @@ -52,6 +57,13 @@ protected Optional<Class<? extends Annotation>[]> getScopes(final ExtensionConte
}
};

protected ClassLoader createMwClassLoader() {
if (meecrowaveCL == null) {
meecrowaveCL = ClassLoaderLock.getUsableContainerLoader();
}
return meecrowaveCL;
}

@Override
public void beforeAll(final ExtensionContext context) {
if (isPerClass(context)) {
Expand All @@ -65,7 +77,20 @@ public void afterAll(final ExtensionContext context) {
ofNullable(store.get(LifecyleState.class, LifecyleState.class))
.ifPresent(s -> s.afterLastTest(context));
if (isPerClass(context)) {
store.get(Meecrowave.class, Meecrowave.class).close();
ClassLoader oldClTmp = null;
try {
if (meecrowaveCL != null) {
oldClTmp = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(meecrowaveCL);
}

store.get(Meecrowave.class, Meecrowave.class).close();
}
finally {
if (oldClTmp != null) {
Thread.currentThread().setContextClassLoader(oldClTmp);
}
}
}
}

Expand All @@ -74,63 +99,104 @@ public void beforeEach(final ExtensionContext context) {
if (!isPerClass(context)) {
doStart(context);
}

if (meecrowaveCL != null) {
oldCl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(meecrowaveCL);
}
}

@Override
public void afterEach(final ExtensionContext context) {
if (!isPerClass(context)) {
doRelease(context);
context.getStore(NAMESPACE).get(Meecrowave.class, Meecrowave.class).close();
ClassLoader oldClTmp = null;
try {
if (!isPerClass(context)) {
if (meecrowaveCL != null) {
oldClTmp = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(meecrowaveCL);
}
doRelease(context);
context.getStore(NAMESPACE).get(Meecrowave.class, Meecrowave.class).close();
}
}
finally {
if (oldCl != null) {
Thread.currentThread().setContextClassLoader(oldCl);
}
else if (oldClTmp != null) {
Thread.currentThread().setContextClassLoader(oldClTmp);
}
}
}

private void doStart(final ExtensionContext context) {
final Meecrowave.Builder builder = new Meecrowave.Builder();
final MeecrowaveConfig config = findConfig(context);
final String ctx;
if (config != null) {
ctx = config.context();

for (final Method method : MeecrowaveConfig.class.getMethods()) {
if (MeecrowaveConfig.class != method.getDeclaringClass()) {
continue;
}
final Thread thread = Thread.currentThread();
ClassLoader oldClTmp = thread.getContextClassLoader();
boolean unlocked = false;
doLockContext();

try {
ClassLoader newCl = createMwClassLoader();
if (newCl != null) {
thread.setContextClassLoader(newCl);
}

try {
final Object value = method.invoke(config);
final Meecrowave.Builder builder = new Meecrowave.Builder();
final MeecrowaveConfig config = findConfig(context);
final String ctx;
if (config != null) {
ctx = config.context();

final Field configField = Configuration.class.getDeclaredField(method.getName());
if (!configField.isAccessible()) {
configField.setAccessible(true);
for (final Method method : MeecrowaveConfig.class.getMethods()) {
if (MeecrowaveConfig.class != method.getDeclaringClass()) {
continue;
}

if (value != null && (!String.class.isInstance(value) || !value.toString().isEmpty())) {
try {
final Object value = method.invoke(config);

final Field configField = Configuration.class.getDeclaredField(method.getName());
if (!configField.isAccessible()) {
configField.setAccessible(true);
}
configField.set(builder, File.class == configField.getType() ? /*we use string instead */new File(value.toString()) : value);

if (value != null && (!String.class.isInstance(value) || !value.toString().isEmpty())) {
if (!configField.isAccessible()) {
configField.setAccessible(true);
}
configField.set(builder, File.class == configField.getType() ? /*we use string instead */new File(value.toString()) : value);
}
}
catch (final NoSuchFieldException nsfe) {
// ignored
}
catch (final Exception e) {
throw new IllegalStateException(e);
}
} catch (final NoSuchFieldException nsfe) {
// ignored
} catch (final Exception e) {
throw new IllegalStateException(e);
}
}

if (builder.getHttpPort() < 0) {
builder.randomHttpPort();
if (builder.getHttpPort() < 0) {
builder.randomHttpPort();
}
}
else {
ctx = "";
}
final ExtensionContext.Store store = context.getStore(NAMESPACE);
final Meecrowave meecrowave = new Meecrowave(builder);
store.put(Meecrowave.class, meecrowave);
store.put(Meecrowave.Builder.class, builder);
meecrowave.bake(ctx);

doInject(context);
store.put(LifecyleState.class, onInjection(context, null));
}
finally {
doUnlockContext(unlocked);
if (oldClTmp != null) {
thread.setContextClassLoader(oldClTmp);
}
} else {
ctx = "";
}
final ExtensionContext.Store store = context.getStore(NAMESPACE);
final Meecrowave meecrowave = new Meecrowave(builder);
store.put(Meecrowave.class, meecrowave);
store.put(Meecrowave.Builder.class, builder);
meecrowave.bake(ctx);

doInject(context);
store.put(LifecyleState.class, onInjection(context, null));
}

private MeecrowaveConfig findConfig(final ExtensionContext context) {
Expand All @@ -144,7 +210,7 @@ private MeecrowaveConfig findConfig(final ExtensionContext context) {
private void doRelease(final ExtensionContext context) {
ofNullable(context.getStore(NAMESPACE).get(CreationalContext.class, CreationalContext.class))
.ifPresent(CreationalContext::release);
scopes.beforeEach(context);
scopes.afterEach(context);
}

private void doInject(final ExtensionContext context) {
Expand Down
Loading

0 comments on commit 7eb9fee

Please sign in to comment.