madrob commented on code in PR #2619:
URL: https://github.com/apache/solr/pull/2619#discussion_r1705773515
##########
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:
Your previous code submitted uniformly random 0-100 tasks, it would have
failed 5% of the time. Thank you for adding the `5 + ...` and the latch.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]