Skip to content

Speed up spring-boot-server-tests #25457

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

Closed
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,6 @@
import java.util.List;

import org.awaitility.Awaitility;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

Expand All @@ -40,7 +39,7 @@
*
* @author Andy Wilkinson
*/
abstract class AbstractApplicationLauncher implements BeforeEachCallback, AfterEachCallback {
abstract class AbstractApplicationLauncher implements BeforeEachCallback {

private final ApplicationBuilder applicationBuilder;

Expand All @@ -56,15 +55,16 @@ protected AbstractApplicationLauncher(ApplicationBuilder applicationBuilder, Bui
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
if (this.process != null) {
this.process.destroy();
public void beforeEach(ExtensionContext context) throws Exception {
if (this.process == null) {
this.process = startApplication();
}
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
this.process = startApplication();
void destroyProcess() {
if (this.process != null) {
this.process.destroy();
}
}

final int getHttpPort() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -62,6 +63,10 @@ class EmbeddedServerContainerInvocationContextProvider
private static final BuildOutput buildOutput = new BuildOutput(
EmbeddedServerContainerInvocationContextProvider.class);

private final Map<String, ApplicationBuilder> builderCache = new HashMap<>();

private final Map<String, AbstractApplicationLauncher> launcherCache = new HashMap<>();

private final Path tempDir;

EmbeddedServerContainerInvocationContextProvider() throws IOException {
Expand All @@ -77,14 +82,13 @@ public boolean supportsTestTemplate(ExtensionContext context) {
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
EmbeddedServletContainerTest annotation = context.getRequiredTestClass()
.getAnnotation(EmbeddedServletContainerTest.class);
return CONTAINERS.stream()
.map((container) -> new ApplicationBuilder(this.tempDir, annotation.packaging(),
container))
return CONTAINERS
.stream().map(
(container) -> getApplicationBuilder(annotation, container))
.flatMap(
(builder) -> Stream
.of(annotation.launchers()).map(
(launcherClass) -> ReflectionUtils.newInstance(launcherClass, builder,
buildOutput))
(launcherClass) -> getAbstractApplicationLauncher(builder, launcherClass))
.map((launcher) -> new EmbeddedServletContainerInvocationContext(
StringUtils.capitalize(builder.getContainer()) + ": "
+ launcher.getDescription(builder.getPackaging()),
Expand All @@ -94,6 +98,34 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
@Override
public void afterAll(ExtensionContext context) throws Exception {
FileSystemUtils.deleteRecursively(this.tempDir);
cleanupCaches();
}

private void cleanupCaches() {
this.launcherCache.values().forEach(AbstractApplicationLauncher::destroyProcess);
this.launcherCache.clear();
this.builderCache.clear();
}

private AbstractApplicationLauncher getAbstractApplicationLauncher(ApplicationBuilder builder,
Class<? extends AbstractApplicationLauncher> launcherClass) {
String cacheKey = builder.getContainer() + ":" + builder.getPackaging() + ":" + launcherClass.getName();
if (this.launcherCache.containsKey(cacheKey)) {
return this.launcherCache.get(cacheKey);
}
AbstractApplicationLauncher launcher = ReflectionUtils.newInstance(launcherClass, builder, buildOutput);
this.launcherCache.put(cacheKey, launcher);
return launcher;
}

private ApplicationBuilder getApplicationBuilder(EmbeddedServletContainerTest annotation, String container) {
String cacheKey = container + ":" + annotation.packaging();
if (this.builderCache.containsKey(cacheKey)) {
return this.builderCache.get(cacheKey);
}
ApplicationBuilder builder = new ApplicationBuilder(this.tempDir, annotation.packaging(), container);
this.builderCache.put(cacheKey, builder);
return builder;
}

static class EmbeddedServletContainerInvocationContext implements TestTemplateInvocationContext, ParameterResolver {
Expand Down