Skip to content

Commit

Permalink
Reset Startables COUNTER when testing parallel startup
Browse files Browse the repository at this point in the history
Closes gh-43369
  • Loading branch information
philwebb committed Dec 4, 2024
1 parent b340c85 commit 4265a0b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions spring-boot-project/spring-boot-testcontainers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
@ExtendWith(SpringExtension.class)
@TestPropertySource(properties = "spring.testcontainers.beans.startup=parallel")
@DisabledIfDockerUnavailable
@ExtendWith(OutputCaptureExtension.class)
@ExtendWith({ OutputCaptureExtension.class, ResetStartablesExtension.class })
@ImportTestcontainers(Containers.class)
class TestContainersParallelStartupWithImportTestcontainersIntegrationTests {

Expand Down

0 comments on commit 4265a0b

Please sign in to comment.