Skip to content

Make arquillian-core thread-safe for parallel JUnit 5/6 class execution#835

Open
olamy wants to merge 2 commits intoarquillian:mainfrom
olamy:parallel-testing
Open

Make arquillian-core thread-safe for parallel JUnit 5/6 class execution#835
olamy wants to merge 2 commits intoarquillian:mainfrom
olamy:parallel-testing

Conversation

@olamy
Copy link
Copy Markdown
Contributor

@olamy olamy commented Apr 30, 2026

Signed-off-by: Olivier Lamy olamy@apache.org

Short description of what this resolves:

Running JUnit 5/6 with parallel classes against an Arquillian-managed container fails — one Manager is shared across the whole JVM and its context-activation stack is thread-local, so parallel classes
stomp on each other.

Changes proposed in this pull request:

  • Opt-in per-class Manager: new system property arquillian.junit5.manager.perClass (default false). When true, JUnitJupiterTestClassLifecycleManager caches its adaptor under a namespace keyed by the test class, so each class gets its own TestRunnerAdaptor and underlying Manager. BeforeSuite / AfterSuite then fire once per class, the only sensible semantics when classes truly run in parallel. When false (the default), the manager is the classic JVM-wide singleton, matching pre-PR behaviour; this preserves compatibility with shared managed containers (WildFly, Payara, GlassFish) that boot once and serve every test class. Documented in junit5/README.adoc.
  • Native ThreadLocal in ArquillianThreadLocal — the old Thread.getId()-keyed map leaked stale values when a ForkJoinPool worker's id got recycled. java.lang.ThreadLocal puts storage on the Thread itself.
    Public API is unchanged.
  • CopyOnWriteArrayList for ManagerImpl.contexts / extensions — observers can register during event dispatch, which used to risk ConcurrentModificationException.
    • volatile on ContainerImpl.state / failureCause — no more data race on state transitions.

Fixes #

Single-threaded behaviour unchanged. But running The Servlet TCK (1751 tests) now passes at parallelism=4 where it previously threw 80+ errors with Jetty 12.1.x. Execution going down from ~13 minutes to 2/3 minutes.

Summary by Sourcery

Make Arquillian core and JUnit 5 integration safe for parallel test class execution by using proper thread-local storage and thread-safe collections.

Enhancements:

  • Replace custom thread-id map in ArquillianThreadLocal with a real ThreadLocal and adjust clear semantics to avoid leaked or stale values under thread reuse.
  • Scope JUnitJupiterTestClassLifecycleManager to a JUnit 5 root store namespace keyed by test class so each class gets its own TestRunnerAdaptor and Manager in parallel runs.
  • Make ContainerImpl state and failureCause fields volatile to ensure visibility across threads.
  • Use CopyOnWriteArrayList for ManagerImpl contexts and extensions collections to support concurrent access and modification.

Summary by Sourcery

Make Arquillian core and JUnit 5 integration safe for parallel test class execution by introducing an optional per-test-class manager and improving thread-safety primitives.

New Features:

  • Add an opt-in per-test-class JUnit 5 manager controlled by the arquillian.junit5.manager.perClass system property to support truly parallel test class execution.

Bug Fixes:

  • Fix race conditions and context leakage that occurred when running tests in parallel against Arquillian-managed containers.

Enhancements:

  • Replace the custom thread-id-based storage in ArquillianThreadLocal with a proper ThreadLocal-backed implementation for safer thread reuse.
  • Make ContainerImpl state and failureCause fields volatile to ensure correct visibility of container state transitions across threads.
  • Use CopyOnWriteArrayList for ManagerImpl contexts and extensions to allow safe concurrent registration during event dispatch.

Documentation:

  • Document the new per-class manager behaviour and configuration in the JUnit 5 README.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 30, 2026

Reviewer's Guide

Makes Arquillian core and its JUnit 5 integration safe for parallel class execution by introducing an opt-in per-test-class Manager, switching to real ThreadLocal storage, and tightening thread-safety of container state and manager collections.

Sequence diagram for JUnitJupiterTestClassLifecycleManager manager retrieval

