Repository navigation
Allow customizing the thread factories of Testcontainers' own threads #12189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mdedetrich
wants to merge
4
commits into
testcontainers:main
Choose a base branch
from
mdedetrich:thread-factory-customizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
26d91ae
Allow customizing the thread factories of Testcontainers' own threads
mdedetrich 00b29e8
Tolerate ServiceLoader errors when loading ThreadFactoryCustomizers
mdedetrich 7976e13
Stop loading ThreadFactoryCustomizers at the first ServiceLoader error
mdedetrich 7500046
Handle provider linkage failures when loading ThreadFactoryCustomizers
mdedetrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
core/src/main/java/org/testcontainers/core/ThreadFactoryCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.testcontainers.core; | ||
|
|
||
| import java.util.concurrent.ThreadFactory; | ||
|
|
||
| /** | ||
| * Callback interface that can be used to customize the {@link ThreadFactory} of the threads Testcontainers creates | ||
| * for its own use, such as the threads that check wait strategies, start containers in parallel or keep the | ||
| * connection to Ryuk open. | ||
| * <p> | ||
| * Implementations are discovered via the Service Provider Interface (SPI) mechanism and must be registered in | ||
| * {@code META-INF/services/org.testcontainers.core.ThreadFactoryCustomizer}. | ||
| * <p> | ||
| * Most of these threads belong to pools that are shared across the JVM and that create their threads lazily, on | ||
| * whichever thread happens to need one. A customizer lets tooling that tracks state per thread, such as a test | ||
| * framework that attributes output to the test that produced it, keep such state from leaking into these shared | ||
| * threads. | ||
| */ | ||
| public interface ThreadFactoryCustomizer { | ||
| /** | ||
| * Callback to customize a {@link ThreadFactory}. | ||
| * | ||
| * @param threadFactory the factory Testcontainers would otherwise use; it names the threads and makes them daemon threads | ||
| * @return the factory to use instead, typically one that delegates to {@code threadFactory} | ||
| */ | ||
| ThreadFactory customize(ThreadFactory threadFactory); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
core/src/main/java/org/testcontainers/utility/ThreadFactoryCustomizers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.testcontainers.UnstableAPI; | ||
| import org.testcontainers.core.ThreadFactoryCustomizer; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.ServiceLoader; | ||
| import java.util.concurrent.ThreadFactory; | ||
|
|
||
| /** | ||
| * Applies the {@link ThreadFactoryCustomizer}s registered via SPI to the thread factories Testcontainers uses | ||
| * for its own threads. | ||
| */ | ||
| @UnstableAPI | ||
| @Slf4j | ||
| public final class ThreadFactoryCustomizers { | ||
|
|
||
| private ThreadFactoryCustomizers() {} | ||
|
|
||
| /** | ||
| * Returns {@code threadFactory} customized by every registered {@link ThreadFactoryCustomizer}, in the order | ||
| * they are found. A customizer that throws or returns {@code null} is skipped. | ||
| */ | ||
| public static ThreadFactory customize(ThreadFactory threadFactory) { | ||
| return customize(threadFactory, Holder.CUSTOMIZERS); | ||
| } | ||
|
|
||
| static ThreadFactory customize(ThreadFactory threadFactory, Iterable<ThreadFactoryCustomizer> customizers) { | ||
| ThreadFactory customized = threadFactory; | ||
| for (ThreadFactoryCustomizer customizer : customizers) { | ||
| try { | ||
| ThreadFactory next = customizer.customize(customized); | ||
| if (next != null) { | ||
| customized = next; | ||
| } else { | ||
| log.warn("Ignoring null ThreadFactory returned by {}", customizer.getClass().getName()); | ||
| } | ||
| } catch (Exception e) { | ||
| log.warn("Ignoring ThreadFactoryCustomizer {} that failed", customizer.getClass().getName(), e); | ||
| } | ||
| } | ||
| return customized; | ||
| } | ||
|
|
||
| // loaded once, on first use, so that customizers are in place before the first shared pool is created | ||
| private static final class Holder { | ||
|
|
||
| private static final List<ThreadFactoryCustomizer> CUSTOMIZERS = load(); | ||
|
|
||
| private static List<ThreadFactoryCustomizer> load() { | ||
| List<ThreadFactoryCustomizer> customizers = new ArrayList<>(); | ||
| for (ThreadFactoryCustomizer customizer : ServiceLoader.load(ThreadFactoryCustomizer.class)) { | ||
| customizers.add(customizer); | ||
| } | ||
| return Collections.unmodifiableList(customizers); | ||
| } | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
...st/java/org/testcontainers/containers/wait/strategy/AbstractWaitStrategyExecutorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.testcontainers.containers.wait.strategy; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.testcontainers.custom.TestThreadFactoryCustomizer; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class AbstractWaitStrategyExecutorTest { | ||
|
|
||
| @Test | ||
| void waitStrategyThreadsAreCreatedByTheCustomizedFactory() throws Exception { | ||
| Thread thread = AbstractWaitStrategy.EXECUTOR.submit(Thread::currentThread).get(); | ||
|
|
||
| assertThat(thread.getName()).startsWith("testcontainers-wait-"); | ||
| assertThat(thread.isDaemon()).isTrue(); | ||
| assertThat(TestThreadFactoryCustomizer.CREATED).contains(thread); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
core/src/test/java/org/testcontainers/custom/TestThreadFactoryCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.testcontainers.custom; | ||
|
|
||
| import org.testcontainers.core.ThreadFactoryCustomizer; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Set; | ||
| import java.util.WeakHashMap; | ||
| import java.util.concurrent.ThreadFactory; | ||
|
|
||
| public class TestThreadFactoryCustomizer implements ThreadFactoryCustomizer { | ||
|
|
||
| public static final Set<Thread> CREATED = Collections.synchronizedSet( | ||
| Collections.newSetFromMap(new WeakHashMap<>()) | ||
| ); | ||
|
|
||
| @Override | ||
| public ThreadFactory customize(ThreadFactory threadFactory) { | ||
| return runnable -> { | ||
| Thread thread = threadFactory.newThread(runnable); | ||
| CREATED.add(thread); | ||
| return thread; | ||
| }; | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
core/src/test/java/org/testcontainers/utility/ThreadFactoryCustomizersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.testcontainers.core.ThreadFactoryCustomizer; | ||
| import org.testcontainers.custom.TestThreadFactoryCustomizer; | ||
| import org.testcontainers.lifecycle.Startable; | ||
| import org.testcontainers.lifecycle.Startables; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.ThreadFactory; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class ThreadFactoryCustomizersTest { | ||
|
|
||
| @Test | ||
| void appliesRegisteredCustomizersToSharedPools() { | ||
| AtomicReference<Thread> startedOn = new AtomicReference<>(); | ||
| Startable startable = new Startable() { | ||
| @Override | ||
| public void start() { | ||
| startedOn.set(Thread.currentThread()); | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() {} | ||
| }; | ||
|
|
||
| Startables.deepStart(startable).join(); | ||
|
|
||
| assertThat(startedOn.get().getName()).startsWith("testcontainers-lifecycle-"); | ||
| assertThat(TestThreadFactoryCustomizer.CREATED).contains(startedOn.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void appliesCustomizersInOrder() { | ||
| ThreadFactory base = runnable -> new Thread(runnable, "base"); | ||
| ThreadFactoryCustomizer first = factory -> runnable -> rename(factory.newThread(runnable), "-first"); | ||
| ThreadFactoryCustomizer second = factory -> runnable -> rename(factory.newThread(runnable), "-second"); | ||
|
|
||
| Thread thread = ThreadFactoryCustomizers.customize(base, Arrays.asList(first, second)).newThread(() -> {}); | ||
|
|
||
| assertThat(thread.getName()).isEqualTo("base-first-second"); | ||
| } | ||
|
|
||
| @Test | ||
| void skipsCustomizersThatFailOrReturnNull() { | ||
| ThreadFactory base = runnable -> new Thread(runnable, "base"); | ||
| ThreadFactoryCustomizer failing = factory -> { | ||
| throw new IllegalStateException("boom"); | ||
| }; | ||
| ThreadFactoryCustomizer returningNull = factory -> null; | ||
|
|
||
| assertThat(ThreadFactoryCustomizers.customize(base, Arrays.asList(failing, returningNull))).isSameAs(base); | ||
| assertThat(ThreadFactoryCustomizers.customize(base, Collections.emptyList())).isSameAs(base); | ||
| } | ||
|
|
||
| private static Thread rename(Thread thread, String suffix) { | ||
| thread.setName(thread.getName() + suffix); | ||
| return thread; | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
core/src/test/resources/META-INF/services/org.testcontainers.core.ThreadFactoryCustomizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.testcontainers.custom.TestThreadFactoryCustomizer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.