Don't initialize containers during AOT processing

Fixes gh-41838
This commit is contained in:
Andy Wilkinson
2024-08-14 18:55:07 +01:00
parent 1d45016d8c
commit e7e5ed1508
2 changed files with 40 additions and 1 deletions

View File

@@ -85,7 +85,7 @@ class TestcontainersLifecycleBeanPostProcessor
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (this.beanFactory.isConfigurationFrozen()) {
if (this.beanFactory.isConfigurationFrozen() && !isAotProcessingInProgress()) {
initializeContainers();
}
if (bean instanceof Startable startableBean) {
@@ -100,6 +100,10 @@ class TestcontainersLifecycleBeanPostProcessor
return bean;
}
private boolean isAotProcessingInProgress() {
return Boolean.getBoolean("spring.aot.processing");
}
private void initializeStartables(Startable startableBean, String startableBeanName) {
logger.trace(LogMessage.format("Initializing startables"));
List<String> beanNames = new ArrayList<>(getBeanNames(Startable.class));

View File

@@ -24,6 +24,7 @@ import org.testcontainers.containers.GenericContainer;
import org.testcontainers.lifecycle.Startable;
import org.testcontainers.utility.TestcontainersConfiguration;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -143,6 +144,17 @@ class TestcontainersLifecycleApplicationContextInitializerTests {
applicationContext.refresh();
}
@Test
void doesNotStartContainersWhenAotProcessingIsInProgress() {
GenericContainer<?> container = mock(GenericContainer.class);
AnnotationConfigApplicationContext applicationContext = createApplicationContext(container);
then(container).shouldHaveNoInteractions();
withSystemProperty("spring.aot.processing", "true",
() -> applicationContext.refreshForAotProcessing(new RuntimeHints()));
then(container).shouldHaveNoInteractions();
applicationContext.close();
}
@Test
void setupStartupBasedOnEnvironmentProperty() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
@@ -159,6 +171,22 @@ class TestcontainersLifecycleApplicationContextInitializerTests {
assertThat(beanPostProcessor).extracting("startup").isEqualTo(TestcontainersStartup.PARALLEL);
}
private void withSystemProperty(String name, String value, Runnable action) {
String previousValue = System.getProperty(name);
System.setProperty(name, value);
try {
action.run();
}
finally {
if (previousValue == null) {
System.clearProperty(name);
}
else {
System.setProperty(name, previousValue);
}
}
}
private AnnotationConfigApplicationContext createApplicationContext(Startable container) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
@@ -166,6 +194,13 @@ class TestcontainersLifecycleApplicationContextInitializerTests {
return applicationContext;
}
private AnnotationConfigApplicationContext createApplicationContext(GenericContainer<?> container) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
applicationContext.registerBean("container", GenericContainer.class, () -> container);
return applicationContext;
}
@Configuration
static class ReusableContainerConfiguration {