You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by js...@apache.org on 2023/10/10 22:43:10 UTC

[sling-org-apache-sling-resourceresolver] branch testing/extra-logging updated: log uncaught exception in thread pool threads

This is an automated email from the ASF dual-hosted git repository.

jsedding pushed a commit to branch testing/extra-logging
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git


The following commit(s) were added to refs/heads/testing/extra-logging by this push:
     new 868be87  log uncaught exception in thread pool threads
868be87 is described below

commit 868be8791b0cc990942daeb3914f3ce2671960ab
Author: Julian Sedding <js...@apache.org>
AuthorDate: Wed Oct 11 00:42:56 2023 +0200

    log uncaught exception in thread pool threads
---
 .../resourceresolver/impl/FactoryRegistrationHandler.java  | 10 ++++++++--
 .../impl/FactoryRegistrationHandlerTest.java               | 14 ++++++--------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java b/src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java
index f738f06..3627f5e 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandler.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.resourceresolver.impl;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.api.resource.runtime.RuntimeService;
 import org.osgi.framework.Bundle;
@@ -62,7 +63,11 @@ public class FactoryRegistrationHandler implements AutoCloseable {
     public FactoryRegistrationHandler() {
         final String originalThreadName = Thread.currentThread().getName();
         this.factoryRegistrationWorker = Executors.newSingleThreadExecutor(
-                r -> new Thread(r, ResourceResolverFactory.class.getSimpleName() + " registration/deregistration (" + originalThreadName + ")"));
+                r -> {
+                    final Thread thread = new Thread(r, originalThreadName + "|" + ResourceResolverFactory.class.getSimpleName() + " registration/deregistration");
+                    thread.setUncaughtExceptionHandler((t, e) -> LOG.warn("Uncaught exception in thread '{}'", t.getName(), e));
+                    return thread;
+                });
     }
 
     public void configure(ResourceResolverFactoryActivator activator, FactoryPreconditions factoryPreconditions) {
@@ -172,7 +177,8 @@ public class FactoryRegistrationHandler implements AutoCloseable {
     private void runWithThreadName(String threadNameSuffix, Runnable task) {
         final String name = Thread.currentThread().getName();
         try {
-            Thread.currentThread().setName(ResourceResolverFactory.class.getSimpleName() + " " + threadNameSuffix + " (" + name + ")");
+            final String infix = "|" + ResourceResolverFactory.class.getSimpleName();
+            Thread.currentThread().setName(StringUtils.substringBefore(name, infix) + infix + " " + threadNameSuffix);
             task.run();
         } finally {
             Thread.currentThread().setName(name);
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandlerTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandlerTest.java
index d916123..c5a34d9 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandlerTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/FactoryRegistrationHandlerTest.java
@@ -26,7 +26,6 @@ import org.apache.sling.resourceresolver.util.events.RecordingListener;
 import org.apache.sling.resourceresolver.util.events.ServiceEventUtil.ServiceEventDTO;
 import org.apache.sling.serviceusermapping.ServiceUserMapper;
 import org.apache.sling.testing.mock.osgi.junit5.OsgiContext;
-import org.apache.sling.testing.mock.osgi.junit5.OsgiContextBuilder;
 import org.apache.sling.testing.mock.osgi.junit5.OsgiContextExtension;
 import org.hamcrest.Matcher;
 import org.jetbrains.annotations.NotNull;
@@ -41,6 +40,8 @@ import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.lang.reflect.Method;
+
 import static org.apache.sling.resourceresolver.util.CustomMatchers.allOf;
 import static org.apache.sling.resourceresolver.util.CustomMatchers.hasItem;
 import static org.apache.sling.resourceresolver.util.events.ServiceEventUtil.registration;
@@ -56,8 +57,6 @@ import static org.mockito.Mockito.when;
 @ExtendWith(OsgiContextExtension.class)
 class FactoryRegistrationHandlerTest {
 
-    private static final Logger LOG = LoggerFactory.getLogger(FactoryRegistrationHandlerTest.class);
-
     private static final int DEFAULT_TEST_ITERATIONS = 20;
 
     private static final @NotNull Matcher<Iterable<? extends ServiceEventDTO>> RRF_REGISTRATION = allOf(
@@ -76,10 +75,7 @@ class FactoryRegistrationHandlerTest {
             hasItem(registration(ResourceResolverFactory.class))
     );
 
-    OsgiContext osgi = new OsgiContextBuilder()
-            .afterSetUp(context -> LOG.info("--- INITIALIZING OSGiContext {}", context))
-            .beforeTearDown(context -> LOG.info("--- TEARING DOWN OSGiContext {}", context))
-            .build();
+    OsgiContext osgi = new OsgiContext();
 
     private ResourceResolverFactoryActivator activator;
     private String originalThreadName;
@@ -87,7 +83,9 @@ class FactoryRegistrationHandlerTest {
     @BeforeEach
     void setUp(TestInfo testInfo) {
         originalThreadName = Thread.currentThread().getName();
-        Thread.currentThread().setName(originalThreadName + "(" + testInfo.getDisplayName() + ")");
+        final String testName = testInfo.getTestMethod().map(Method::getName).map("#"::concat).orElse("")
+                + "(" + testInfo.getDisplayName() + ")";
+        Thread.currentThread().setName(testName);
         final ResourceProviderTracker resourceProviderTracker = mock(ResourceProviderTracker.class);
         doReturn(mock(ResourceProviderStorage.class)).when(resourceProviderTracker).getResourceProviderStorage();