Reset `Startables` COUNTER when testing parallel startup

Closes gh-43369
This commit is contained in:
Phillip Webb 2024-12-03 18:49:59 -08:00
parent b340c855c0
commit 4265a0bcc2
3 changed files with 65 additions and 1 deletions

View File

@ -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"
}

View File

@ -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);
}
}
}

View File

@ -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 {