Skip to content

Commit 561c7f7

Browse files
committed
Don't start containers imported via @ImportTestcontainers
Remove early start of containers imported via `@ImportTestcontainers` so that parallel startup can happen. Fixes gh-38831
1 parent 88429b6 commit 561c7f7

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ContainerFieldsImporter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323

2424
import org.testcontainers.containers.Container;
25-
import org.testcontainers.lifecycle.Startable;
2625

2726
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2827
import org.springframework.util.Assert;
@@ -39,9 +38,6 @@ void registerBeanDefinitions(BeanDefinitionRegistry registry, Class<?> definitio
3938
for (Field field : getContainerFields(definitionClass)) {
4039
assertValid(field);
4140
Container<?> container = getContainer(field);
42-
if (container instanceof Startable startable) {
43-
startable.start();
44-
}
4541
registerBeanDefinition(registry, field, container);
4642
}
4743
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.lifecycle;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.testcontainers.containers.PostgreSQLContainer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
24+
import org.springframework.boot.test.system.CapturedOutput;
25+
import org.springframework.boot.test.system.OutputCaptureExtension;
26+
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
27+
import org.springframework.boot.testcontainers.lifecycle.TestContainersParallelStartupWithImportTestcontainersIntegrationTests.Containers;
28+
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
29+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
30+
import org.springframework.test.annotation.DirtiesContext;
31+
import org.springframework.test.context.TestPropertySource;
32+
import org.springframework.test.context.junit.jupiter.SpringExtension;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Integration test for parallel startup.
38+
*
39+
* @author Phillip Webb
40+
*/
41+
@ExtendWith(SpringExtension.class)
42+
@TestPropertySource(properties = "spring.testcontainers.beans.startup=parallel")
43+
@DirtiesContext
44+
@DisabledIfDockerUnavailable
45+
@ExtendWith(OutputCaptureExtension.class)
46+
@ImportTestcontainers(Containers.class)
47+
public class TestContainersParallelStartupWithImportTestcontainersIntegrationTests {
48+
49+
@Test
50+
void startsInParallel(CapturedOutput out) {
51+
assertThat(out).contains("-lifecycle-0").contains("-lifecycle-1").contains("-lifecycle-2");
52+
}
53+
54+
static class Containers {
55+
56+
@Container
57+
static PostgreSQLContainer<?> container1 = new PostgreSQLContainer<>(DockerImageNames.postgresql());
58+
59+
@Container
60+
static PostgreSQLContainer<?> container2 = new PostgreSQLContainer<>(DockerImageNames.postgresql());
61+
62+
@Container
63+
static PostgreSQLContainer<?> container3 = new PostgreSQLContainer<>(DockerImageNames.postgresql());
64+
65+
}
66+
67+
}

0 commit comments

Comments
 (0)