madrob commented on code in PR #2619:
URL: https://github.com/apache/solr/pull/2619#discussion_r1705719172


##########
solr/solrj/src/test/org/apache/solr/common/util/ExecutorUtilTest.java:
##########
@@ -108,6 +111,48 @@ public void 
testExecutorUtilAwaitsTerminationWhenTaskRespectsInterupt() throws E
     }
   }
 
+  @Test
+  public void testCMDCAwareCachedThreadPool() {
+    // 5 threads max, unbounded queue
+    ExecutorService executor =
+        ExecutorUtil.newMDCAwareCachedThreadPool(
+            5, Integer.MAX_VALUE, new NamedThreadFactory("test"));
+
+    AtomicInteger concurrentTasks = new AtomicInteger();
+    AtomicInteger maxConcurrentTasks = new AtomicInteger();
+    int taskCount = random().nextInt(100);
+
+    for (int i = 0; i < taskCount; i++) {
+      String core = "id_" + random().nextLong();
+
+      Callable<Void> task =
+          () -> {
+            // ensure we never have too many concurrent tasks
+            int concurrent = concurrentTasks.incrementAndGet();
+            assertTrue(concurrent <= 5);
+            maxConcurrentTasks.getAndAccumulate(concurrent, Math::max);
+
+            // assert MDC context is copied from the parent thread that 
submitted the task
+            assertEquals(core, MDC.get("core"));
+
+            // Sleep for a couple of millis before considering the task is done
+            int delay = random().nextInt(10);
+            Thread.sleep(delay);
+            concurrentTasks.decrementAndGet();
+            return null;
+          };
+
+      MDCLoggingContext.setCoreName(core);
+      executor.submit(task);
+    }
+
+    ExecutorUtil.shutdownAndAwaitTermination(executor);
+
+    // assert the pool was actually multithreaded. Since we submitted many 
tasks,
+    // all the threads should have been started
+    assertEquals(5, maxConcurrentTasks.get());

Review Comment:
   It's possible that taskCount will be < 5 and the test will fail here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to