From 4265a0bcc2a71d14443126bb285555c038aae15e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 3 Dec 2024 18:49:59 -0800 Subject: [PATCH] Reset `Startables` COUNTER when testing parallel startup Closes gh-43369 --- .../spring-boot-testcontainers/build.gradle | 4 ++ .../lifecycle/ResetStartablesExtension.java | 60 +++++++++++++++++++ ...hImportTestcontainersIntegrationTests.java | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/ResetStartablesExtension.java diff --git a/spring-boot-project/spring-boot-testcontainers/build.gradle b/spring-boot-project/spring-boot-testcontainers/build.gradle index 091fcf0462c..1fa86932aac 100644 --- a/spring-boot-project/spring-boot-testcontainers/build.gradle +++ b/spring-boot-project/spring-boot-testcontainers/build.gradle @@ -96,3 +96,7 @@ dependencies { testImplementation("org.springframework.pulsar:spring-pulsar") testImplementation("org.testcontainers:junit-jupiter") } + +dockerTest { + jvmArgs += "--add-opens=java.base/java.util.concurrent=ALL-UNNAMED" +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/ResetStartablesExtension.java b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/ResetStartablesExtension.java new file mode 100644 index 00000000000..08fe0fe0eda --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/ResetStartablesExtension.java @@ -0,0 +1,60 @@ +/* + * 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 java.lang.reflect.InaccessibleObjectException; +import java.util.concurrent.atomic.AtomicLong; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.testcontainers.lifecycle.Startables; + +import org.springframework.test.util.ReflectionTestUtils; + +/** + * JUnit extension used by reset startables. + * + * @author Phillip Webb + */ +class ResetStartablesExtension implements BeforeEachCallback, AfterEachCallback { + + @Override + public void afterEach(ExtensionContext context) throws Exception { + reset(); + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + reset(); + } + + private void reset() { + try { + Object executor = ReflectionTestUtils.getField(Startables.class, "EXECUTOR"); + Object threadFactory = ReflectionTestUtils.getField(executor, "threadFactory"); + AtomicLong counter = (AtomicLong) ReflectionTestUtils.getField(threadFactory, "COUNTER"); + counter.set(0); + } + catch (InaccessibleObjectException ex) { + throw new IllegalStateException( + "Unable to reset field. Please run with '--add-opens=java.base/java.util.concurrent=ALL-UNNAMED'", + ex); + } + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/TestContainersParallelStartupWithImportTestcontainersIntegrationTests.java b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/TestContainersParallelStartupWithImportTestcontainersIntegrationTests.java index e3a9cfb57fb..adbcbb900e0 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/TestContainersParallelStartupWithImportTestcontainersIntegrationTests.java +++ b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/lifecycle/TestContainersParallelStartupWithImportTestcontainersIntegrationTests.java @@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(SpringExtension.class) @TestPropertySource(properties = "spring.testcontainers.beans.startup=parallel") @DisabledIfDockerUnavailable -@ExtendWith(OutputCaptureExtension.class) +@ExtendWith({ OutputCaptureExtension.class, ResetStartablesExtension.class }) @ImportTestcontainers(Containers.class) class TestContainersParallelStartupWithImportTestcontainersIntegrationTests {