sequenceDiagram
    participant JUnitEngine as JUnitEngine
    participant Extension as ArquillianExtension
    participant LifecycleMgr as JUnitJupiterTestClassLifecycleManager
    participant Context as ExtensionContext
    participant Store as ExtensionContextStore

    JUnitEngine->>Extension: beforeAll(context)
    Extension->>LifecycleMgr: getManager(context)

    alt PER_CLASS_MANAGER true
        LifecycleMgr->>Context: getRequiredTestClass()
        LifecycleMgr->>Context: getRoot()
        LifecycleMgr->>Context: getStore(Namespace(JUnitJupiterTestClassLifecycleManager, testClass))
    else PER_CLASS_MANAGER false
        LifecycleMgr->>Context: getRoot()
        LifecycleMgr->>Context: getStore(Namespace(JUnitJupiterTestClassLifecycleManager))
    end

    LifecycleMgr->>Store: getOrComputeIfAbsent(MANAGER_KEY, factory)
    alt first access for MANAGER_KEY
        Store-->>LifecycleMgr: create new manager via factory
        LifecycleMgr->>LifecycleMgr: initializeAdaptor()
        alt initializeAdaptor throws
            LifecycleMgr->>LifecycleMgr: caughtInitializationException = e
        end
    else existing manager
        Store-->>LifecycleMgr: existing manager instance
    end

    LifecycleMgr-->>Extension: manager instance
    Extension->>LifecycleMgr: hasInitializationException()
    alt initialization failed
        Extension->>LifecycleMgr: handleSuiteLevelFailure()
    else initialization ok
        Extension->>LifecycleMgr: use adaptor for tests
    end

    JUnitEngine->>Context: root context closed
    Context->>LifecycleMgr: close()
    LifecycleMgr->>LifecycleMgr: afterSuite() and cleanup
Loading

Class diagram for updated concurrency-related classes

classDiagram
    class JUnitJupiterTestClassLifecycleManager {
        <<extensionResource>>
        +static String PER_CLASS_MANAGER_PROPERTY
        +static boolean PER_CLASS_MANAGER
        -static String MANAGER_KEY
        -TestRunnerAdaptor adaptor
        -Exception caughtInitializationException
        -JUnitJupiterTestClassLifecycleManager()
        +static JUnitJupiterTestClassLifecycleManager getManager(ExtensionContext context)
        -void initializeAdaptor() throws Exception
        -boolean hasInitializationException()
        -void handleSuiteLevelFailure()
        +void close()
    }

    class ArquillianThreadLocal~T~ {
        -volatile ThreadLocal~T~ delegate
        +ArquillianThreadLocal()
        -ThreadLocal~T~ newDelegate()
        +T initialValue()
        +T get()
        +void remove()
        +void clear()
    }

    class ContainerImpl~T extends ContainerConfiguration~ {
        -DeployableContainer~T~ deployableContainer
        -String name
        -volatile State state
        -volatile Throwable failureCause
        -ContainerDef containerConfiguration
    }

    class ManagerImpl {
        -CopyOnWriteArrayList~Context~ contexts
        -CopyOnWriteArrayList~Extension~ extensions
        -RuntimeLogger runtimeLogger
        +ManagerImpl(Collection~Class~ extends Context~~ contextClasses, Collection~Class~~ extensionClasses)
    }

    class ExtensionContext {
    }

    class TestRunnerAdaptor {
    }

    class ContainerConfiguration {
    }

    class DeployableContainer~T extends ContainerConfiguration~ {
    }

    class State {
    }

    class ContainerDef {
    }

    class Context {
    }

    class Extension {
    }

    class RuntimeLogger {
    }

    JUnitJupiterTestClassLifecycleManager --> TestRunnerAdaptor : owns
    JUnitJupiterTestClassLifecycleManager --> ExtensionContext : used in getManager
    ArquillianThreadLocal ..|> ThreadLocal : wraps
    ContainerImpl --> DeployableContainer : uses
    ContainerImpl --> ContainerConfiguration : parameterized by
    ContainerImpl --> State : holds state
    ContainerImpl --> ContainerDef : configured by
    ManagerImpl --> Context : manages
    ManagerImpl --> Extension : manages
    ManagerImpl --> RuntimeLogger : uses
Loading

Flow diagram for ArquillianThreadLocal lifecycle operations

flowchart TD
    A[Thread calls get] --> B[delegate.get]
    B --> C{Value present for thread?}
    C -->|Yes| D[Return existing value]
    C -->|No| E[delegate.initialValue]
    E --> F[ArquillianThreadLocal.initialValue]
    F --> G[Store value in delegate for current thread]
    G --> D

    H[Thread calls remove] --> I[delegate.remove for current thread]

    J[Some component calls clear] --> K[delegate = newDelegate]
    K --> L[All threads see fresh ThreadLocal]
    L --> M[Next get uses initialValue again]
Loading

File-Level Changes

Change Details Files
Add opt-in per-test-class TestRunnerAdaptor/Manager scoping for JUnit 5 to avoid cross-thread interference in parallel class execution.
  • Introduce PER_CLASS_MANAGER_PROPERTY and PER_CLASS_MANAGER flag to control per-class vs singleton manager behavior.
  • Change manager lookup to use a Namespace keyed either only by JUnitJupiterTestClassLifecycleManager (singleton) or additionally by the required test class (per-class).
  • Use Store.getOrComputeIfAbsent to lazily construct and initialize the lifecycle manager and capture initialization exceptions instead of manual null-check logic.
  • Update class documentation to clearly describe the two caching modes and suite lifecycle semantics.
