This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch feature/SLING-13146-adapter-manager-child-thread in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit c3eb72d899c25d477e06dfeacb66764a8e665127 Author: Stefan Seifert <[email protected]> AuthorDate: Tue Mar 31 12:19:25 2026 +0200 SLING-13146 Add more diagnosis information around thread and child thread handling, reuse bundle context for child threads --- .../sling/ThreadsafeMockAdapterManagerWrapper.java | 38 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/apache/sling/testing/mock/sling/ThreadsafeMockAdapterManagerWrapper.java b/core/src/main/java/org/apache/sling/testing/mock/sling/ThreadsafeMockAdapterManagerWrapper.java index fae40ed..4ed6995 100644 --- a/core/src/main/java/org/apache/sling/testing/mock/sling/ThreadsafeMockAdapterManagerWrapper.java +++ b/core/src/main/java/org/apache/sling/testing/mock/sling/ThreadsafeMockAdapterManagerWrapper.java @@ -48,9 +48,7 @@ class ThreadsafeMockAdapterManagerWrapper implements AdapterManager { @Override protected AdapterManagerBundleContextFactory childValue( AdapterManagerBundleContextFactory parentValue) { - // Create a new instance for child threads instead of sharing the parent's instance - // This prevents race conditions when parent and child threads have different lifecycles - return new AdapterManagerBundleContextFactory(); + return new AdapterManagerBundleContextFactory(parentValue); } }; @@ -81,21 +79,40 @@ class ThreadsafeMockAdapterManagerWrapper implements AdapterManager { private static class AdapterManagerBundleContextFactory { + private AdapterManagerBundleContextFactory parent; private BundleContext bundleContext; + AdapterManagerBundleContextFactory() { + // default constructor + } + + AdapterManagerBundleContextFactory(AdapterManagerBundleContextFactory parent) { + // take over bundle context from parent thread if available + this.bundleContext = parent.bundleContext; + } + public void setBundleContext(@NotNull final BundleContext bundleContext) { - log.debug("Set bundle context for AdapterManager, bundleContext={}", bundleContext); + log.debug( + "Set bundle context for AdapterManager, bundleContext={}, factory={}, parent={}", + bundleContext, + this, + parent); this.bundleContext = bundleContext; // register adapter manager MockAdapterManagerImpl adapterManagerImpl = new MockAdapterManagerImpl(); - Dictionary<String, Object> properties = new Hashtable<String, Object>(); + Dictionary<String, Object> properties = new Hashtable<>(); MockOsgi.injectServices(adapterManagerImpl, bundleContext); MockOsgi.activate(adapterManagerImpl, bundleContext, properties); bundleContext.registerService(AdapterManager.class.getName(), adapterManagerImpl, properties); } public void clearBundleContext() { + log.debug( + "Clear bundle context for AdapterManager, bundleContext={}, factory={}, parent={}", + bundleContext, + this, + parent); this.bundleContext = null; } @@ -104,12 +121,19 @@ class ThreadsafeMockAdapterManagerWrapper implements AdapterManager { if (bundleContext == null) { BundleContext newBundleContext = MockOsgi.newBundleContext(); log.warn( - "Create new bundle context for adapter manager because it was null, bundleContext={}", - bundleContext); + "Create new bundle context for adapter manager because it was null, bundleContext={}, factory={}, parent={}", + bundleContext, + this, + parent); setBundleContext(newBundleContext); } ServiceReference<AdapterManager> serviceReference = bundleContext.getServiceReference(AdapterManager.class); if (serviceReference != null) { + log.trace( + "Get AdapterManager service from bundle context, bundleContext={}, factory={}, parent={}", + bundleContext, + this, + parent); return bundleContext.getService(serviceReference); } else { throw new RuntimeException("AdapterManager not registered in bundle context.");
