-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure container's started before connection details use it
Prior to this commit, a Testcontainer that was managed as a bean would not have been started in time if it was accessed before the bean factory's configuration had been frozen. A common way for this to occur is when using JPA. The entity manager factory bean is LoadTimeWeaverAware which causes it to be created before configuration is frozen. Creating this bean requires the DataSource which in turn requires the JdbcConnectionDetails and its JDBC URL. Getting the JDBC URL From the connection details requires the container hosting the SQL database to have been started. This commit updates ContainerConnectionDetails, the super-class for all Testcontainer-based ConnectionDetails implementations, to publish an event when the Container is retrieved from the details. When this event is published, TestcontainersLifecycleBeanPostProcessor initializes all containers that are defined as beans. Closes gh-40585
- Loading branch information
1 parent
cb22d57
commit 468e246
Showing
11 changed files
with
174 additions
and
8 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
.../java/org/springframework/boot/testcontainers/lifecycle/BeforeTestcontainerUsedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2012-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.testcontainers.lifecycle; | ||
|
||
import org.testcontainers.containers.Container; | ||
|
||
import org.springframework.context.ApplicationEvent; | ||
|
||
/** | ||
* Event published just before a Testcontainers {@link Container} is used. | ||
* | ||
* @author Andy Wilkinson | ||
* @since 3.2.6 | ||
*/ | ||
public class BeforeTestcontainerUsedEvent extends ApplicationEvent { | ||
|
||
public BeforeTestcontainerUsedEvent(Object source) { | ||
super(source); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...t/java/org/springframework/boot/testcontainers/LoadTimeWeaverAwareConsumerContainers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2012-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.testcontainers; | ||
|
||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
|
||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
|
||
/** | ||
* Container definitions for {@link LoadTimeWeaverAwareConsumerImportTestcontainersTests}. | ||
* | ||
* @author Andy Wilkinson | ||
*/ | ||
interface LoadTimeWeaverAwareConsumerContainers { | ||
|
||
@Container | ||
@ServiceConnection | ||
PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:16.1"); | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...ngframework/boot/testcontainers/LoadTimeWeaverAwareConsumerImportTestcontainersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2012-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.testcontainers; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.testcontainers.context.ImportTestcontainers; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.weaving.LoadTimeWeaverAware; | ||
import org.springframework.instrument.classloading.LoadTimeWeaver; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
@ImportTestcontainers(LoadTimeWeaverAwareConsumerContainers.class) | ||
public class LoadTimeWeaverAwareConsumerImportTestcontainersTests implements LoadTimeWeaverAwareConsumerContainers { | ||
|
||
@Autowired | ||
private LoadTimeWeaverAwareConsumer consumer; | ||
|
||
@Test | ||
void loadTimeWeaverAwareBeanCanUseJdbcUrlFromContainerBasedConnectionDetails() { | ||
assertThat(this.consumer.jdbcUrl).isNotNull(); | ||
} | ||
|
||
@Configuration | ||
@ImportAutoConfiguration(DataSourceAutoConfiguration.class) | ||
static class TestConfiguration { | ||
|
||
@Bean | ||
LoadTimeWeaverAwareConsumer loadTimeWeaverAwareConsumer(JdbcConnectionDetails connectionDetails) { | ||
return new LoadTimeWeaverAwareConsumer(connectionDetails); | ||
} | ||
|
||
} | ||
|
||
static class LoadTimeWeaverAwareConsumer implements LoadTimeWeaverAware { | ||
|
||
private final String jdbcUrl; | ||
|
||
LoadTimeWeaverAwareConsumer(JdbcConnectionDetails connectionDetails) { | ||
this.jdbcUrl = connectionDetails.getJdbcUrl(); | ||
} | ||
|
||
@Override | ||
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) { | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters