1818import org .junit .platform .commons .support .ReflectionSupport ;
1919import org .testcontainers .DockerClientFactory ;
2020import org .testcontainers .lifecycle .Startable ;
21+ import org .testcontainers .lifecycle .Startables ;
2122import org .testcontainers .lifecycle .TestDescription ;
2223import org .testcontainers .lifecycle .TestLifecycleAware ;
2324
@@ -52,9 +53,7 @@ public void beforeAll(ExtensionContext context) {
5253 Store store = context .getStore (NAMESPACE );
5354 List <StoreAdapter > sharedContainersStoreAdapters = findSharedContainers (testClass );
5455
55- sharedContainersStoreAdapters .forEach (adapter -> {
56- store .getOrComputeIfAbsent (adapter .getKey (), k -> adapter .start ());
57- });
56+ startContainers (sharedContainersStoreAdapters , store , context );
5857
5958 List <TestLifecycleAware > lifecycleAwareContainers = sharedContainersStoreAdapters
6059 .stream ()
@@ -66,6 +65,18 @@ public void beforeAll(ExtensionContext context) {
6665 signalBeforeTestToContainers (lifecycleAwareContainers , testDescriptionFrom (context ));
6766 }
6867
68+ private void startContainers (List <StoreAdapter > storeAdapters , Store store , ExtensionContext context ) {
69+ if (storeAdapters .isEmpty ()) {
70+ return ;
71+ }
72+
73+ if (isParallelExecutionEnabled (context )) {
74+ Startables .deepStart (storeAdapters .stream ().map (storeAdapter -> storeAdapter .container )).join ();
75+ } else {
76+ storeAdapters .forEach (adapter -> store .getOrComputeIfAbsent (adapter .getKey (), k -> adapter .start ()));
77+ }
78+ }
79+
6980 @ Override
7081 public void afterAll (ExtensionContext context ) {
7182 signalAfterTestToContainersFor (SHARED_LIFECYCLE_AWARE_CONTAINERS , context );
@@ -75,18 +86,39 @@ public void afterAll(ExtensionContext context) {
7586 public void beforeEach (final ExtensionContext context ) {
7687 Store store = context .getStore (NAMESPACE );
7788
78- List <TestLifecycleAware > lifecycleAwareContainers = collectParentTestInstances (context )
89+ List <StoreAdapter > restartContainers = collectParentTestInstances (context )
7990 .parallelStream ()
8091 .flatMap (this ::findRestartContainers )
81- .peek (adapter -> store .getOrComputeIfAbsent (adapter .getKey (), k -> adapter .start ()))
82- .filter (this ::isTestLifecycleAware )
83- .map (lifecycleAwareAdapter -> (TestLifecycleAware ) lifecycleAwareAdapter .container )
8492 .collect (Collectors .toList ());
8593
94+ List <TestLifecycleAware > lifecycleAwareContainers = findTestLifecycleAwareContainers (
95+ restartContainers ,
96+ store ,
97+ context
98+ );
99+
86100 store .put (LOCAL_LIFECYCLE_AWARE_CONTAINERS , lifecycleAwareContainers );
87101 signalBeforeTestToContainers (lifecycleAwareContainers , testDescriptionFrom (context ));
88102 }
89103
104+ private List <TestLifecycleAware > findTestLifecycleAwareContainers (
105+ List <StoreAdapter > restartContainers ,
106+ Store store ,
107+ ExtensionContext context
108+ ) {
109+ startContainers (restartContainers , store , context );
110+
111+ return restartContainers
112+ .stream ()
113+ .filter (this ::isTestLifecycleAware )
114+ .map (lifecycleAwareAdapter -> (TestLifecycleAware ) lifecycleAwareAdapter .container )
115+ .collect (Collectors .toList ());
116+ }
117+
118+ private boolean isParallelExecutionEnabled (ExtensionContext context ) {
119+ return findTestcontainers (context ).map (Testcontainers ::parallel ).orElse (false );
120+ }
121+
90122 @ Override
91123 public void afterEach (ExtensionContext context ) {
92124 signalAfterTestToContainersFor (LOCAL_LIFECYCLE_AWARE_CONTAINERS , context );
0 commit comments