junit5/core/src/main/java/org/jboss/arquillian/junit5/JUnitJupiterTestClassLifecycleManager.java
junit5/README.adoc
Replace custom thread-id-based storage in ArquillianThreadLocal with a proper ThreadLocal, and adjust clear semantics to avoid leaks under thread reuse.
  • Remove Hashtable<Long, T>-backed implementation keyed by Thread.getId().
  • Introduce a volatile ThreadLocal delegate with overridden initialValue() delegating to ArquillianThreadLocal.initialValue().
  • Implement get() and remove() by delegating to the underlying ThreadLocal.
  • Change clear() to swap in a fresh ThreadLocal instance, dropping all thread values at once.
core/spi/src/main/java/org/jboss/arquillian/core/spi/ArquillianThreadLocal.java
Harden Manager and container internals for concurrent access by making collections and state fields thread-safe.
  • Change ManagerImpl.contexts and ManagerImpl.extensions from ArrayList to CopyOnWriteArrayList to avoid ConcurrentModificationException when observers/extensions are added during event dispatch.
  • Mark ContainerImpl.state and ContainerImpl.failureCause as volatile to ensure safe publication and visibility of state transitions and error causes across threads.
core/impl-base/src/main/java/org/jboss/arquillian/core/impl/ManagerImpl.java
container/impl-base/src/main/java/org/jboss/arquillian/container/impl/ContainerImpl.java

Possibly linked issues

  • #: PR’s default singleton Manager mode ensures Payara/GlassFish start once, preventing the repeated bootstrapping failure described.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Signed-off-by: Olivier Lamy <olamy@apache.org>
@olamy olamy force-pushed the parallel-testing branch from cb54d30 to 7ef262c Compare April 30, 2026 00:57
Signed-off-by: Olivier Lamy <olamy@apache.org>
*
* @see JUnitJupiterTestClassLifecycleManager
*/
public static final String PER_CLASS_MANAGER_PROPERTY = "arquillian.junit5.manager.perClass";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the only big change here and it's not activated per default

@olamy
Copy link
Copy Markdown
Contributor Author

olamy commented Apr 30, 2026

@arquillian/core can you please review this one? The changes are not very significant (maybe except one which is not activated per default).
running the Servlet TCK for Jetty is going down from 13min ish to 3min ish
Thanks

@olamy
Copy link
Copy Markdown
Contributor Author

olamy commented Apr 30, 2026

@sourcery-ai maybe update your review with last changes

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 30, 2026

Sure! I'm generating a new review now.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Using CopyOnWriteArrayList for contexts and extensions avoids ConcurrentModificationException but can be very costly if you add/remove observers frequently; if registration is not strictly one-time at startup, consider a different concurrent collection (e.g. synchronized List or a lock around a plain ArrayList) to avoid repeated array copies under load.
  • ArquillianThreadLocal.clear() now swaps in a new ThreadLocal, which leaves the old ThreadLocal instance and its per-thread values to be cleaned up only when threads die; if clear() is called repeatedly in a long-lived process with thread pools, this can accumulate stale ThreadLocals, so you may want to document that clear() is intended to be used sparingly or consider explicitly removing from known threads when feasible.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Using CopyOnWriteArrayList for contexts and extensions avoids ConcurrentModificationException but can be very costly if you add/remove observers frequently; if registration is not strictly one-time at startup, consider a different concurrent collection (e.g. synchronized List or a lock around a plain ArrayList) to avoid repeated array copies under load.
- ArquillianThreadLocal.clear() now swaps in a new ThreadLocal, which leaves the old ThreadLocal instance and its per-thread values to be cleaned up only when threads die; if clear() is called repeatedly in a long-lived process with thread pools, this can accumulate stale ThreadLocals, so you may want to document that clear() is intended to be used sparingly or consider explicitly removing from known threads when feasible.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 30, 2026

Hey @olamy, I've posted a new review for you!

@rhusar
Copy link
Copy Markdown
Member

rhusar commented Apr 30, 2026

@arquillian/core can you please review this one? The changes are not very significant (maybe except one which is not activated per default). running the Servlet TCK for Jetty is going down from 13min ish to 3min ish Thanks

Cool stuff @olamy

I believe for #673 the agreement was that the approach is not right - though I don't see now where the conversations happened. I assume this takes a different approach with manager instance where appropriate.

It looks like you also tested this in your testsuite successfully, which is great to hear. I ll be off next week, but I ll have a look when I come back

@olamy
Copy link
Copy Markdown
Contributor Author

olamy commented Apr 30, 2026

@arquillian/core can you please review this one? The changes are not very significant (maybe except one which is not activated per default). running the Servlet TCK for Jetty is going down from 13min ish to 3min ish Thanks

Cool stuff @olamy

I believe for #673 the agreement was that the approach is not right - though I don't see now where the conversations happened. I assume this takes a different approach with manager instance where appropriate.

correct. the current changes are less intrusive. (and works, well at least for me :) )

It looks like you also tested this in your testsuite successfully, which is great to hear. I ll be off next week, but I ll have a look when I come back

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants