Skip to content
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 @@ -16,6 +16,7 @@
import org.junit.platform.commons.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
Expand Down Expand Up @@ -83,11 +84,15 @@ public WireMockServer createWireMockServer(
context.addApplicationListener(
event -> {
if (event instanceof ContextClosedEvent) {
this.logger.info("Stopping WireMockServer with name '{}'", options.name());
newServer.stop();
this.stopWireMockServer(options.name(), newServer);
}
});

if (context.getBeanFactory() instanceof DefaultSingletonBeanRegistry singletonBeanRegistry) {
singletonBeanRegistry.registerDisposableBean(
options.name() + "-shutdown", () -> this.stopWireMockServer(options.name(), newServer));
}

if (httpEnabled) {
Arrays.stream(options.baseUrlProperties())
.filter(StringUtils::isNotBlank)
Expand Down Expand Up @@ -150,6 +155,13 @@ public WireMockServer createWireMockServer(
return newServer;
}

private void stopWireMockServer(final String name, final WireMockServer server) {
if (server.isRunning()) {
this.logger.info("Stopping WireMockServer with name '{}'", name);
server.stop();
}
}

private void configureMappings(ConfigureWireMock options, WireMockConfiguration serverOptions) {
boolean isFilesUnderDirectorySupplied = options.filesUnderDirectory().length != 0;
boolean isFilesUnderClasspathSupplied = !options.filesUnderClasspath().isEmpty();
Expand Down
58 changes: 58 additions & 0 deletions wiremock-spring-boot/src/test/java/usecases/AotProcessingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package usecases;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.aot.TestContextAotGenerator;
import org.wiremock.spring.EnableWireMock;

class AotProcessingTest {

@SpringBootTest
@EnableWireMock
static class AotProcessedTest {}

@Test
void wireMockIsStoppedWhenAotProcessingClosesContext() throws InterruptedException {
final Set<String> poolsBefore =
this.nonDaemonJettyThreads().stream()
.map(this::jettyThreadPoolName)
.collect(Collectors.toSet());

new TestContextAotGenerator(new InMemoryGeneratedFiles(), new RuntimeHints(), true)
.processAheadOfTime(Stream.of(AotProcessedTest.class));

final Set<Thread> leaked = this.leakedJettyThreads(poolsBefore);
final long deadline = System.currentTimeMillis() + 10_000;
while (!leaked.isEmpty() && System.currentTimeMillis() < deadline) {
Thread.sleep(100);
leaked.removeIf(thread -> !thread.isAlive());
}
assertThat(leaked)
.as("non-daemon Jetty threads still running after AOT processing closed the context")
.isEmpty();
}

private Set<Thread> leakedJettyThreads(final Set<String> poolsBefore) {
return this.nonDaemonJettyThreads().stream()
.filter(thread -> !poolsBefore.contains(this.jettyThreadPoolName(thread)))
.collect(Collectors.toSet());
}

private Set<Thread> nonDaemonJettyThreads() {
return Thread.getAllStackTraces().keySet().stream()
.filter(thread -> !thread.isDaemon())
.filter(thread -> thread.getName().startsWith("qtp"))
.collect(Collectors.toSet());
}

private String jettyThreadPoolName(final Thread thread) {
return thread.getName().replaceFirst("-\\d+$", "");
}
}
Loading