Allow tests to be run in parallel across multiple workers

Closes gh-19876
2.4.x
Andy Wilkinson 3 years ago
parent 2462f67be4
commit 22d85e6d7b

@ -156,6 +156,7 @@ class JavaConventions {
project.getTasks().withType(Test.class, (test) -> {
test.useJUnitPlatform();
test.setMaxHeapSize("1024M");
test.setMaxParallelForks(project.getGradle().getStartParameter().getMaxWorkerCount());
project.getTasks().withType(Checkstyle.class, (checkstyle) -> test.mustRunAfter(checkstyle));
project.getTasks().withType(CheckFormat.class, (checkFormat) -> test.mustRunAfter(checkFormat));
});

@ -29,8 +29,8 @@ import org.awaitility.Awaitility;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.boot.testsupport.BuildOutput;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
@ -43,15 +43,15 @@ abstract class AbstractApplicationLauncher implements BeforeEachCallback {
private final Application application;
private final BuildOutput buildOutput;
private final File outputLocation;
private Process process;
private int httpPort;
protected AbstractApplicationLauncher(Application application, BuildOutput buildOutput) {
protected AbstractApplicationLauncher(Application application, File outputLocation) {
this.application = application;
this.buildOutput = buildOutput;
this.outputLocation = outputLocation;
}
@Override
@ -71,6 +71,7 @@ abstract class AbstractApplicationLauncher implements BeforeEachCallback {
Thread.currentThread().interrupt();
}
}
FileSystemUtils.deleteRecursively(this.outputLocation);
}
final int getHttpPort() {
@ -85,7 +86,7 @@ abstract class AbstractApplicationLauncher implements BeforeEachCallback {
private Process startApplication() throws Exception {
File workingDirectory = getWorkingDirectory();
File serverPortFile = new File(this.buildOutput.getRootLocation(), "server.port");
File serverPortFile = new File(this.outputLocation, "server.port");
serverPortFile.delete();
File archive = new File("build/spring-boot-server-tests-app/build/libs/spring-boot-server-tests-app-"
+ this.application.getContainer() + "." + this.application.getPackaging());

@ -27,7 +27,6 @@ import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.springframework.boot.testsupport.BuildOutput;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StreamUtils;
@ -43,9 +42,9 @@ class BootRunApplicationLauncher extends AbstractApplicationLauncher {
private final File exploded;
BootRunApplicationLauncher(Application application, BuildOutput buildOutput) {
super(application, buildOutput);
this.exploded = new File(buildOutput.getRootLocation(), "run");
BootRunApplicationLauncher(Application application, File outputLocation) {
super(application, outputLocation);
this.exploded = new File(outputLocation, "run");
}
@Override

@ -16,6 +16,7 @@
package org.springframework.boot.context.embedded;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
@ -26,6 +27,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;
import org.apache.http.impl.client.HttpClients;
@ -110,7 +112,8 @@ class EmbeddedServerContainerInvocationContextProvider
if (this.launcherCache.containsKey(cacheKey)) {
return this.launcherCache.get(cacheKey);
}
AbstractApplicationLauncher launcher = ReflectionUtils.newInstance(launcherClass, application, buildOutput);
AbstractApplicationLauncher launcher = ReflectionUtils.newInstance(launcherClass, application,
new File(buildOutput.getRootLocation(), "app-launcher-" + UUID.randomUUID()));
this.launcherCache.put(cacheKey, launcher);
return launcher;
}

@ -22,11 +22,9 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.function.Supplier;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.springframework.boot.testsupport.BuildOutput;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StreamUtils;
@ -38,16 +36,16 @@ import org.springframework.util.StreamUtils;
*/
class ExplodedApplicationLauncher extends AbstractApplicationLauncher {
private final Supplier<File> exploded;
private final File exploded;
ExplodedApplicationLauncher(Application application, BuildOutput buildOutput) {
super(application, buildOutput);
this.exploded = () -> new File(buildOutput.getRootLocation(), "exploded");
ExplodedApplicationLauncher(Application application, File outputLocation) {
super(application, outputLocation);
this.exploded = new File(outputLocation, "exploded");
}
@Override
protected File getWorkingDirectory() {
return this.exploded.get();
return this.exploded;
}
@Override
@ -61,8 +59,7 @@ class ExplodedApplicationLauncher extends AbstractApplicationLauncher {
: "org.springframework.boot.loader.JarLauncher");
try {
explodeArchive(archive);
return Arrays.asList("-cp", this.exploded.get().getAbsolutePath(), mainClass,
serverPortFile.getAbsolutePath());
return Arrays.asList("-cp", this.exploded.getAbsolutePath(), mainClass, serverPortFile.getAbsolutePath());
}
catch (IOException ex) {
throw new RuntimeException(ex);
@ -70,12 +67,12 @@ class ExplodedApplicationLauncher extends AbstractApplicationLauncher {
}
private void explodeArchive(File archive) throws IOException {
FileSystemUtils.deleteRecursively(this.exploded.get());
FileSystemUtils.deleteRecursively(this.exploded);
JarFile jarFile = new JarFile(archive);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
File extracted = new File(this.exploded.get(), jarEntry.getName());
File extracted = new File(this.exploded, jarEntry.getName());
if (jarEntry.isDirectory()) {
extracted.mkdirs();
}

@ -27,7 +27,6 @@ import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.springframework.boot.testsupport.BuildOutput;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StreamUtils;
@ -43,9 +42,9 @@ class IdeApplicationLauncher extends AbstractApplicationLauncher {
private final File exploded;
IdeApplicationLauncher(Application application, BuildOutput buildOutput) {
super(application, buildOutput);
this.exploded = new File(buildOutput.getRootLocation(), "the+ide application");
IdeApplicationLauncher(Application application, File outputLocation) {
super(application, outputLocation);
this.exploded = new File(outputLocation, "the+ide application");
}
@Override

@ -20,8 +20,6 @@ import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.springframework.boot.testsupport.BuildOutput;
/**
* {@link AbstractApplicationLauncher} that launches a packaged Spring Boot application
* using {@code java -jar}.
@ -30,8 +28,8 @@ import org.springframework.boot.testsupport.BuildOutput;
*/
class PackagedApplicationLauncher extends AbstractApplicationLauncher {
PackagedApplicationLauncher(Application application, BuildOutput buildOutput) {
super(application, buildOutput);
PackagedApplicationLauncher(Application application, File outputLocation) {
super(application, outputLocation);
}
@Override

Loading…
Cancel
Save